Chrome扩展程序:如何将消息从content.js发送到popup.js - Page_Action

时间:2018-02-05 10:15:28

标签: google-chrome google-chrome-extension

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"
            });
        }
    });
});

2 个答案:

答案 0 :(得分:1)

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

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

df = df[~(df.duplicated(['Service','Status'], keep=False) & (df['Status'] == 'Healthy'))]
你的popup.js中的

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

当您的弹出窗口处于活动状态(=打开)时,只会收到从content.js发送到popup.js的消息 < / h2>,即单击工具栏中的page_action(browser_action)图标,弹出窗口,然后打开/激活。只有这样,它才能发送和接收消息!

enter image description here

您可以像这样测试

将以下脚本放入 content.js:

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

并在您的** popup.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);

只要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?" }); }); 执行,您就可以点击您的扩展程序图标,打开弹出窗口,然后它会显示警告。

答案 1 :(得分:0)

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

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