Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalStorage returning null in a different tab in chrome

This is my issue:

I update the localStorage in popup.js in a new tab. I access the same localStorage(same key) in the background.js.

Now this is returning null in every tab apart from the chrome://extensions tab(when I load the extensions.)

I thought localStorage was persistant across all tabs.

Code:

popup.js:

$(document).ready(function (){

    alert(localStorage.getItem('filters'));
    var oldFilters = localStorage.getItem('filters');
    //All the filters show up on the popup.html page.
    document.getElementById('td1').innerHTML = oldFilters;

    var dat = oldFilters + "," + newArray[j]
    localStorage.setItem('filters',String(dat));
}

background.js:

$(window).ready(function() {
  // Handler for .ready() called.

 var filters = localStorage.getItem('filters');

   alert("background + "+ filters);
    //This shows all the filters in the chrome:extensions page but always pops up "background + null" in every new tab load. 

//changeImage(filters);

});
like image 983
Hick Avatar asked Dec 23 '12 04:12

Hick


People also ask

Does localStorage work across tabs?

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.

Does localStorage clear on tab close?

( localStorage data for a document loaded in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.)

How do I change local storage in Chrome?

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. In Chrome version 65, you can manually modify and add new items.

Can I use localStorage in incognito mode?

Local Storage data stored on normal browsing sessions will not be available when you open a browser in private browsing or in Incognito mode. Local Storage data will not get cleared even if you close the browser.


1 Answers

Background and Browser Action(In your case) Pages live in isolated worlds, their local storage details are not accessible to each other, if you want this sort of access to happen use chrome.storage for your storage needs.

It has few advantages

  • Your extension's content scripts can directly access user data without the need for a background page.
  • A user's extension settings can be persisted even when using split incognito behavior.
  • User data can be stored as objects (the localStorage API stores data in strings).

Methods used

  • chrome.storage.local.get
  • chrome.storage.local.set
    (use sync instead of local if the data needs to be synchronized with Google Sync)

Demonstration

manifest.json

Ensure all permissions are available for accessing storage API.

{
"name":"Local Storage Demo",
"description":"This is a small use case for using local storage",
"version":"1",
"manifest_version":2,
"background":{
    "scripts":["background.js"]
},
"browser_action":{
    "default_popup":"popup.html",
    "default_icon":"logo.png"
},
"permissions":["storage"]
}

popup.html

A trivial popup html page which refers popup.js to surpass CSP.

<!doctype html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>

background.js

This scripts sets content to chrome storage

//Set some content from background page
chrome.storage.local.set({"identifier":"Some awesome Content"},function (){
    console.log("Storage Succesful");
});
//get all contents of chrome storage
chrome.storage.local.get(null,function (obj){
        console.log(JSON.stringify(obj));
});

popup.js

This script retrieves and sets content from\to chrome storage

document.addEventListener("DOMContentLoaded",function (){
    //Fetch all contents
    chrome.storage.local.get(null,function (obj){
        console.log(JSON.stringify(obj));
    });
    //Set some content from browser action
    chrome.storage.local.set({"anotherIdentifier":"Another awesome Content"},function (){
        console.log("Storage Succesful");
    });
});

If you look at outputs of these js pages, communication of storage (Background -> popup and popup -> background) is achieved.

like image 103
Sudarshan Avatar answered Nov 15 '22 21:11

Sudarshan