Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Google chrome extension 如何在不将标签加载到Google Chrome扩展中的情况下打开标签?_Google Chrome Extension - Fatal编程技术网

Google chrome extension 如何在不将标签加载到Google Chrome扩展中的情况下打开标签?

Google chrome extension 如何在不将标签加载到Google Chrome扩展中的情况下打开标签?,google-chrome-extension,Google Chrome Extension,我唯一能想到的就是使用chrome.tabs.discard,下面是我当前的代码: var test_button= document.getElementById('test_button'); test_button.onclick = function(element) { var to_load = {"url": "https://stackoverflow.com", "active": false, "selected": false}; chrome.tabs.c

我唯一能想到的就是使用
chrome.tabs.discard
,下面是我当前的代码:

var test_button= document.getElementById('test_button');
test_button.onclick = function(element) {
    var to_load = {"url": "https://stackoverflow.com", "active": false, "selected": false};
    chrome.tabs.create(to_load, function(tab) {
        chrome.tabs.discard(tab.id);
    });
};
但是,在加载之前调用
chrome.tabs.discard
会导致chrome将其替换为
about:blank
,而不是阻止加载此页面

我找到的唯一“解决方案”是等待加载选项卡,但在卸载它之前等待加载会破坏目的,特别是当我同时打开大量选项卡时


任何帮助都将不胜感激。

解决方案是只调用
chrome.tabs。在其URL值更新后,放弃选项卡上的
,例如:

var tabs_to_unload = {}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, changedTab) {
    if (tabs_to_unload[tabId] == true) {
        // We can only discard the tab once its URL is updated, otherwise it's replaced with about:empty
        if(changeInfo.url) {
            chrome.tabs.discard(tabId);
            delete tabs_to_unload[tabId];
        }
    }
});

var test_button= document.getElementById('test_button');
test_button.onclick = function(element) {
    var to_load = {"url": "https://stackoverflow.com", "active": false, "selected": false};
    chrome.tabs.create(to_load, function(tab) {
        tabs_to_unload[tab.id] = true;
    });
};
在我的例子中,确切的代码有点不同,因为我是在弹出窗口中执行这些操作的,并且由其脚本注册的变量和侦听器的寿命只有弹出窗口的寿命长,但其背后的原理是相同的