使用org-mode管理emacs中的firefox选项卡

时间:2011-05-11 00:44:16

标签: javascript firefox firefox-addon

我最近找到了一种在emacs中管理firefox选项卡的方法。这听起来有点疯狂。我使用树形样式选项卡(firefox addon),Moz Repl,emacs,org-mode来完成它。

对于10-15个标签,我的计划运作正常。但是20多个标签,我的firefox随机挂起。也许javascript堆栈溢出或其他什么?我不知道我的代码有什么问题。我在这里发布了最重要的代码。有人帮我找些错误吗?

下面是一个基本的firefox chrome代码,你可以在没有emacs和MozPepl的情况下在firefox中运行它。

我使用树形样式选项卡api来获取选项卡并将每个选项卡设置为cetain级别。输出将在带有org-mode的emacs中使用。 树形样式选项卡api:http://piro.sakura.ne.jp/xul/_treestyletab.html.en#api

守则可以多种方式运作。我推荐“工作区插件”。复制我的代码,选择chrome上下文来运行它。 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.

1 个答案:

答案 0 :(得分:2)

我不确定是什么导致了这个问题,因为我无法重现它,但我看到这段代码存在很多问题。我不记得MozRepl是如何工作的,但是这个改进的代码应该会给你一个很好的org-mode友好选项卡输出。我希望这可以帮助你,或者无论谁遇到这个主题。

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);
}

我希望这会有所帮助。