2017-09-01 116 views
1

您可以使用browser.storage.local.set存儲數組,還是使用不同的方法實現相同的結果?Firefox WebExtension,將數組存儲在瀏覽器的存儲器中

詳情:

我的分機當前會重定向通過options.html形式指定的網站。目前,當您指定新網站時,舊網站將被替換。有沒有一種方法可以追加到一組將重定向而不是替換網站的網站?

options.js:(將在options.html從形式處理信息)

function saveOptions(e) { 
    e.preventDefault(); 
    browser.storage.local.set({ 
     url: document.querySelector("#url").value 
    }); 
} 
function restoreOptions() { 
    function setCurrentChoice(result) { 
     document.querySelector("#url").value = result.url || "reddit.com"; 
    } 
    function onError(error) { 
     console.log(`Error: ${error}`); 
    } 
    var getting = browser.storage.local.get("url"); 
    getting.then(setCurrentChoice, onError); 
} 
document.addEventListener("DOMContentLoaded", restoreOptions); 
document.querySelector("form").addEventListener("submit", saveOptions); 

redirect.js:

function onError(error) { 
    console.log(`Error: ${error}`); 
} 
function onGot(item) { 
    var url = "reddit.com"; 
    if (item.url) { 
     url = item.url; 
    } 
    var host = window.location.hostname; 
    if ((host == url) || (host == ("www." + url))) { 
     window.location = chrome.runtime.getURL("redirect/redirect.html"); 
    } 
} 
var getting = browser.storage.local.get("url"); 
getting.then(onGot, onError); 

我想過是每個URL添加存儲位置,但是i也必須被存儲以防止每次加載options.js時它被重置。 (有什麼類似於下面的代碼)

var i = 0; 
browser.storage.local.set({ 
    url[i]: document.querySelector("#url").value 
}); 
i++; 

甲多個邏輯的解決辦法是爲url存儲位置是一個數組。

如果沒有爲url一種方式是一個數組,然後將redirect.html可能包含以下內容:

if ((url.includes (host)) || (url.includes ("www." + host))){ 
    window.location = chrome.runtime.getURL("redirect.html"); 
} 
+1

是。你有沒有試過存儲數組? –

+1

[用chrome.storage.local存儲數組]可能重複(https://stackoverflow.com/questions/16605706/store-an-array-with-chrome-storage-local) –

+0

爲什麼你認爲你可能不會能夠存儲數組?認真。我希望能夠更改文檔,以便其他人不會產生混淆。我試圖找到可以改進文檔的位置,以防止其他人獲得這種印象。 – Makyen

回答

0

新鮮的眼光已經解決了我的問題。

在options.js:

function saveOptions(e) { 
    e.preventDefault(); 
    var array = (document.querySelector("#url").value).split(","); 
    browser.storage.local.set({ 
     url: array 
    }); 

在redirect.js:

function onGot(item) { 
    var url = ""; 
    if (item.url) { 
     url = item.url; 
    } 
    var host = window.location.hostname; 
    if ((url.includes(host)) || (url.includes("www." + host))) { 
     window.location = chrome.runtime.getURL("redirect/redirect.html"); 
    } 
}