gpt4 book ai didi

google-chrome - Chrome 扩展 : How to send message from content. js 到 popup.js - Page_Action

转载 作者:行者123 更新时间:2023-12-03 16:25:06 26 4
gpt4 key购买 nike

Chrome v64。

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

我设法从 popup.js 向 content.js 发送消息。但是如何以相反的方式做到这一点?我还下载了一个示例扩展,它也不起作用。

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

权限:

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

最佳答案

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

发送消息 来自 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?"
});
});

从 content.js 发送到 popup.js 的消息只会在您的 Popup 处于事件状态(=打开)时收到 ,即您单击工具栏中的 page_action (browser_action) 图标并出现弹出窗口,然后它是打开/事件的。只有这样,它才能发送和接收消息!

enter image description here

你可以这样测试

将以下脚本放入您 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执行你可以点击你的扩展图标,打开弹出窗口,然后它会显示一个警报。

关于google-chrome - Chrome 扩展 : How to send message from content. js 到 popup.js - Page_Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48620183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com