gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:19:54 30 4
gpt4 key购买 nike

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

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

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

我使用树形标签 api 获取标签并将每个标签设置为特定级别。输出将在带有 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.

最佳答案

我不确定是什么导致了这个问题,因为我无法重现它,但我看到这段代码有很多问题。我不记得 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);
}

我希望这能以某种方式帮助您。

关于javascript - 使用 org-mode 在 emacs 中管理 firefox 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958009/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com