The window variable and temporarily storage

When I declare my custom variable like window.myflag and assign to it something, like a string or a class etc… what is its scope? The current browser window or the current tab? I tried to access it from my option page but it was not possible.

Is there some way to store temporary data or session data, that self clean when the browser is closed, or do I need to handle this temporary aspect manually?

It depends on where you’re doing that. In general, extensions will always have their separate scope, and every extension page has its own scope. If you want to store something just for the current session you can just use global variables in the background page while background pages are persistent, for example.

I tired with global variables in my content script, but I found it was not accessible in option page, so I tried with window.myvar and again this was also not accesible in option page. If I declare it with relative class declaration in background script, will the variable persist in content script and option page until I close the browser?

There is no way to directly share data with other contexts. You will want to use messaging to exchange state information, or use one of the browser.storages. If you set something in the background script it will be available int he background script as long as that script stays loaded, irrespective of what other contexts are doing. Every extension page (background page, popup, sidebar, options etc.) essentially runs in its own tab, same for content scripts, which run in the tab they’re injected in. As such, you can’t just expect to be able to access global variables between them.

so storage is the only reliable solution to exchange data? Is there a temporary storage option or do I need to delete local storage after? And what if they disable the local storing of data?

In the Manifest v2 world, your background script can act as the repository of data and reply to messages with current values. I think in other threads there was a discussion that this no longer works in the Manifest v3 world – no background scripts as we have come to know them – so storage is our best bet going forward.

I eventually used window variables, and refresh them for storage.onChanged event.