Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser tab storage?

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.

like image 837
Ray Arifin Avatar asked Jun 21 '17 05:06

Ray Arifin


People also ask

Is local storage per tab?

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.

How do I access my browser storage?

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.

How do I reduce my browser storage?

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".


1 Answers

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

like image 178
Mohamed Abbas Avatar answered Nov 07 '22 12:11

Mohamed Abbas