(Chrome扩展)Chrome.tabs.Remove自动删除选项卡,我在单击超链接时打开

发布于 2025-02-05 07:12:47 字数 2321 浏览 2 评论 0原文

上下文:我正在构建一个关闭选项卡(用户输入)的Chrome扩展名。在我的背景脚本(顺便说一句,清单V3)中,我写了一些代码,其中使用chrome.tabs.remove关闭URL的选项卡,其中包含网站的字符串值以阻止。

我的问题:,因为这是一个镀铬扩展,因此有一个选项页面。我的弹出窗口中有一个设置按钮,当单击时,将在新标签中打开Chrome扩展的选项页面。当您右键单击扩展名并单击选项时,也会发生同样的事情。有时,当我打开一个新选项卡时(或使用键盘快捷键Ctrl + t打开一个新选项卡)时也会发生这种情况。但是,每次我单击其中一个按钮/链接时,它会立即被我的扩展程序删除,并且此错误出现在控制台/扩展错误列表中:未接收到的(未验证)错误:无带有ID的选项卡:167。

(( 167。当单击任何其他超链接时,这也会发生。)

是的,我拥有[TABS]权限以及[Activetab]权限。

有趣的是,在某个时候,它有时会再次工作(有时),但是如果这样做,则chrome.tabs.remove不再有效。实际上,通过查看控制台,负责处理chrome.tabs.remove的整个功能无法正常工作,因为我的控制台。因此,如果有人能向我解释为什么听众也随机停止工作:)。

代码: 请注意,我的调试中有一些台。

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

非常感谢!

Context: I'm building a chrome extension which closes tabs (inputted by the user). 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.

My Problem: As this is a chrome extension, there is an options page. I have a setting button in my popup that when clicked opens up the chrome extension's options page in a new tab. 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). 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.

(This also happens when clicking on any other hyperlink.)

Yes, I have the [tabs] permission as well as the [activeTab] permission.

Interestingly, at some point it starts working again (sometimes) but if it does, the chrome.tabs.remove no longer works. 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. 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

 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2025-02-12 07:12:47

由于某种原因,IF语句包含chrome.tabs.remove命令的条件是正确的。 on Activated的运行可能比页面的负载快得多,因此它会收到一个任意的URL,该URL通过了我稍后为IF语句所做的条件。

临时修复:在on Activated中进行函数调用,以使settimeout(loopinfo(1,false),1500),1500)。 1500毫秒,以防计算机滞后/较低的Internet速度。

不过,有兴趣看到更好的解决方案。

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. 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.

Temporary fix: make the function call in onActivated to have a setTimeout(loopInfo(1, false), 1500). 1500 ms just in case of computer lag/low internet speed.

Would be interested in seeing a better solution though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文