在其他页面上访问扩展数据 [英] Access extension data on other pages

查看:101
本文介绍了在其他页面上访问扩展数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的扩展中保存一些值(比如userid),然后想在其他页面上访问它。我无法使用cookie,因为我只能在扩展名内访问cookie。



有没有办法做到这一点?

解决方案

我很抱歉,这可能只是我很厚,但我还是不明白你想要做什么。



如果您想创建一个持续存在于不同域的变量,则需要使用chrome.storage。它与HTML5 localStorage类似,但有一些显着的改进(坚持跨域作为我的主要域)。你可以阅读更多关于设置和获取值:



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



如果这不是您所需要的,请尝试举一个你想要做什么的具体例子,我会再试一次。



编辑:请看下面的例子。

你可以像这样设置一个变量是一个可选的参数):

  var storage = chrome.storage.local; 
var myVal =foo;
$ b $ storage.set({'myVar':myVal},function(){
alert('myVar已经保存在本地存储中');
});

您可以像这样检索它(函数不是可选的):

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

当'myVar'未被storage.get定义时,您可以返回默认值({'myVar': ''}) - 这节省了检查与if(items.myVar){线。



编辑:请参阅下面的问题要注意。 p>

首先,这些方法是异步的。所以......

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

可能会输出1,然后是3,然后是2.没有什么大不了,但可以节省很多重组以后如果你事先知道的话。



其次,
请注意,你拥有的存储空间是有限制的。
对于chrome.storage.sync(变量在所有用户浏览器中自动同步),存储空间非常有限,大约为100kb。 (还有其他限制,例如最多512个独立变量 - 请参阅上面的文档链接)。基本上,chrome.storage.sync仅适用于一些小变量。



chrome.storage.local拥有大约5mb的存储空间(除非您设置无限存储空间允许)。与chrome.storage.sync不同,对存储chrome.storage.local没有其他限制。



如果一组将使您超过存储限制,则它将失败。 / p>

第三,使用变量作为变量名不起作用:

  var myVal =foo; 
var myVarName ='myVar';
$ b storage.set({myVarName:myVal},function(){
alert('THIS will not');
});

如果像我一样,您真的想要/需要做这样的事情,这是通过创建一个对象并将其作为属性分配给变量,然后将该对象存储。例如:

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

var myVal =foo;
var mySecondVal =bar;

var obj = {};
obj [myVarName] = myVal;
obj [mySecondVarName] = mySecondVal;
//添加您希望保存到存储
的许多变量(只要您没有超出上述的最大存储空间)。

storage .local.set(OBJ);

您可以通过以下方式获取值:

  chrome.storage.local.get(myVarName,function(items){
if(items [myVarName]){
alert('You got the value that被设置为myVarName');
}
});

或者如果您需要mySecondVarName的值:

  chrome.storage.local.get(mySecondVarName,function(items){
if(items [mySecondVarName]){
alert('You got the value这是设置为mySecondVarName');
}
});

这些可以嵌套。


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?

解决方案

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.

这篇关于在其他页面上访问扩展数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆