6

I am building a Chrome Extension. I was wondering if there is a way to remember js variables for a tab even if the page changes. For eg. If I am on example1.com and go to example2.com on the same tab, I should retain the variables which were set on example1.com. I don't want to use Chrome Storage.

I can't use localStorage or sessionStorage because chrome has different storages for different domains. What are the ways by which this can be accomplished ?

  • Use a cookie storing stringified JSON, which has the URL of the page as its key. – Rory McCrossan Jun 21 '14 at 14:46
  • @RoryMcCrossan I guess I can't access cookies which were set on example1.com, from example2.com. –  Jun 21 '14 at 14:49
  • **chrome has different storages for different domains. What are the ways by which this can be accomplished** Answer is no, local storage for your extension is isolated regardless of any domain – Mr. Alien Jun 21 '14 at 14:50
  • @Mr.Alien I am talking about localStorage, and as far as I know localStorage is different for different domain names. I just rechecked it. –  Jun 21 '14 at 14:56
  • @user3763053 I've developed extensions, and I've used localstorage as well, look for chrome.storage and you will get my point – Mr. Alien Jun 21 '14 at 14:56
  • @Mr.Alien I am not using chrome storage APIs (chrome.storage.sync...), I am using localStorage which is present in the resources section. –  Jun 22 '14 at 05:38

3 Answers3

4

Apart from using localStorage( through background page), you can directly save those variables in the background page by messaging from content script to the background page. If those variables are in the extension page(other that content or injected scripts) you can easily save them by chrome.extension.getBackgroundPage().window.variableYouWant method and access those variables in the content script itself by messaging.
If those variables are in the content script use messaging to send those variables to background page like this:

chrome.runtime.sendMessage(object);

object could be any Object you might want to send.

on background page have a lisner like this:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    myVariabeInBackground = message.yourObjectProperties;
});

when you require those variables you can use chrome.extension.getBackgroundPage().window.myVariabeInBackground in your extension page(other than content script) or you can send message from background page to content script by calling chrome.tabs.sendMessage(tabId, messageObject) in the background page itself and receive at the content script by having a listner like this:

chrome.runtime.onMessage.addListener(
  function(request, sender) {}); //request is your sent object.

For localStorage also you can do the same thing to get that stored variable into your content script.
And No, you cant directly access the background page from content script.

PS: variables in background page will reset if extension is reloaded. If you want to use those variables even after reloading the extension use local storage. Extension does not reload on browser actions.

For more details read https://developer.chrome.com/extensions/messaging

krg265
  • 383
  • 1
  • 10
0

use localstorage. the extension is completely isolated from the page in function and variable storage. you will need to use localstorage in a background page, so when you change tabs, the variable is never lost.

Marc Guiselin
  • 3,442
  • 2
  • 25
  • 37
  • I am using content scripts. Are you saying that I store my variables in the background page of my extension ? Is it allowed for the content script to access extension's background pages ? –  Jun 22 '14 at 05:43
0

You should look into chrome.storage API as an alternative to localStorage+Messaging.

It is asynchronous compared to localStorage, but so is Messaging, and Messaging just for variable storage leads to a lot of boilerplate.

See documentation here, and a recent overview (by me) of pros/cons here.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206