Javascript 将信息从网站传递到chrome扩展
我正在尝试将数据从网站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);}
$("#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