Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:关闭打开的选项卡,无论其是否为当前选项卡_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Chrome扩展:关闭打开的选项卡,无论其是否为当前选项卡

Javascript Chrome扩展:关闭打开的选项卡,无论其是否为当前选项卡,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,这是我的chrome扩展的background.js。我是一个编程chrome扩展的新手。 如何关闭本例中打开的选项卡,而不管该选项卡是否为当前选项卡 chrome.tabs.onCreated.addListener(function() { chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { url = tabs[0].url;

这是我的chrome扩展的background.js。我是一个编程chrome扩展的新手。 如何关闭本例中打开的选项卡,而不管该选项卡是否为当前选项卡

chrome.tabs.onCreated.addListener(function() {
    chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, 
        function (tabs) {
            url = tabs[0].url;
            if (url=="http://www.example.com"){
                setTimeout(function(){
                    //would be nice to have this executed after EVERYTHING on the page is loaded
                    chrome.tabs.executeScript(null, {file:"jquery-1.11.1.min.js"});
                    chrome.tabs.executeScript(null, {file:"contentscript.js"});
                },17000);
                setTimeout(function(){
                    tabs[0].remove();
                    alert('tab removed');
                },25000);
            }
        }
    );
});

from是正确的-只需使用chrome.tabs.remove(…您要删除的选项卡id…)

假设我理解您的意图,此代码中的许多代码都需要改进

假设您在选项卡中的链接上单击鼠标中键。它在后台创建一个选项卡,并为其调用
onCreated
listener

然而,您查询活动选项卡,并获得初始选项卡,而不是新选项卡

  • 您应该使用选项卡信息的事实

  • 当您调用
    executeScript
    时,它仍然遵循,默认情况下,它位于
    document\u idle
    。这意味着至少在静态DOM就绪之前,它不会执行

    所以不需要超时。为了更加安全,您可以将内容脚本代码包装在
    $(document.ready()

    如果要等待图像等资源加载,可以使用主体的
    load
    event

    如果需要等待特定事件发生/某些脚本在页面中执行,可以使用timeout和/或类似的方法。然而,将这种等待添加到
    contentscript.js而不是注入代码中会更有意义

    chrome.tabs.onCreated.addListener(function(tab) { // Can have a parameter!
      // Assuming you want to check the address contains that and not exactly it
      if(~tab.url.indexOf("http://www.example.com")) {
        chrome.tabs.executeScript(tab.id, {file:"jquery-1.11.1.min.js"});
        chrome.tabs.executeScript(tab.id, {file:"contentscript.js"});
      }
    });
    
  • 最后,如果要关闭页面,可以调用
    chrome.tabs.remove(tab.id)

    但最好是直接从内容脚本中关闭它——它可能更清楚什么时候做

    在内容脚本中,如果扩展插件打开了选项卡,则只需使用
    window.close()


  • 你应该使用
    chrome.tabs.remove(tab.id)
    关闭选项卡。

    使用
    chrome.tabs.remove(tabId)

    而tabId是选项卡的id。如果您不知道从哪里获取选项卡ID,可以在background.js中使用以下代码

    chrome.tabs.query(
        {
            active:true,
            windowType:"normal",
             currentWindow: true
        },
        function(d)
        {
            tabId = d[0].id;
        });
    

    你应该使用
    chrome.tabs.remove(tab.id)
    关闭选项卡。见:
    chrome.tabs.query(
        {
            active:true,
            windowType:"normal",
             currentWindow: true
        },
        function(d)
        {
            tabId = d[0].id;
        });