基本的谷歌chrome扩展消息传递不起作用
Basic google chrome extension message passing not working
我一直试图让chrome扩展的消息传递正常工作,但console.log(response)给了我未定义的消息。我已经查看了文档中的消息传递和stackoverflow,但在我的情况下,它不起作用。我不知道为什么。
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",
"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
请改用chrome.runtime.sendMessage
。
(注意:您已经在使用新的chrome.runtime.onMessage
,因此您不必更改侦听器端的任何内容。)
几个旁注:
-
对于这里提供的代码,您不需要任何权限(尤其是不需要强大的
tabs
权限) - 只要可能,最好使用非持久性后台页面(也称为事件页面),因为它们对资源更友好
相关文章:
- 基本的谷歌chrome扩展消息传递不起作用
- Chrome 扩展消息传递体系结构
- Chrome扩展消息传递:未发送响应
- Chrome 消息传递.未捕获错误:连接到扩展时出错
- 将消息从 Chrome 扩展程序传递到网页
- Chrome 扩展程序:无法让消息传递在后台脚本和内容脚本之间正常工作
- 消息传递在chrome扩展中不起作用
- 如何动态地将chrome扩展ID发送到网页以进行消息传递
- Chrome扩展消息传递-如何传递HTML
- 面板&在Firefox扩展中传递的PageMod内容脚本消息
- Chrome扩展通过消息传递与网页进行通信
- Chrome扩展消息在内容和后台之间传递
- 内容脚本和背景页面之间的消息传递在chrome扩展是不工作,因为它应该是
- Chrome扩展消息传递与localStorage,值是未定义
- 实现跨扩展消息传递在chrome扩展和应用程序
- 谷歌Chrome扩展消息传递与run_at document_start
- 消息传递在chrome扩展不工作
- 在页与扩展之间传递消息
- 在javascript chrome扩展中传递json消息通知
- Chrome扩展消息传递-代码包括