Google chrome extension 从弹出窗口发送消息到content.js在chrome扩展中不起作用
我正在尝试为chrome扩展创建一个弹出界面。我似乎无法从popup.html/popup.js向content.js脚本发送消息。这是我到目前为止所拥有的。当我点击扩展图标时,我会看到一个按钮,上面写着clickme。我点击它,什么也没有发生,chrome javascript控制台中没有错误,也没有发送到content.js的消息 显示Google chrome extension 从弹出窗口发送消息到content.js在chrome扩展中不起作用,google-chrome-extension,popup,sendmessage,Google Chrome Extension,Popup,Sendmessage,我正在尝试为chrome扩展创建一个弹出界面。我似乎无法从popup.html/popup.js向content.js脚本发送消息。这是我到目前为止所拥有的。当我点击扩展图标时,我会看到一个按钮,上面写着clickme。我点击它,什么也没有发生,chrome javascript控制台中没有错误,也没有发送到content.js的消息 显示 { "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'sel
{
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"manifest_version": 2,
"name": "extensiontest",
"version": "0.2",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
],
"browser_action": {
"default_icon": "Beaker.png",
"default_popup":"popup.html"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
]
}
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
我修改了你的
popup.js
,并使用了DOMContentLoaded
,就像Chrome建议的那样:
popup.js:
function popup() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
});
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("button1").addEventListener("click", popup);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
<!DOCTYPE html>
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
content.js:
function popup() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
});
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("button1").addEventListener("click", popup);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
<!DOCTYPE html>
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
popup.html:
function popup() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
});
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("button1").addEventListener("click", popup);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
<!DOCTYPE html>
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
我已经测试过了,它可以工作。来自2105的答案仍然适用于chrome版本88.0.4324.104。我刚刚测试过这个。我要更新的一件事是 警报(“启动”);在content.js中使用console.log(“启动”) 您有您的证据(点击F12,控制台选项卡)证明它对您有效,并且您可以验证x次单击产生x次“已启动”日志条目 确保每次“加载解包”时都重新加载目标页面,这让我卡住了一会儿 我的manifest.json如下所示:
{
"name":"Chr Ext",
"description":"blah blah",
"version":"2",
"minimum_chrome_version": "46",
"manifest_version":2,
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}],
"permissions": [
"tabs"
],
"browser_action": {
"default_popup": "popup.html"
}
}
{
“名称”:“Chr Ext”,
“描述”:“废话”,
“版本”:“2”,
“最低chrome版本”:“46”,
“清单版本”:2,
“内容脚本”:[
{
“匹配项”:[
""
],
“js”:[“content.js”]
}],
“权限”:[
“选项卡”
],
“浏览器操作”:{
“默认弹出窗口”:“popup.html”
}
}
Hi@gui47我知道你在2015年发表了这篇文章,但我想知道从那时起chrome扩展api或策略是否有重大变化,因为我尝试了你的确切代码,没有发现任何错误,但内容脚本似乎没有执行onMessage。@Nina同样的情况在这里不起作用。。。我认为在Chrome版本“version 85.0.4183.121(version 85.0.4183.121(version 85.0.4183.121(version Official Build)(64位)”中,策略已经有所改变。对于像我这样急于立即看到结果的人,不要忘记在实施此(或任何其他)修复后重新加载扩展。然后它就起作用了。