为什么我没有收到来自 Node.JS Native App 到我的 chrome 扩展程序的任何消息?

问题描述

我是 chrome 本机消息传递的新手,我想在我的 chrome 扩展程序和本机应用程序之间建立连接。澄清一下,我想从我的分机接收消息,处理它们,然后发回其他消息。但现在我正试图从一方面做到这一点。阅读了有关它的所有必要信息后,我遇到了一个问题,即我的扩展保持沉默并且不记录收到的(可能)消息。

popup.js:

let port = chrome.runtime.connectNative("com.defus.nodejs");

console.log("Waiting for response...");

port.onMessage.addListener((response) => {
  console.log("Received: " + response);
});

manifest.json(应用程序):

{
  "name": "com.defus.nodejs","description": "Run Node.JS","path": "./node.bat","type": "stdio","allowed_origins": [
    "chrome-extension://myid/"
  ]
}

node.bat:

@echo off
node ./index.js

index.js (nodejs): — How to send message from native app written in Node.js to Chrome extension?(我尝试使用链接,但其中一些,例如来自评论的 https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging$compare?locale=en-US&to=1595872&from=1593969 已损坏...)

const header = Buffer.alloc(4);
let message = "something\n";

header.writeUInt32LE(message.length,0);

process.stdout.write(header);
process.stdout.write(message);

我已经在 regedit 中注册了我的应用程序 Computer\HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.defus.nodejs,如果我只是启动一个随机应用程序,一切都正常。

我试图使用绝对路径、发送 json 对象(和字符串化对象)以及从 nodejs 应用程序获取消息的其他各种实现,但没有任何解决方案无济于事。我只是得到“等待回应......”,仅此而已。谁能提供一个简单的例子来说明如何做我需要的事情。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)