0

I want to save some value (say userid) from my extension and then want to access it on other pages. I cannot do it using cookie because I can access cookie only within the extension.

Is there a way to do that?

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
  • In essence the same problem ("Access extension info from page") and answer ("Modify the page's DOM") as [Check whether user has my chrome extension installed](http://stackoverflow.com/questions/6293498/check-whether-user-has-my-chrome-extension-installed) – Rob W Sep 05 '12 at 16:23
  • Are you wanting a global variable saved, so that the extension can access it on different sites? Or are you wanting to access the variable from the page that the extension is running on? – Gravitate Sep 05 '12 at 16:59
  • @Gravitate Yes, I want a global variable thing. That I can access on any site. – Adil Malik Sep 05 '12 at 17:04
  • @RobW The thread you are referring to wants to read the extension variable from their own page. I want to read the extension variable from a page developed by a Third Party. e.g. user logs in to my extension. Now when they load imdb.com I should know which user is visiting imdb.com so I can log their visited data in my DB. I hope the scenario is clear now. – Adil Malik Sep 05 '12 at 17:08
  • So, you want to read a global variable in a page? See [Chrome extension - retrieving Gmail's original message](http://stackoverflow.com/a/9636008/938089). – Rob W Sep 05 '12 at 17:19

1 Answers1

3

I am sorry, it might just be me being thick but I still don't really understand what you are trying to do.

If you are wanting to create a variable that persists across different domains, you need to use chrome.storage. It is similar to HTML5 localStorage but with some significant improvements (persisting across domains being a main one for me). You can read more about setting and getting the values here:

http://developer.chrome.com/extensions/storage.html

If this isn't what you need, please try to give a specific example of what you are trying to do and I will try again.

EDIT: Please see below for examples.

You can set a variable into storage like this (the function is an optional param):

var storage = chrome.storage.local;
var myVal="foo";

storage.set({'myVar': myVal}, function() {
    alert('myVar has been saved in local storage');
});

You can retrieve it like this (function is NOT optional):

storage.get('myVar', function(items) {
    if (items.myVar) {
        alert(items.myVar);
    }
});

You can return default values when 'myVar' is undefined by storage.get({'myVar': ''}) - this saves checking with the "if (items.myVar) {" line.

EDIT: Please see below for issues to watch out for.

Firstly, the methods are asynchronous. So...

alert("1");
storage.get('myVar', function(items) {
    alert("2");
});
alert("3");

will probably output 1, then 3, then 2. Not a big deal but saves a lot of restructuring later if you know beforehand.

Secondly, please note that there is a limit to the amount of storage space you have. For chrome.storage.sync (for which the variable are automatically synced across all of that users browsers) the storage space is very limited at about 100kb. (there are other limits as well, such as a max of 512 separate variables - see the documentation link above). Basically, chrome.storage.sync is for a few small variables only.

chrome.storage.local has much more storage space at about 5mb (unless you set the unlimited storage permission). Unlike chrome.storage.sync there is no other limits on storage chrome.storage.local.

If a set will take you over the storage limit, then it fails.

Thirdly, using a variable as your variable name does NOT work:

var myVal="foo";
var myVarName='myVar';

storage.set({myVarName: myVal}, function() {
    alert('THIS WILL NOT WORK');
});

If like me, you really wanted/needed to do something like this, the way to do something like this is by creating an object and assigning it the variables as properties and then putting the object in storage. For example:

var myVarName='myVar';
var mySecondVarName='mySecondVar';

var myVal="foo";
var mySecondVal="bar";

var obj = {};
obj[myVarName] = myVal;
obj[mySecondVarName] = mySecondVal;
//add as many vars as you want to save to the storage 
//(as long as you do not exceed the max storage space as mentioned above.)

storage.local.set(obj);

you can then get the values with:

chrome.storage.local.get(myVarName, function (items) {
    if (items[myVarName]) {
        alert('You got the value that was set to myVarName');
    }
});

Or if you wanted the value for mySecondVarName:

chrome.storage.local.get(mySecondVarName, function (items) {
    if (items[mySecondVarName]) {
        alert('You got the value that was set to mySecondVarName');
    }
});

And these can be nested.

Gravitate
  • 2,885
  • 2
  • 21
  • 37
  • Yes, That seems to be what I am looking for. Let me try it. I'll inform you if it works or if I have any problems. I appreciate your help. – Adil Malik Sep 05 '12 at 21:48
  • In that case, I will expand on my answer with examples and problems that I had with it as soon as I get time to sit down and write it out... probably tonight. – Gravitate Sep 06 '12 at 06:16
  • Managed to add an example but will add some problems to watch out for that might cause it not to work later today... – Gravitate Sep 06 '12 at 06:32