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

发布于 2024-11-06 11:45:23 字数 2508 浏览 0 评论 0原文

我最近找到了一种在 emacs 中管理 Firefox 选项卡的方法。这听起来有点疯狂。我使用树形选项卡(firefox 插件)、Moz Repl、emacs、org-mode 来完成此操作。

对于 10-15 个选项卡,我的计划效果很好。但是超过 20 个标签页时,我的 Firefox 会随机挂起。也许是javascript堆栈溢出或者其他什么?我不知道我的代码有什么问题。我在这里发布了最重要的代码。有人帮我找到一些错误吗?

下面是一个基本的 Firefox chrome 代码,您可以在 Firefox 中运行它,而不需要 emacs 和 MozPepl。

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

代码可以通过多种方式运行。我推荐“工作区插件”。复制我的代码,选择 chrome 上下文来运行它。 https://addons.mozilla.org/en-US/firefox/addon/工作区/

// 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2024-11-13 11:45:23

我不确定具体是什么导致了这个问题,因为我无法重现它,但我发现这段代码存在很多问题。我不记得 MozRepl 是如何工作的,但是这个改进的代码应该会给你一个很好的组织模式友好的选项卡输出。我希望这对您或任何偶然发现此线程的人有所帮助。

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'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文