【问题标题】:Chrome-extension: How to send message from content.js to popup.js - Page_ActionChrome 扩展:如何将消息从 content.js 发送到 popup.js - Page_Action
【发布时间】:2018-02-05 10:15:28
【问题描述】:

Chrome v64.

我想从 content.js 向 popup.js 发送消息。

我设法从 popup.js 向 content.js 发送了一条消息。但是如何反其道而行之呢?我还下载了一个示例扩展,它也不起作用。

我必须添加特殊权限吗? 我尝试了一次性消息和长时间运行的消息通道。

权限:

 "permissions": [
    "background",
    "tabs",
    "activeTab",
    "storage",
    "webRequest",

Content.js

chrome.runtime.sendMessage({
    data: "mauzen"
}, function (response) {
   return true;
});
debugger;
var port = chrome.runtime.connect({
    name: "knockknock"
});
port.postMessage({
    joke: "Knock knock"
});
port.onMessage.addListener(function (msg) {
    debugger;
    if (msg.question == "Who's there?")
        port.postMessage({
            answer: "Madame"
        });
    else if (msg.question == "Madame who?")
        port.postMessage({
            answer: "Madame... Bovary"
        });
});

Background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    sendResponse({
        data: "background"
    });
    if (request.data === "success") {
        alert("success");
    } else {
        //alert(request.data);
    }
});

console.assert(port.name == "knockknock");
port.onMessage.addListener(function (msg) {
    if (msg.joke == "Knock knock")
        port.postMessage({
            question: "Who's there?"
        });
    else if (msg.answer == "Madame")
        port.postMessage({
            question: "Madame who?"
        });
    else {
        port.postMessage({
            question: "background"
        });
    }
});

Popup.js

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    sendResponse({
        data: "popup"
    });
    if (message.data === "success") {
        alert("success");
    } else {
        // alert(message.data);
    }
});

chrome.runtime.onConnect.addListener(function (port) {
    console.assert(port.name == "knockknock");
    port.onMessage.addListener(function (msg) {
        if (msg.joke == "Knock knock")
            port.postMessage({
                question: "Who's there?"
            });
        else if (msg.answer == "Madame")
            port.postMessage({
                question: "Madame who?"
            });
        else {
            port.postMessage({
                question: "popup"
            });
        }
    });
});

【问题讨论】:

    标签: google-chrome google-chrome-extension


    【解决方案1】:

    这是我发现的,测试了一下。

    要将消息从 content.js 脚本发送到您的弹出窗口,请执行以下操作:

     chrome.runtime.sendMessage({
                        data: "Hello popup, how are you"
                    }, function (response) {
                        console.dir(response);
                    });
    

    和 在您的 popup.js 中:

    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        sendResponse({
            data: "I am fine, thank you. How is life in the background?"
        }); 
    });
    

    只有在您的 Popup 处于活动状态 (=open) 时,即您点击 page_action ( browser_action) 图标并出现弹出窗口,然后它处于打开/活动状态。只有这样,它才能发送和接收消息!


    你可以这样测试

    将以下脚本放入您的content.js:

     var timer = 0;
      var si = setInterval(() => {
                try {
                   chrome.runtime.sendMessage({
                        data: "Hello popup, how are you"
                    }, function (response) {
                        console.dir(response);
                    });
                    timer++;
                    if (timer === 5) {
                        clearInterval(si);
                    }
                } catch (error) {
                    // debugger;
                    console.log(error);
                }
            }, 2000);
    

    在你的 **popup.js 中:**

    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        alert("I am popup!");
        sendResponse({
            data: "I am fine, thank you. How is life in the background?"
        }); 
    });
    

    只要setInterval执行,你可以点击你的扩展图标,打开弹出窗口,然后它会显示一个警报。

    【讨论】:

    • 我已经使用了你的代码,但是我收到了这个错误Unchecked runtime.lastError: The message port closed before a response was received.
    【解决方案2】:

    从您的代码看来,您正在从内容脚本向后台和弹出脚本发送消息,这与您所描述的相反。

    从您的扩展程序向内容脚本发送消息需要您使用chrome.tabs.sendMessage,请参阅https://developer.chrome.com/apps/messaging

    【讨论】:

    • chrome.tabs.sendMessage 是发送消息 TO 内容脚本,而不是来自它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多