Reub
Reub

Reputation: 392

Firefox Webextensions: Run code on browser start

Using the WebExtensions api, how do I run some code on browser start? I need to be able to distinguish browser restart events from normal start events.

Before WebExtensions, it could be done by checking the loadReason as mentioned in this post. Reference: MDN docs.

One way would be to listen for the windows.onCreated event and then check if it is the first window created (MDN docs). However, I don't see a way to distinguish browser restart events using this method.

EDIT:

My use case: I need to open a few tabs when the browser is opened. But, I do not want the tabs opened when I'm just restarting the browser for whatever reason (ex: installing updates).

EDIT 1:

Checking if there are tabs with the same url already open will not always work correctly because some urls redirect to another page (a login page for example), which will result in the urls not matching and the tab being opened again.

Upvotes: 0

Views: 1005

Answers (2)

Smile4ever
Smile4ever

Reputation: 3704

Your best bet is probably keeping track of the urls and tabIds of tabs you opened in background.js. https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/onCreated

let tabUrls = [];
function handleCreated(tab) {
  tabUrls[tab.id] = tab.url;
}

browser.tabs.onCreated.addListener(handleCreated);

Then you'd have to subscribe to the onUpdated event to track previous URL of the tabs you opened. https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/onUpdated

function handleUpdated(tabId, changeInfo, tabInfo) {
  if (changeInfo.url && tabUrls[tabId] != null) {
    tabUrls[tabId] = changeInfo.url;
  }
}

browser.tabs.onUpdated.addListener(handleUpdated);

Then you'd have to subscribe to https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/onRemoved to delete urls of tabs you opened that have been closed.

function handleRemoved(tabId, removeInfo) {
  tabUrls[tabId] = null;
}

browser.tabs.onRemoved.addListener(handleRemoved);

To persist these changes across browser (re)starts, you will need to save tabUrls variable inside browser.storage.local when a tab is created, updated or removed (without tabIds):

function updateStorage(){
  browser.storage.local.set("urlValues", Object.values(tabUrls)); // Store URLs without tab ids. They will probably not be the same after a restart or quit, you can only assume the urls are correct
}

Next steps:

  • Read from local storage on initialisation of background.js (just call a function in background.js, you can call that function init or something like that), check if any of the current URLs match to those stored URLs or the start URLs. If no match is found, you should open that URL.
  • Remove onUpdated listener after a second or less, when all required tabs are opened, to prevent user navigation from impacting your logic

You could use webRequest https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest to see whether the page you are requesting is a redirect (statusCode = 302 or 301 - see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onHeadersReceived)

Upvotes: 1

Andrew Swan
Andrew Swan

Reputation: 1343

I'm not sure what you mean by "normal start events", extension startup outside of browser startup would only happen for relatively uncommon events like the first install, manual disbaling/enabling, or extension updates.

But in any case, the runtime.onStartup event is what you want: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onStartup

Upvotes: 1

Related Questions