我的背景页面上有这个:
function go(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
go();
这是我的内容脚本:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
alert(request.greeting);
});
我希望在启用该应用程序时启动警报,但是什么也没有发生。我想念什么吗?
编辑:manifest.json:
{
"manifest_version": 2,
"name": "Naming",
"description": "any",
"version": "1.0",
"permissions": [
"tabs",
"activeTab",
"https://google.com.br/"
],
"background": {"scripts":["background.js"]},
"content_scripts": [
{
"js": ["CLAWS_Content_Script.js", "jquery.js"],
"css": ["CLAWS_Content_Script_CSS.css"],
"matches": ["<all_urls>"]
}
],
"web_accessible_resources": [
"CLAWS_Sem_Imagens.html",
"icone_XVermelho.png"
]
}
内容脚本被加载,因为它提供的其他功能完美地起作用了。控制台上没有错误。
当浏览器启动或启用扩展时,将发送该消息,但那时没有活动的内容脚本可接收该消息。同样,当您重新加载网页时,不会重新加载背景页面,因此不会发送任何消息,因此,什么也收不到。
如果确实需要the alert to launch as I enable the extension
,请通过chrome.tabs.executeScript手动注入内容脚本,在这种情况下,您不需要"content_scripts"
manifest.json中的部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句