如何使用Google Chrome存储API保存字符串值并将其打印在其他页面上

问题描述 投票:0回答:1

我希望用户输入,然后使用谷歌浏览器存储API保存该用户输入,但我找不到任何有用的资源。

我有2个元素,我希望使用JS永久保存:

 var title = encodeURIComponent(document.getElementById('title').value);
var url = encodeURIComponent(document.getElementById('url').value);

我想创建一个2个数组,一个保存标题值,一个保存url值,然后我想在另一个页面上打印这些值。

javascript google-chrome google-chrome-extension google-api google-chrome-devtools
1个回答
0
投票

您可以使用chrome.storage.local对象来完成此操作。

如果您需要在chrome存储中存储数据,请使用以下句子:

chrome.storage.local.set({'titles': [title]});
chrome.storage.local.set({'urls': [url]});

现在,如果要检索chrome存储中的当前数据,请使用以下命令:

let newTile = "new title";
chrome.storage.local.get('titles', function(titles){
    titles.push(newTile);
    chrome.storage.local.set({'titles': titles});
});

let newUrl = "http://.....";
chrome.storage.local.get('urls', function(urls){
    urls.push(newUrl);
    chrome.storage.local.set({'urls': urls});
});

重要提示:此处缺少的是检查本地存储中当前数据的条件。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.