![](/img/trans.png)
[英]How can I save data from background.js to local machine and fetch this data when required by a button on the popup.html page?
[英]Can I save input data from popup.html in localStorage?
我正在研究非常簡單的擴展。 我的popup.html有一些單選按鈕,代表用戶的預定義選項集。 問題是我的內容腳本的邏輯取決於這些選項。
因此,我的問題是: 讓我們假設我將一個腳本附加到“ popup.html”(與Chrome CSP開會)上,該腳本偵聽我的單選按鈕選擇中的任何更改,並且一旦發生,便會執行諸如.localstorage.setItem(key,值)? 我的內容腳本是否可以在適當的時候從本地存儲中獲取此值?
當然,您可以這樣做。 這里有兩個例子:
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);
});
或者,您可以使用同步存儲:
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);
});
這是文檔 。
chrome.storage
和window.localStorage
之間的區別在於,您可以在每個最近的瀏覽器上使用window.localStorage
。 也在網站上。 存儲的對象將一直存儲到用戶關閉瀏覽器為止。
您正在構建Chrome擴展程序,因此,我建議您使用為此而設計的API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.