使用chrome.storage,如何将字符串保存到本地存储,然后测试是否存在?
With chrome.storage, how to save a string into local storage and then test for existence?
我正在编写一个 chrome 扩展,我想在其中将一个字符串(一个 URL)保存到本地阶段并稍后测试是否存在。
例如:
function getURL(e) {
let domain = e.url;
//if domain does not exist in local storage, save domain into local storage
}
我看了 chrome.storage.local.get()
和 set()
的教程,但我仍然想不出一个简单的方法来做到这一点。
保存在数组中,然后测试。
function getURL(e) {
let domain = e.url;
//if domain does not exist in local storage, save domain into local storage
// get the saved `urls`
chrome.storage.local.get("urls", i => {
// urls might not saved before
let urls = i.urls || []
// test if exists
if (!urls.includes(domain)) {
urls.push(domain)
//save it into store
chrome.storage.local.set({urls})
}
})
}
我正在编写一个 chrome 扩展,我想在其中将一个字符串(一个 URL)保存到本地阶段并稍后测试是否存在。
例如:
function getURL(e) {
let domain = e.url;
//if domain does not exist in local storage, save domain into local storage
}
我看了 chrome.storage.local.get()
和 set()
的教程,但我仍然想不出一个简单的方法来做到这一点。
保存在数组中,然后测试。
function getURL(e) {
let domain = e.url;
//if domain does not exist in local storage, save domain into local storage
// get the saved `urls`
chrome.storage.local.get("urls", i => {
// urls might not saved before
let urls = i.urls || []
// test if exists
if (!urls.includes(domain)) {
urls.push(domain)
//save it into store
chrome.storage.local.set({urls})
}
})
}