我想从content-script向后台脚本发送一个名为"website_hostname“的变量。它包含您当前所在网站的主机名。
内容脚本:
var website_hostname = window.location.href;
//Code to send website_hostname 背景脚本:
// Get website_hostname 发布于 2020-10-12 01:21:54
我相信你可以通过使用chrome onMessage来做到这一点。
在内容方面,
//content-script.js
function notifyExtension() {
chrome.runtime.sendMessage({"url": window.loaction.href});
}
//background.js
chrome.runtime.onMessage.addListener(notify);
function notify(message) {
variable = message.url
});
}这是我从https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage那里得到的
发布于 2020-12-05 03:37:43
与上一个答案中的建议类似,但要做到这一点,最好使用长期连接端口
长期连接https://developer.chrome.com/extensions/messaging
var port = chrome.runtime.connect() Send a message to background page port.postMessage()监听程序
for response port.onMessage.addListener()https://stackoverflow.com/questions/64306818
复制相似问题