基本的谷歌浏览器扩展消息传递不起作用 [英] Basic google chrome extension message passing not working
问题描述
我一直试图让消息传递的chrome扩展工作,但是console.log(response)给了我未定义。我已经看过消息传递和在计算器上的文档,但在我的情况下,它不会工作。
myscript.js:
chrome.extension.sendRequest({greeting:hello},function(response){
console.log(response);
return true;
});
background.js:
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
sendResponse({farewell:goodbye});
return true;
});
manifest.json
{
manifest_version:2,
name:nameasf,
description:asfasaf,
版本:1.0,
权限:[
标签,
http://somedomain.com/
],
content_scripts:[
{
matches:[http://somedomain.com/*],
css:[mystyles。 css],
js:[jquery.js,myscript.js],
all_frames:true
}
],
background:{
scripts:[background.js]
},
browser_action:{
default_icon :icon.png,
default_popup:popup.html
}
}
您正在使用过时的 chrome.extension.sendRequest
。
使用 改为使用chrome.runtime.sendMessage
。
(注意:您已使用新的 chrome.runtime.onMessage
,因此您不必在侦听器端更改任何内容。)
一些附注: ul> I've been trying to get message passing for a chrome extension to work but console.log(response) gives me undefined. I've looked at the docs for message passing and on stackoverflow, but in my case it won't work. I am at a loss as to why. myscript.js: background.js: manifest.json
You are using the obsolete (Note: You are already using the new A couple of sidenotes: 这篇关于基本的谷歌浏览器扩展消息传递不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
选项卡
权限)。
chrome.extension.sendRequest({greeting: "hello"}, function(response) {
console.log(response);
return true;
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
return true;
});
{
"manifest_version": 2,
"name": "nameasf",
"description": "asfasaf",
"version": "1.0",
"permissions": [
"tabs",
"http://somedomain.com/"
],
"content_scripts": [
{
"matches": ["http://somedomain.com/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"],
"all_frames": true
}
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
chrome.extension.sendRequest
.
Use chrome.runtime.sendMessage
instead. chrome.runtime.onMessage
so you don't have to change anything on the listener side.)
tabs
permission).