使用 javascript 在 firefox-addon 中关闭带有特定元标签的标签
Closing tabs with a specific meta TAG in firefox-addon using javascript
我有这个插件,它在 opens.Towards 末尾的页面上插入以下 "meta" TAG,我想关闭页眉上带有该 TAG 的所有选项卡。
<meta id="CLOSE_LATER">
我写了一个简单的for循环:
var TAG = "CLOSE_LATER";
var tabs = require("sdk/tabs");
for(var i=0; i<tabs.length; i++){
var tab = tabs[i];
console.log("****START****");
console.log("CLOSING TAB : "+i+"of : "+tabs.length);
searchTag(tab, TAG,i)
.then(function success(rValue) {
if (rValue) {
tab.close();
console.log("CLOSED i: "+i);
}
else{
console.log("NOT CLOSED i: "+i);
}
}, function failure(error) {
console.log("Error : searchTag()");
});
console.log("****END****");
}
其中 searchTag() 将脚本附加到页面 "seachtag.js" 以搜索 TAG。
searchTag() returns promise 解析为 TRUE 是否在页面上找到 TAG,否则解析为 FALSE。
示例情况:我有 3 个 URL,其中只有 URL-2 和 URL-3 的头部有 TAG。所以只有 URL-2 & 3 应该被关闭,但只有 URL-3 被关闭。
下面是示例日志:
试试这个:
var TAG = "CLOSE_LATER";
var tabs = require("sdk/tabs");
var success = function (rValue) {
if (rValue) {
this.close();
//console.log("CLOSED i: "+i);
}
else{
//console.log("NOT CLOSED i: "+i);
}
};
for(var i=0; i<tabs.length; i++){
var tab = tabs[i];
console.log("****START****");
console.log("CLOSING TAB : "+i+"of : "+tabs.length);
searchTag(tab, TAG,i)
.then(success.bind(tab), function failure(error) {
console.log("Error : searchTag()");
});
console.log("****END****");
}
将您感兴趣的 tab
绑定到 success
函数中的 this
变量,这样 success
的每次调用都会有正确的 tab
要处理的对象。
请注意,我注释掉了日志记录,因为您不再有权访问 i
,但如果您希望它再次工作,您只需封装 tab
和 i
在每个循环迭代的新对象中并绑定它。
我以前没有在 promise 中使用过这个技巧,所以 promise 解析可能会破坏绑定,但希望不会。
我有这个插件,它在 opens.Towards 末尾的页面上插入以下 "meta" TAG,我想关闭页眉上带有该 TAG 的所有选项卡。
<meta id="CLOSE_LATER">
我写了一个简单的for循环:
var TAG = "CLOSE_LATER";
var tabs = require("sdk/tabs");
for(var i=0; i<tabs.length; i++){
var tab = tabs[i];
console.log("****START****");
console.log("CLOSING TAB : "+i+"of : "+tabs.length);
searchTag(tab, TAG,i)
.then(function success(rValue) {
if (rValue) {
tab.close();
console.log("CLOSED i: "+i);
}
else{
console.log("NOT CLOSED i: "+i);
}
}, function failure(error) {
console.log("Error : searchTag()");
});
console.log("****END****");
}
其中 searchTag() 将脚本附加到页面 "seachtag.js" 以搜索 TAG。
searchTag() returns promise 解析为 TRUE 是否在页面上找到 TAG,否则解析为 FALSE。
示例情况:我有 3 个 URL,其中只有 URL-2 和 URL-3 的头部有 TAG。所以只有 URL-2 & 3 应该被关闭,但只有 URL-3 被关闭。
下面是示例日志:
试试这个:
var TAG = "CLOSE_LATER";
var tabs = require("sdk/tabs");
var success = function (rValue) {
if (rValue) {
this.close();
//console.log("CLOSED i: "+i);
}
else{
//console.log("NOT CLOSED i: "+i);
}
};
for(var i=0; i<tabs.length; i++){
var tab = tabs[i];
console.log("****START****");
console.log("CLOSING TAB : "+i+"of : "+tabs.length);
searchTag(tab, TAG,i)
.then(success.bind(tab), function failure(error) {
console.log("Error : searchTag()");
});
console.log("****END****");
}
将您感兴趣的 tab
绑定到 success
函数中的 this
变量,这样 success
的每次调用都会有正确的 tab
要处理的对象。
请注意,我注释掉了日志记录,因为您不再有权访问 i
,但如果您希望它再次工作,您只需封装 tab
和 i
在每个循环迭代的新对象中并绑定它。
我以前没有在 promise 中使用过这个技巧,所以 promise 解析可能会破坏绑定,但希望不会。