我目前正在编写一个具有上下文菜单条目的 Chrome 扩展程序,当您单击它时,Chrome 会打开一个新选项卡,其地址使用不同的协议。(我的例如称为 cmdown:// )
无论如何,问题是我无法在加载和打开程序以处理协议后关闭这些选项卡。
我的背景.js:
chrome.contextMenus.removeAll();
var name1 = localStorage["name1"];
var name2 = localStorage["name2"];
var name3 = localStorage["name3"];
var path1 = localStorage["path1"];
var path2 = localStorage["path2"];
var path3 = localStorage["path3"];
var menuids = [];
function downloadImage(info, tab) {
for(var i = 0; i < menuids.length; i++) {
if(menuids[i] == info.menuItemId) {
switch(i) {
case 0:
chrome.tabs.create({"url":"cmdown://"+info.srcUrl+";"+path1,"active":false}, function(tab){
//Close tab here
});
break;
case 1:
chrome.tabs.create({"url":"cmdown://"+info.srcUrl+";"+path2,"active":false}, function(tab){
//Close tab here
});
break;
case 2:
chrome.tabs.create({"url":"cmdown://"+info.srcUrl+";"+path3,"active":false}, function(tab){
//Close tab here
});
break;
}
}
}
}
if(typeof name1 != "undefined" && typeof path1 != "undefined") menuids[0] = chrome.contextMenus.create({"title": name1, "contexts":["image"],
"onclick": downloadImage});
if(typeof name2 != "undefined" && typeof path2 != "undefined") menuids[1] = chrome.contextMenus.create({"title": name2, "contexts":["image"],
"onclick": downloadImage});
if(typeof name3 != "undefined" && typeof path3 != "undefined") menuids[2] = chrome.contextMenus.create({"title": name3, "contexts":["image"],
"onclick": downloadImage});