如何从内容脚本访问chrome扩展弹出窗口localStorage中的变量? [英] How do I reach variables in a chrome extension popup localStorage from content scripts?

查看:96
本文介绍了如何从内容脚本访问chrome扩展弹出窗口localStorage中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在chrome扩展程序中存储了一些选项.使用localStorage.setItem().

I have stored some options in my chrome extension. Using localStorage.setItem().

但是当我想在页面上使用它们时,它不起作用.我知道这是因为它们完全位于不同的域中,但是如何从其他任何域(在我的页面脚本中)访问chrome扩展域中的设置.

But when I want to use them on a page, it's not working. I know it's because they are totally in different domains, but how can I reach the settings in my chrome extension domain from any other domain (in my page scripts).

谢谢

推荐答案

您可以使用chrome.storage API.

要在本地保存内容:

chrome.storage.sync.set({"key":"value"});

要检索值:

chrome.storage.sync.get("key",function(res) {
console.log(res[key])
});

如果您不希望存储跨用户帐户同步,则可以使用chrome.storage.local代替chrome.storage.sync

If you don't want the storage to sync across the user's account, you can use chrome.storage.local instead of chrome.storage.sync

文档

这篇关于如何从内容脚本访问chrome扩展弹出窗口localStorage中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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