tabs onUpdated event not detected ?

Hi,

I am trying to test along the tabs onUpdated event on a simple web extension/addon under Chrome. I used the Safari XCRUN converter !

What I am trying to do is :

1- Open new tab on Google Scholar with set prefs params, from "options.js" script (code below)

2- Listen for tab to be updated and ready (e.g. tab status is complete)

3 - Then, inject a content script that will simulate the user click on save button

4- Then wait 1,5s (for GS tab to reload and finish saving) and remove the listener

5- Finally close this GS tab and back to extension Options page tab.

    // Detect browser language
    const gsUrl = currentBrowser.i18n.getUILanguage().includes("fr")
      ? GSCHOLAR_SET_PREFS_FR_URL
      : GSCHOLAR_SET_PREFS_COM_URL;
    // Listener to detect when the GS tab has finished loading
    const gsTabListener = (tabId, changeInfo, tabInfo) => {
      if (changeInfo.url && changeInfo.url.startsWith(GSCHOLAR_HOST)) {
        currentBrowser.tabs.executeScript(
          tabId,
          {
            code: `document.getElementsByName("save")[0].click();`,
          },
          () => {
            currentBrowser.tabs.onUpdated.removeListener(gsTabListener);
            setTimeout(() => currentBrowser.tabs.remove(tabId), 1500);
          }
        );
      }
    };
    currentBrowser.tabs.onUpdated.addListener(gsTabListener); // Add tab listener
    currentBrowser.tabs.create({
      url: `${gsUrl}?inst=${gScholarInstIdList.join("&inst=")}&save=#2`,
      active: false,
    }); // Open GS tab according to browser language

The problem is that it works well on Chrome/Edge/Firefox, but not Safari : the GS tab isn't closed and nothing happens :-/

PS:

  • Of course tabs onUpdated event is well supported on Safari according to MDN.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onUpdated

  • I have also tried webNavigation onCompleted event, but same !

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onCompleted

Thanks for your feedback.

FYI, I am developing on :

  • MacBookAir under MacOS Monterey 12.4
  • Safari 15.4 (17613.2.7.18)
  • XCode 13.3.1 (13E500a)
  • My project is bundled with Webpack 5.68.0 (e.g. building the background + content + options assets files).

I really don't see what I am doing wrong (even in debug mode) 0_0

why wouldn't this tab event be intercepted ?

Any Help or Support please ?

After debugging I finally sloved this by noticing that in fact the events were triggered, but missed because of the availability and values of parameters passed into callabck (changeInfo, details) depending on the browser we're on.

So I switched from onUpdated to webNavigation.onCompleted API, which is better suited to our need (tab page fully loaded) and whose parameter is simple and consistent across browsers :-)

const uiLanguage = currentBrowser.i18n.getUILanguage().includes("fr")
  ? "fr"
  : "com"; // Detect browser language
const gsUrl = `${GSCHOLAR_SETTINGS_HOST}.${uiLanguage}`;
// Listener to detect when the GS tab has finished loading
const gsTabListener = (details) => {
  if (details && details.url && details.tabId) {
    if (details.url.startsWith(`${gsUrl}/scholar_settings?`)) {
      currentBrowser.tabs.executeScript(details.tabId, {
        code: `document.getElementsByName("save")[0].click();`,
      });
    } else if (details.url.startsWith(`${gsUrl}/scholar?`)) {
      currentBrowser.webNavigation.onCompleted.removeListener(
        gsTabListener
      );
      currentBrowser.tabs.remove(details.tabId);
    }
  }
};
currentBrowser.webNavigation.onCompleted.addListener(gsTabListener); // Add GS tab listener
currentBrowser.tabs.create({
  url: `${gsUrl}/scholar_settings?inst=${gScholarInstIdList.join(
    "&inst="
  )}&save=#2`,
  active: false,
}); // Open GS tab according to browser language
tabs onUpdated event not detected ?
 
 
Q