簡體   English   中英

是否可以將來自popup.html的輸入數據保存在localStorage中?

[英]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.storagewindow.localStorage之間的區別在於,您可以在每個最近的瀏覽器上使用window.localStorage 也在網站上。 存儲的對象將一直存儲到用戶關閉瀏覽器為止。

您正在構建Chrome擴展程序,因此,我建議您使用為此而設計的API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM