所以我试图将数据从 chrome 背景页面的本地存储发送到内容脚本,然后对数据进行一些操作。之后,我想将其发送回后台页面并更新后台页面的本地存储。这可能吗。我知道如何将数据从后台发送到内容脚本,但是如何从内容脚本发送到后台?
背景.html
var background = chrome.extension.getBackgroundPage();
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse, getBackResponse) {
if (request.extensionSettings === "storage") {
// send local storage data {"one": "data", "two": "data"}
sendResponse({storageString: background.localStorage.extdata});
// save new data from content script
localStorage.setItem("extdata", getBackResponse);
}
});
脚本.js
chrome.runtime.sendMessage({extensionSettings: "storage"}, function(response) {
var json = JSON.parse(response.storageString);
console.log(json);
// take json object do a few things with data
// take data and make new json string, and send it background page
sendBack('{"one": "new data", "two": "more new data"}');
});