Is there a browser storage that is usable only by the page that created it?
I'm making a TamperMonkey script to automate my work. It is triggered when a page from a specific domain is opened. It would then find a specific link (same domain) in said page and open it in the same tab. If the newly opened page matches a condition, it would go back to the previous page (to be checked manually). If it doesn't, then it would close itself.
I used localStorage to mark if a page has tried to do this check. Otherwise, the script would re-open the link when the original page is loaded and got caught in a loop.
The script runs smoothly when only a single tab is running. But it often fails when I run multiple tabs (all in the same domain). I'm guessing that each tab could access the same localStorage, thus messing with the loop check. I have not found anyway to give a unique name through the same script to each tab's localStorage.
As such, I need a browser storage that could be used by a tab even after it opened a new url in the same domain, but not usable by another tab which has the same domain.
The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot.
It's simple. Just go to the developer tools by pressing F12 , then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.
To clear Web Storage in Chrome: Select "More tools → Clear browsing data" from the menu, or type chrome://settings/clearBrowserData in the address bar. In the resulting dialog, select a time range of "All time", check the "Cookies and other site data" option, and click "Clear data".
You can use Window.sessionStorage for this purpose.
Example
// Store in `name` in the current tab
sessionStorage.setItem('name', 'Some Name');
// Get `name` from sessionStorage
var name = sessionStorage.getItem('name');
alert(name); // Some Name
According to MDN :
The sessionStorage property allows you to access a session Storage object for the current origin. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With