简体   繁体   English

Chrome扩展程序:关闭选项卡,该选项卡之前已打开外部程序

[英]Chrome Extension: Close tab that opened an external program before

I'm currently writing a Chrome extension which has a context menu entry and when you click on it, Chrome opens a new tab whose address uses a different protocol. 我目前正在编写一个具有上下文菜单项的Chrome扩展程序,当您单击它时,Chrome浏览器将打开一个新标签,其地址使用其他协议。 (Mine eg is called cmdown:// ) (我的例如被称为cmdown://)

Anyway, the problem is that I can't get these tabs to close after loading and opening the program to handle the protocol. 无论如何,问题是在加载和打开程序来处理协议后,我无法关闭这些选项卡。

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

Use chrome.tabs.query() to get the tab, then close it with chrome.tabs.remove() 使用chrome.tabs.query()来获取选项卡,然后用chrome.tabs.remove关闭()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM