弹出式脚本和内容脚本无法通信? [英] Popup script and content script can't communicate?

查看:101
本文介绍了弹出式脚本和内容脚本无法通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我似乎无法从popup.js传输到我的内容中获取消息 chrome.extension.sendMessage(on); 。 js。



来自popup.js的代码:

 函数click (e){
if(e.target.id ==green){
chrome.extension.sendMessage(start);
console.info(oN);
return;
}

if(e.target.id ==red){
chrome.extension.sendMessage(stop);
console.info(oFF);
return;


popup.js在收到消息时非常好在代码中添加一个监听器。但我的content.js似乎无法得到它。



来自content.js的代码:



<$ p $ b $ chrome.extension.onMessage.addListener(
函数(request,sender,sendResponse){
console.info(ok);
}
);

清单:

 content_scripts:[
{
matches:[< all_urls>],
js:[content.js],
run_at:document_end
}
],

帮助非常感谢。

解决方案

chrome.extension.sendMessage 是一个非经典名称。



旧的不推荐使用的API是 chrome.extension.sendRequest ,新的API是 chrome.runtime.sendMessage ,并且事件同样是 chrome.runtime.onMessage






那就是说,你的问题是试图发送一条消息给内容脚本 chrome.runtime.sendMessage 将消息发送到分机自己的页面;内容脚本不被视为这样。


要发送消息到内容脚本,您必须使用 tabId chrome.tabs.sendMessage API调用来调用/ extensions / tabs#method-sendMessagerel =nofollow noreferrer / code>。



假设您想要当前可见标签:

 <$ (e){
if(e.target.id ==green){
chrome.tabs.query({active:true,currentWindow:true},function(标签){
chrome.tabs.sendMessage(tabs [0] .id,start);
});
console.info(oN);
return;

/ * ... * /
}

如果您想要 all 标签,只需将 {} 传递给查询并遍历 tabs



最后,注意内容脚本注入时间怪癖


My problem is that I can't seem to get the message chrome.extension.sendMessage("on"); from my popup.js transfer to my content.js.

Code from the popup.js:

function click(e) {
    if ( e.target.id == "green"){
        chrome.extension.sendMessage("start");
        console.info("oN");
        return;
    }

    if ( e.target.id == "red"){
        chrome.extension.sendMessage("stop");
        console.info("oFF");
        return;
    }
}

The popup.js receives the message perfectly well when i add a listener to the code. But my content.js can't seem to get it.

Code from the content.js:

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.info("ok");
    }
);

Manifest:

"content_scripts": [
{
    "matches": ["<all_urls>"],
    "js": ["content.js"],
    "run_at": "document_end"
}
],

Any help is greatly appreciated.

解决方案

chrome.extension.sendMessage is a non-canonical name.

The old, deprecated API is chrome.extension.sendRequest, and the new API is chrome.runtime.sendMessage, and the event is likewise chrome.runtime.onMessage.


That said, your problem is trying to send a message to a content script. chrome.runtime.sendMessage send messages to extension's own pages; content scripts are not considered such.

To send a message to a content script, you have to use the chrome.tabs.sendMessage API call by tab's tabId.

Assuming you want the current visible tab:

function click(e) {
    if ( e.target.id == "green"){
        chrome.tabs.query({active:true, currentWindow: true}, function(tabs){
            chrome.tabs.sendMessage(tabs[0].id, "start");
        });
        console.info("oN");
        return;
    }
    /* ... */
}

If you want all tabs, just pass {} to query and iterate over tabs.

Finally, take note of content scripts inject time quirks.

这篇关于弹出式脚本和内容脚本无法通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆