Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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
Javascript 将信息从网站传递到chrome扩展_Javascript_Google Chrome Extension - Fatal编程技术网

Javascript 将信息从网站传递到chrome扩展

Javascript 将信息从网站传递到chrome扩展,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在尝试将数据从网站javascript发送到我的chrome扩展。 我读了一些关于它的文章,但我无法让它发挥作用 在我的网站上: $("#button-id").click(function(){ chrome.runtime.sendMessage("msg", {arg: "1"}, function(response){alert("got response: " + response);}

我正在尝试将数据从网站javascript发送到我的chrome扩展。 我读了一些关于它的文章,但我无法让它发挥作用

在我的网站上:

$("#button-id").click(function(){
    chrome.runtime.sendMessage("msg", {arg: "1"},
    function(response){alert("got response: " + response);});
});
和background.js(在chrome扩展中)

我还向清单中添加了:

   "externally_connectable": {
        "matches": ["*://localhost/project/public/*","http://localhost/project/public/*","*://*/*/*"]
    },
当我点击这个按钮时,我得到的只是一个警告,上面写着“GetResponse undefined”

我尝试了一些可能有助于解决问题的方法: 当我在我的网站上,打开chrome开发者控制台,输入“chrome.runtime.sendMessage”(“jjj”) 我变得“未定义”


提前感谢

要发送响应,需要将其传递给侦听器的
sendResponse
参数,这是第三个参数:

chrome.runtime.onMessageExternal.addListener(
  function(message, sender, sendResponse) {
    alert("in background");
    sendResponse("1");
  }
);
需要注意的一点是:如果要异步调用
sendResponse
,则必须
返回true



此外,从网页发送消息时,必须提供扩展名的id作为第一个参数。在示例代码中,它是
“msg”
:它必须是扩展id。

要发送响应,需要将其传递给侦听器的
sendResponse
参数,这是第三个参数:

chrome.runtime.onMessageExternal.addListener(
  function(message, sender, sendResponse) {
    alert("in background");
    sendResponse("1");
  }
);
需要注意的一点是:如果要异步调用
sendResponse
,则必须
返回true



此外,从网页发送消息时,必须提供扩展名的id作为第一个参数。在示例代码中,它是
“msg”
:它必须是扩展id。

要发送响应,需要将其传递给侦听器的
sendResponse
参数,这是第三个参数:

chrome.runtime.onMessageExternal.addListener(
  function(message, sender, sendResponse) {
    alert("in background");
    sendResponse("1");
  }
);
需要注意的一点是:如果要异步调用
sendResponse
,则必须
返回true



此外,从网页发送消息时,必须提供扩展名的id作为第一个参数。在示例代码中,它是
“msg”
:它必须是扩展id。

要发送响应,需要将其传递给侦听器的
sendResponse
参数,这是第三个参数:

chrome.runtime.onMessageExternal.addListener(
  function(message, sender, sendResponse) {
    alert("in background");
    sendResponse("1");
  }
);
需要注意的一点是:如果要异步调用
sendResponse
,则必须
返回true


此外,从网页发送消息时,必须提供扩展名的id作为第一个参数。在示例代码中,它是
“msg”
:它必须是扩展id