(Chrome Extension) chrome.tabs.remove auto deleting tabs that I open when clicking on hyperlinks

Viewed 187

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

1 Answers

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.

Related