Chrome扩展程序:之前打开外部程序的关闭标签

时间:2013-09-02 02:00:40

标签: javascript google-chrome google-chrome-extension tabs

我目前正在编写一个Chrome扩展程序,其中包含一个上下文菜单条目,当您点击它时,Chrome会打开一个新标签,其地址使用不同的协议。 (我的名字叫做cmdown://)

无论如何,问题是我在加载和打开程序以处理协议后无法关闭这些标签。

我的background.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});

1 个答案:

答案 0 :(得分:1)

使用 chrome.tabs.query()获取标签,然后使用 chrome.tabs.remove()

关闭它

http://developer.chrome.com/extensions/tabs.html