0
因此,我試圖將數據從本地存儲的鉻背景頁面發送到內容腳本,然後對數據進行一些操作。之後,我想將其發送回後臺頁面,並更新本地存儲的後臺頁面。這可能嗎。我知道如何將數據從後臺發送到內容腳本,但是如何從內容腳本發送到後臺?chrome消息從背景到內容腳本再到背景
background.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);
}
});
的script.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"}');
});