点击扩展图标后,我试图从content.js
向background.js
发送消息。
Background.js
:
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.query({active : true, lastFocusedWindow : true}, function (tabs) {
var CurrTab = tabs[0];
chrome.tabs.sendMessage(CurrTab, 'run');
})
})
Content.js
:
chrome.runtime.onMessage.addListener(function(){
view();
})
我在background.js
中遇到此错误,我不知道为什么。
Error handling response: TypeError: Error in invocation of
tabs.sendMessage(integer tabId, any message, optional object options,
optional function responseCallback): No matching signature.
我在做什么错了?
答案 0 :(得分:2)
在Background.js中,更改以下内容:
chrome.tabs.sendMessage(CurrTab, 'run');
到
chrome.tabs.sendMessage(CurrTab.id, 'run');
如wOxxOm在评论中所述。
其次,请确保在manifest.json文件中,您已在content_scripts / matches标记中指定了网站的网址(需要在其中注入内容脚本)。