简体   繁体   English

是否可以将来自popup.html的输入数据保存在localStorage中?

[英]Can I save input data from popup.html in localStorage?

I am working on pretty simple extension. 我正在研究非常简单的扩展。 My popup.html has some radiobuttons, which represent set of predefined options for user. 我的popup.html有一些单选按钮,代表用户的预定义选项集。 The problem is that logic of my content-script depends upon those options. 问题是我的内容脚本的逻辑取决于这些选项。

So, my question is: let us assume I would attach a script to 'popup.html' (meeting Chrome CSP) that listens to any change in my radiobuttons selection and as soon as it happens does someting like .localstorage.setItem(key, value)? 因此,我的问题是: 让我们假设我将一个脚本附加到“ popup.html”(与Chrome CSP开会)上,该脚本侦听我的单选按钮选择中的任何更改,并且一旦发生,便会执行诸如.localstorage.setItem(key,值)? Would my content-script be able to get this value from local storage at appropriate moment? 我的内容脚本是否可以在适当的时候从本地存储中获取此值?

Naturally, you can do that. 当然,您可以这样做。 Here two examples: 这里有两个例子:

chrome.storage.local.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

chrome.storage.local.get('value', function(obj) {
    //Notify that we get the value.
    message('Value is ' + obj.value);
});

Or you can use the sync storage: 或者,您可以使用同步存储:

chrome.storage.sync.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

chrome.storage.sync.get('value', function(obj) {
    //Notify that we get the value.
    message('Value is ' + obj.value);
});

Here is the documentation . 这是文档

The difference between chrome.storage and window.localStorage , is that you can use window.localStorage on every recent browser. chrome.storagewindow.localStorage之间的区别在于,您可以在每个最近的浏览器上使用window.localStorage Also on websites. 也在网站上。 The stored objects will be stored until the user close the browser. 存储的对象将一直存储到用户关闭浏览器为止。

You are building a Chrome extension, so I would recommend you to use the API that was make for that. 您正在构建Chrome扩展程序,因此,我建议您使用为此而设计的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM