Search code examples
google-chrome-extension

What does a browser session mean in the context of chrome extension


Please note, this question is not about client-server sessions. It's about Chrome session.

I'm reading this article about tabId and it states that:

Tab IDs are unique within a browser session.

What is browser session here? Does the session begin when I open a browser and ends when I close it? Is there a way to track tab across sessions?


Solution

  • What is browser session here? Does the session begin when I open a browser and ends when I close it?

    That's correct. It means a tab will preserve its ID only until you close the browser.

    Even if the browser is configured to reopen the previews windows on startup, they will all have a different tab ID and window ID.

    Is there a way to track tab across sessions?

    Yes, with the tabs permission! Mostly.

    The IDs will be different but you can query all the open tabs and windows and you will receive the position of a tab in a window (e.g. first tab in window 2) together with its URL. You'd have to regularly query and save this data via chrome.storage.local.set()

    You can then compare this piece of information to the data you stored before closing the browser and match them to each other. For example you receive tab on position 1, with URL xyz, in a window with 3 other tabs, you can find a tab with the same details in your storage.

    The "mostly" part: If the user has 2 windows with 1 tab, both pointing to the same page, you won't be able to tell which is which.