我正在创建一个 Google Chrome 扩展程序,并且有一个 background.js 和一个 content_script.js。当网页加载时,我向内容脚本发送一条消息以运行函数;不幸的是它的表现并不如预期。有问题的代码位于我的 content_script.js 文件中,如下所示:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
alert("Test 1 2 3");
welcome(tab);
}
);
我有一个名为 welcome(tab)
的函数,它将当前选项卡作为参数并执行一些任意函数。我知道正在收到消息,因为 alert
总是会发生,但 welcome
函数永远不会执行。我知道 welcome
功能有效,我已经做了很多测试。当我添加 addListener
时,事情停止了。有什么建议么?到目前为止我已经到处找过了。另外,这可能与 addListener 函数不知道全局 tab
变量是什么有关吗?
最佳答案
我认为问题出在此处的选项卡上。看看这个:https://developer.chrome.com/extensions/messaging
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
alert("Test 1 2 3");
welcome(sender.tab);
}
);
关于javascript - 在 chrome.runtime.onMessage.addListener 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468776/