Google Chrome浏览器扩展程序中每个标签的本地存储变量范围 [英] local storage variable scope per tab in google chrome extensions

查看:158
本文介绍了Google Chrome浏览器扩展程序中每个标签的本地存储变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储每个标签范围的变量。同样的问题已经提出,建议使用localStorage。但是如何使用localstorage来保存关联数组。



例如。用户正在登录网站。我想创建一个变量来引用该用户。像这样使用会打开很多标签,他会登录很多网站。所以我需要在localstorage中维护域名和用户引用。所以如果我使用用户名作为唯一的字段,那么我可以维护与用户名的关联,最后我需要将这些信息存储在数据库中。



所以如何在Google Chrome扩展中维护数据结构。以上只是一个例子,但它可能是任何情况下,我们需要不同值的不同值的变量名称。



该文档声明使用json格式。但是,我仍然希望用户对此提出建议。



所以任何形式的回复都会对答案,建议,引用和评论有帮助。

解决方案

从你的问题来看,选项卡范围是什么意思并不是很清楚。

如果您确实需要选项卡作用域,即使用户导航到新站点时也是该特定选项卡的存储值,那么您可以将tabid作为关键字(仅限于在浏览器重新启动之间,选项卡ID不是持久的)。

如果您确实是指域作用域 - 那么它就是免费的内容脚本。如果您在内容脚本中使用 localStorage ,它将仅存储此域的值。您只需确保您的密钥不会与潜在网站的密钥相冲突,因为该网站可能使用相同的localStorage。



如果您只是询问如何创建数据结构将域名作为关键字,那么它可能是这样的:

  var data = {}; 
data [google.com] = {
用户名:user1,
密码:pass1
};
data [yahoo.com] = {
用户名:user2,
密码:pass2
};

//存储
localStorage [data] = JSON.stringify(data);

// load
data = JSON.parse(localStorage [data]);

//读取google用户名
console.log(data [google.com]。username);


I wanted to store variables per tab scope. This same question was raised already and the suggestion was to use localStorage. But how to use localstorage to save associative arrays.

For example. A user is logging in a site. i want to create a variable to refer that user. like this the use will open many tabs and he will log in many sites. so i need to maintain the domain name and the user reference in localstorage. so if i use the username as a unique field then i can maintain an association with reference to the username and finally i will need to store these information in the database.

So how to maintain a data structure in google chrome extension. The above is just an example but it could be any case where we need the same variable name in various tabs with different values.

The documentation states to use json format. But still i want the users suggestion on this.

so any kind of response will he helpful like answers, suggestions, references and comments.

解决方案

It is not very clear from your question what do you mean by "tab scope".

If you really need the tab scope, that is store values for this specific tab even when user navigates to a new site, then you can take tabid as a key (only tab id is not persistent between browser restarts).

If you actually mean "domain scope" - then it comes "for free" with content scripts. If you use localStorage inside a content script it would store the value only for this domain. You only need to make sure your keys don't collide with potential site's keys as the site might use the same localStorage.

If you are just asking how to create a data structure that would have domain names as keys, then it could be something like this:

var data = {};
data["google.com"] = {
    username: "user1",
    password: "pass1"
};
data["yahoo.com"] = {
    username: "user2",
    password: "pass2"
};

//store it
localStorage["data"] = JSON.stringify(data);

//load
data = JSON.parse(localStorage["data"]);

//read google username
console.log(data["google.com"].username);

这篇关于Google Chrome浏览器扩展程序中每个标签的本地存储变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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