如何在不将其加载到谷歌浏览器扩展程序中的情况下打开标签?



我唯一能想到的就是使用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

我找到的唯一"解决方案"是等待选项卡加载,但是在卸载它之前等待它加载会破坏目的,特别是如果我一次打开大量选项卡。

任何帮助将不胜感激。

解决方案是仅在 URL 值更新后在选项卡上调用chrome.tabs.discard,例如:

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;
});
};

就我而言,确切的代码有点不同,因为我是从弹出窗口中执行这些操作的,并且其脚本注册的变量和侦听器仅与弹出窗口一样长,但其背后的原理是相同的。

相关内容

最新更新