0

In a popup web-extension that I developing, I want to close a certain tab right before my pop-up closes. I tried doing

function onBlur(){
browser.tabs.remove(tabId);
}

window.addEventListener("blur", onBlur);

However, even though browser.tabs.remove(tabId) works normally, it does not seem to work when inside the blur event. Am I doing something wrong? Is there a way to implement this?

Tohiko
  • 1,860
  • 2
  • 18
  • 26
  • 1
    When the popup closes it terminates instantly so you should use `onDisconnect` event in the background script as shown [here](https://stackoverflow.com/a/39756934). You can replace `chrome` with `browser`, of course. – wOxxOm Apr 19 '20 at 14:04
  • That seems to be exactly the problem! Is the correct way to communicate `tabId` in my popup to the background script through `chrome.extension.getBackgroundPage().tabId = tabId;` as see [here](https://stackoverflow.com/questions/9537435/how-to-interact-with-background-js-from-the-popup?noredirect=1&lq=1) or is there a better way? – Tohiko Apr 19 '20 at 14:10
  • 1
    Since you'll be using browser.runtime.connect anyway you can pass tabId through port.postMessage() so your onMessage listener in the background script will store tabId in the outer scope's variable. – wOxxOm Apr 19 '20 at 14:11

0 Answers0