Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension chrome扩展编程中的消息传递_Google Chrome Extension - Fatal编程技术网

Google chrome extension chrome扩展编程中的消息传递

Google chrome extension chrome扩展编程中的消息传递,google-chrome-extension,Google Chrome Extension,contenscript.js可以使用与option.js通信吗 chrome.extension.sendRequest() 及 当然,这就是重点。看 下面是一个示例,如果您需要: 发送: 接收: 试试这个 在内容脚本中: 在扩展脚本中: 那么为什么我在1的第2行中没有定义。sendRequest({method:“getStatus”},函数(response){2.console.log(response.status);})虽然我已经在option.jsOk中定义了chrome.ex

contenscript.js可以使用与option.js通信吗

chrome.extension.sendRequest()


当然,这就是重点。看

下面是一个示例,如果您需要:

发送:

接收:

试试这个

在内容脚本中:

在扩展脚本中:


那么为什么我在
1的第2行中没有定义。sendRequest({method:“getStatus”},函数(response){2.console.log(response.status);})虽然我已经在option.jsOk中定义了
chrome.extension.onRequest.addListener()
,这很好,但是我如何将整个本地存储作为响应传递给contentscript呢?您不能。但是你可以直接在contentscript中访问本地存储。我们他不能吗?本地存储只是一个简单的JSON。只需发送
{localStorage:JSON.stringify(localStorage)}
chrome.extension.onRequest.addListener()
chrome.extension.sendRequest({method: "getStatus"}, function(response) {
    console.log(response.status);
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.method === "getStatus") sendResponse({status: "status"});
});