【发布时间】:2015-07-02 18:58:46
【问题描述】:
script/contentscript.js
'use strict';
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
alert("hi");
return true;
});
chrome.runtime.sendMessage(null, {hello:"hello"});
我很茫然。 addListener 没有注册回调,发送消息时没有触发回调,或者介于两者之间。我已经尽我所能来限制可能的错误。有一次,sendMessage 在我的popup.js 中,经过数小时尝试不同的变体后,我将它移植到另一台计算机上。当我绝望到将sendMessage 放在同一个文件中时,我惊讶地发现sendMessage 即使在同一个内容脚本中也不起作用!
我使用了运行时和选项卡对象,以及sendMessage 和onMessage.addListener 的每个变体。
一件奇怪的事情是,当在内容脚本中的断点处,我看到hasListener 通常返回 false,hasListeners 在添加侦听器后返回 true。
{
"name": "__MSG_appName__",
"version": "0.0.1",
"manifest_version": 2,
"description": "__MSG_appDescription__",
"icons": {
"16": "images/icon-16.png",
"128": "images/icon-128.png"
},
"default_locale": "en",
"content_scripts":[
{
"matches": ["http://*/*", "https://*/*"],
"js":["scripts/contentscript.js"]
}
],
"browser_action": {
"default_icon": {
"19": "images/icon-19.png",
"38": "images/icon-38.png"
},
"default_title": "pluralsight dl",
"default_popup": "popup.html"
},
"options_page": "options.html",
"permissions":[
"activeTab",
"tabs",
"http://*/*",
"https://*/*"
]
}
关于为什么这不起作用的任何想法?两台计算机都使用版本 43。
同样不注册的弹出式 js 消息。相同的扩展包。
script\popup.js
'use strict';
console.log('\'Allo \'Allo! Popup');
// File executed, it's ready for the message
chrome.runtime.sendMessage(null, { action: "start"});
【问题讨论】:
标签: javascript google-chrome google-chrome-extension