Google chrome extension 如何通过以下管道将消息从background.js传递到同一扩展内容脚本,然后再传递到应用程序?
嘿,伙计们,我正在处理一个需要与串行设备通信的项目,不幸的是,我无法直接与web应用通信,因此我决定创建一个额外的扩展,看看是否可以将信息从打包的应用发送到名为扩展“a”的扩展,并从扩展发送到web应用 现在,我在打包的应用程序中有以下代码块,它只发送一个简单的“hello world”,但我不知道如何从扩展名“a”中的background.js将消息发送到内容脚本,然后再发送到web应用程序 这是我在打包扩展中所做的简单虚拟测试:Google chrome extension 如何通过以下管道将消息从background.js传递到同一扩展内容脚本,然后再传递到应用程序?,google-chrome-extension,google-chrome-app,Google Chrome Extension,Google Chrome App,嘿,伙计们,我正在处理一个需要与串行设备通信的项目,不幸的是,我无法直接与web应用通信,因此我决定创建一个额外的扩展,看看是否可以将信息从打包的应用发送到名为扩展“a”的扩展,并从扩展发送到web应用 现在,我在打包的应用程序中有以下代码块,它只发送一个简单的“hello world”,但我不知道如何从扩展名“a”中的background.js将消息发送到内容脚本,然后再发送到web应用程序 这是我在打包扩展中所做的简单虚拟测试: var extensionId = "ncccfpjlfecg
var extensionId = "ncccfpjlfecgeggdiigcmakj22223aiiebecd";
var port = chrome.runtime.connect(extensionId);
//send hello world
port.postMessage("hello world");
扩展名“A”具有以下代码来接收数据
var CORE = {
port: null
};
chrome.runtime.onConnectExternal.addListener(function(port) {
CORE.port = port;
CORE.port.onMessage.addListener(function(msg) {
console.log(msg); // <== message come from the packaged extension.
/// next lines are where I need to find out how to send this information to the content script and then to the web app.
});
});
内容脚本如下所示
chrome.runtime.onMessage.addListener(function(msg, sender, respond) {
console.log("you send hello from brackground to inject");
});
我建议您阅读,并且(但是它没有提到使用
window.postMessage()
,或者使用自定义事件)。如果您还没有这样做,您可能还想阅读(可能还有从概述链接的页面)。好的,还有一个问题,在打包的appp的background.js中,有没有任何方法可以随时将消息传回?是还是不是?因为我可以通过以下侦听器将消息从web应用发送到打包的应用和viceversa。但是还有别的办法吗chrome.runtime.onMessageExternal.addListener(函数(请求、发送方、发送响应){sendResponse(“hello im from background.js”)}
chrome.runtime.onMessage.addListener(function(msg, sender, respond) {
console.log("you send hello from brackground to inject");
});