我无法从后台脚本向内容脚本发送消息,除非我从manifest.json中的浏览器操作中删除popup.html。谁来帮帮我
background.js
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tabs) {
// Send a message to the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(tabs[0].id, {"message": "clicked_browser_action"});
});
});
// This block is new! and will be used later
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "open_new_tab" ) {
chrome.tabs.create({"url": request.url});
}
}
);
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message == "clicked_browser_action" ) {
console.log("Congratulations you can now work!");
// This line is new!
chrome.runtime.sendMessage({"message": "open_new_tab", "url": firstHref});
}
}
);
的manifest.json
{
"name": "Wowprezi lead tool",
"version": "1.0",
"description": "Extension to find leads and add to sales force!",
"author": "Djouonang Landry",
"manifest_version": 2,
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
"background": {
"persistent": true,
"scripts": ["js/background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/jquery-3.3.1.min.js","js/content.js"],
"all_frames": true
}
],
"browser_action": {
"default_popup": "html/popup.html",
"default_title": "Find leads"
}
}
popup.html - 除了将其从manifest.json中删除,我无法从后台向content.js发送消息。请注意我使用控制台功能来检查消息是否已发送到content.js
根据documentation,chrome.browserAction.onClicked
听众是
单击浏览器操作图标时触发。如果浏览器操作有弹出窗口,则不会触发。
(加粗型)。
删除browserAction弹出窗口或将当前在侦听器中的代码移动到弹出页面脚本。