Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage firefox tabs in emacs with org-mode

I recently find a way to manage firefox tab in emacs. This sounds a little crazy. I use tree style tabs(firefox addon), Moz Repl, emacs, org-mode to do it.

For 10-15 tabs, my plan works fine. But 20+ tabs, My firefox hangs randomly. Maybe javascript stack overflow or something else? I don't know what's wrong with my code. I post the most import code here. Somesone help me to find some bugs?

It's a basic firefox chrome code below, you can run it in firefox without emacs and MozPepl.

I use tree style tabs api to get tabs and set each tab a cetain level. The output will be used in emacs with org-mode. tree style tabs api: http://piro.sakura.ne.jp/xul/_treestyletab.html.en#api

The Code can run in many ways. I recommend "workspace addon". Copy My code, choose chrome context to run it. https://addons.mozilla.org/en-US/firefox/addon/workspace/

// two helper function to get title and url of tab
function getTitle(tab)
{ 
    var brower = gBrowser.getBrowserForTab(tab)
    var url = brower.currentURI.spec
    var title = brower.contentTitle
    return title
}
function getUrl(tab)
{ 
    var brower = gBrowser.getBrowserForTab(tab)
    var url = brower.currentURI.spec
    var title = brower.contentTitle
    return ":PROPERTIES:\n:URL:"+url+"\n:END:\n"
}

var L = gBrowser.tabContainer.childNodes.length //firefox tabs length
var str = "" //global string for output

//parse tabs. If tab has child, parse it. It tab has no child, just output.
for(i = 0; i < L; i++){
    level = "*"
    tab = gBrowser.tabContainer.childNodes[i]
    if ('TreeStyleTabService' in window){
    if(TreeStyleTabService.hasChildTabs(tab))
    {
      str = [str, level, " [+] ",  getTitle(tab), "\n", getUrl(tab)].join("") //output title and url. level used in org-mode
      treeparse(TreeStyleTabService.getChildTabs(tab), "**") //if a tab has child tabs. parse it and level up
    }   
str = [str, level, " ",  getTitle(tab), "\n", getUrl(tab)].join("")
}

function treeparse(tablist,level) //parse a list of tabs. If tab has not a child, output. If it has childs, parse again
{
    for(i=0 ; i < tablist.length;i++) {
    tab = tablist[i]
    if ('TreeStyleTabService' in window){
        if(TreeStyleTabService.hasChildTabs(tab))
        {
        str = [str, level, " [+] ",  getTitle(tab), "\n", getUrl(tab)].join("")
        newlevel = level + "*"
        treeparse(TreeStyleTabService.getChildTabs(tab),newlevel)       
        }
        } }
    str = [str, level, " ",  getTitle(tab), "\n", getUrl(tab)].join("")
    }
}

alert(str) //alert to view result. You can also write the result into a file.
like image 351
textpattern Avatar asked May 11 '11 00:05

textpattern


1 Answers

I'm not sure what's specifically causing the problem, as I couldn't reproduce it, but I see loads of issues with this code. I can't remember how MozRepl works, but this improved code should give you a nice org-mode friendly tab output. I hope this helps you, or whoever stumbles across this thread.

var bullet = "*"; // Org-mode bullet

// two helper function to get title and url of tab
function getTitle(tab) { 
    var brower = gBrowser.getBrowserForTab(tab);
    var url = brower.currentURI.spec;
    var title = brower.contentTitle;
    return title;
}

function getUrl(tab) { 
    var brower = gBrowser.getBrowserForTab(tab);
    var url = brower.currentURI.spec;
    var title = brower.contentTitle;
    return ":PROPERTIES:\n:URL:"+url+"\n:END:\n";
}

// NOTE: we factor these string-generation functions out,
// to make things a bit more clear
function makeParentNodeOutput(tab, level) {
    return (Array(level+1).join(bullet) +
        " [+] " +
        getTitle(tab) +
        "\n" +
        getUrl(tab));
}

function makeLeafNodeOutput(tab, level) {
    return (Array(level+1).join(bullet) +
        " " +
        getTitle(tab) +
        "\n" +
        getUrl(tab));
}

// NOTE: we only need to handle parsing a collection of tabs
// in once place, and we have a function for it here.
function parseTabCollection(tabs, level) {
    var currentTab;
    var outputString = "";
    for(var i = 0; i < tabs.length; i++){
        currentTab = tabs[i];

        // For a parent node, we output the node and its children
        if(TreeStyleTabService.hasChildTabs(currentTab)){
            outputString += makeParentNodeOutput(currentTab, level);
            outputString += parseTabCollection(
                TreeStyleTabService.getChildTabs(currentTab),
                level + 1
            );
        } else {
            outputString += makeLeafNodeOutput(currentTab, level);
        }
    }
    return outputString;
}

if ('TreeStyleTabService' in window){
    //NOTE: Start with the rootTabs only. The old version started with
    // *all* tabs, which isn't what we want
    var orgModeOutput = parseTabCollection(TreeStyleTabService.rootTabs, 1);
    alert(orgModeOutput);
}

I hope this helps somehow.

like image 71
Richard Peterson Avatar answered Nov 06 '22 14:11

Richard Peterson