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;
});
};
在我的例子中,确切的代码有点不同,因为我是在弹出窗口中执行这些操作的,并且由其脚本注册的变量和侦听器的寿命只有弹出窗口的寿命长,但其背后的原理是相同的