[英](Chrome Extension) chrome.tabs.remove auto deleting tabs that I open when clicking on hyperlinks
Context: I'm building a chrome extension which closes tabs (inputted by the user).上下文:我正在构建一个关闭选项卡(由用户输入)的 chrome 扩展。 In my background script (btw, manifest V3), I have some code written that uses chrome.tabs.remove
to close tabs who's URL include the string value of the website to block.在我的后台脚本(顺便说一句,清单 V3)中,我编写了一些代码,这些代码使用chrome.tabs.remove
来关闭其 URL 包含要阻止的网站的字符串值的选项卡。
My Problem: As this is a chrome extension, there is an options page.我的问题:因为这是一个 chrome 扩展,所以有一个选项页面。 I have a setting button in my popup that when clicked opens up the chrome extension's options page in a new tab.我的弹出窗口中有一个设置按钮,单击该按钮会在新选项卡中打开 chrome 扩展的选项页面。 The same thing happens when you right click on the extension and click options.当您右键单击扩展程序并单击选项时,也会发生同样的事情。 Occasionally, this also occurs when I open a new tab (or use the keyboard shortcut ctrl + t to open a new tab).有时,当我打开一个新选项卡(或使用键盘快捷键 ctrl + t 打开一个新选项卡)时也会发生这种情况。 However, somehow every time I click on one of those buttons/links, it's instantly auto deleted by my extension, and this error appears in the console/extension error list: Uncaught (in promise) Error: No tab with id: 167.但是,不知何故,每次我单击其中一个按钮/链接时,它都会立即被我的扩展程序自动删除,并且此错误出现在控制台/扩展程序错误列表中:未捕获(承诺)错误:没有 ID 为 167 的选项卡。
(This also happens when clicking on any other hyperlink.) (单击任何其他超链接时也会发生这种情况。)
Yes, I have the [tabs] permission as well as the [activeTab] permission.是的,我拥有 [tabs] 权限以及 [activeTab] 权限。
Interestingly, at some point it starts working again (sometimes) but if it does, the chrome.tabs.remove
no longer works.有趣的是,在某些时候它会再次开始工作(有时),但如果确实如此,则chrome.tabs.remove
将不再工作。 In fact, by looking at the console, the whole function responsible to deal with the chrome.tabs.remove
fails to work, as my console.log doesn't print anything to the console when it should.事实上,通过查看控制台,负责处理chrome.tabs.remove
的整个函数无法正常工作,因为我的 console.log 没有在控制台打印任何内容。 So it would be great if someone could explain to me why the event listeners randomly stop working as well :).因此,如果有人可以向我解释为什么事件侦听器也会随机停止工作,那就太好了:)。
Code: note, some console.log is in there from my debugging代码:注意,我的调试中有一些console.log
function loopInfo(tabsid, trueorfalse) {
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/query
let querying = chrome.tabs.query({currentWindow: true, active: true});
querying.then((tabs) => {
chrome.storage.sync.get("list", (value) => {
var websiteList = value.list;
console.log(String(holdURL) + "1");
for (var i = 0; i < websiteList.length; i++) {
console.log("It is looping..." + "2");
if (String(holdURL).includes(websiteList[i]) || websiteList[i].includes(String(holdURL))) {
// onUpdated
if (trueorfalse) {
chrome.tabs.remove(parseInt(tabsid));
}
// onActivated
chrome.tabs.remove(parseInt(tabit));
console.log("It worked!!!!!!!!!!!!!" + "3");
}
}
});
}, () =>{});
}
var holdURL;
var tabit;
chrome.tabs.onActivated.addListener((event) => {
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
var tab = tabs[0];
holdURL = tab.url;
console.log(tab.url + " onActivated");
tabit = event.tabId;
});
// the 1 below is not a tab id, it's just a placeholder
loopInfo(1, false);
})
chrome.tabs.onUpdated.addListener((tabsid, changeInfo, tab) => {
console.log(changeInfo.status);
if (changeInfo.status == "complete") {
var tempId = tabsid;
holdURL = tab.url;
console.log(tab.url + " onUpdated");
loopInfo(tempId, true);
}
});
Much thanks!!非常感谢!!
For some reason, the condition for the if statement containing the chrome.tabs.remove
commands turns out to be true whenever a tab instantly loads.出于某种原因,只要选项卡立即加载,包含chrome.tabs.remove
命令的 if 语句的条件就会变为 true。 There is a possibility that the running of onActivated
occurs faster than the load of the page, so that it receives an arbitrary url which passes the condition I made for the if statement later. onActivated
的运行有可能比页面的加载速度更快,因此它接收到一个任意 url,该 url 传递了我稍后为 if 语句设置的条件。
Temporary fix: make the function call in onActivated
to have a setTimeout(loopInfo(1, false), 1500)
.临时修复:使onActivated
中的函数调用具有setTimeout(loopInfo(1, false), 1500)
。 1500 ms just in case of computer lag/low internet speed. 1500 毫秒,以防电脑延迟/网速低。
Would be interested in seeing a better solution though.不过有兴趣看到更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.