c# - 从 chrome 扩展到用 C# 编写的 native 主机的 native 消息传递

标签 c# google-chrome-extension chrome-native-messaging

我正在尝试通过 native 消息从我的 chrome 扩展程序接收消息。 popup.html 控制台指示消息正在发送,但我的主机由于某种原因没有收到它。我可以看到主机 native.exe 正在任务管理器中启动,但主机没有收到发送的数据。

popup.js

document.addEventListener('DOMContentLoaded', function() {
    var downloadButton= document.getElementById('Button');
    downloadButton.addEventListener('click', function () {
        chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
            chrome.tabs.executeScript(tabs[0].id, {file: "myScript.js"}, function (data) {
                sendNativeMessage(data[0]);
            });
        });
    });
});

function sendNativeMessage(msg) {
    var hostName = "com.google.example";

    console.log("Connecting to host: " + hostName);
    port = chrome.runtime.connectNative(hostName);

    message = {"text": msg};
    port.postMessage(message);

    console.log("Sent message: " + JSON.stringify(message));
}

现在,当我从我的 Chrome 扩展程序发送消息时,我可以看到 popup.html 的控制台日志并且消息是正确的。

我试图通过使用一些有人声称有效的代码来测试所有内容,来自 Native Messaging Chrome .具体来说,我有

native.exe

    static void Main(string[] args) {
        while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
        {
            Console.WriteLine(OpenStandardStreamIn());
        }
    }

    private static string OpenStandardStreamIn()
    {
        //Read first four bytes for length information
        System.IO.Stream stdin = Console.OpenStandardInput();
        int length = 0;
        byte[] bytes = new byte[4];
        stdin.Read(bytes, 0, 4);
        length = System.BitConverter.ToInt32(bytes, 0);

        string input = "";
        for (int i = 0; i < length; i++)
            input += (char)stdin.ReadByte();

        return input;
    }

我仍在努力思考 nativeMessaging,但由于我可以看到通过控制台日志发送的消息,我不得不假设错误出在我的 .exe 上。我已经创建了注册表项 HKCU,并创建了主机的 manifest.json 文件。由于扩展正在启动 .exe,我很确定所有这些都工作正常。

如果有人可以提供一些帮助,那将不胜感激。谢谢!


编辑: 在完全没有改变之后,我无法让我的 native.exe 从我的扩展中启动了。为了解决这个问题,我尝试重写我的发件人以不打开端口:

...
chrome.tabs.executeScript(tabs[0].id, {file: "myScript.js"}, function (data) {
    chrome.runtime.sendNativeMessage('com.google.example', {"text":data[0]}, function(response) {
        if (chrome.runtime.lastError)
            console.log("ERROR");
        } else {
            console.log("Response: " + response);
        }
    });
});
...

这也根本行不通。我不知道我做错了什么,也不知道为什么当我从我的分机发送消息时我的主机 .exe 停止启动。


编辑#2

好消息!

我删除了我的注册表项,然后再次将其添加到本地计算机注册表(即 HKLM)中。同样,我使用了 Native Messaging Chrome 中的代码从我的主机发送和接收。现在,当我查看我的扩展日志时,我可以看到主机的正确响应。我通过简单地调用 chrome.runtime.sendNativeMessage(...) 来使用“无端口”方法,这对我的目的来说很好。不幸的是,我没有收到从分机到主机的消息。此外,我的 host.exe 永远不会退出,即使在收到消息后也是如此。我不确定为什么,但如果有人能提供帮助,我将不胜感激。

在我的主机中,我试图将传入的消息写入一个文件,只是为了测试我是否收到消息:

System.IO.File.WriteAllText(@"path_to_file.txt", OpenStandardStreamIn());

注意:我从扩展程序传递到主机的消息大约为 250KB(因消息而异)。

问题是:

  1. 没有任何内容写入文件。一片空白。
  2. 创建文件需要很长时间(~4 秒)。
  3. 我的host.exe 实例永远不会终止。它仍在运行(我可以在任务管理器中查看它)。

最佳答案

为了让我的扩展正常工作,我遵循了以下步骤:

  1. 我删除了我的注册表项,然后将其重新添加到我的 HKLM 注册表中。
  2. 虽然不必要,host.exe 实际上只需要从扩展接收一条消息,所以我将之前的“开放端口”消息更改为“无端口”消息,即

    chrome.runtime.sendNativeMessage(...);

  3. 重要的是,当我将发送的文本从 {"text":data[0]} 更改为简单的字符串 {"text":"Hello from Chrome !”,一切开始顺利进行。 data[0] 是一个大约 250KB 的字符串,根据 developer's site 这绝对应该是可接受的消息大小.但是,我创建了一个 different question对于这个问题,因为我能够解决我遇到的一般消息发送和接收问题。

关于c# - 从 chrome 扩展到用 C# 编写的 native 主机的 native 消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27643892/

相关文章:

c# - 我可以为 .NET Windows 窗体窗口中的工具提示设置无限的 AutoPopDelay 吗?

c# - 是否使用 select 或 AsEnumerable().Where()

c# - 网络驱动器在启动时不可用

c# - 在 Rx 中,为什么 When 给我过时的元素

javascript - 调试 Chrome 原生消息传递

javascript - chrome 扩展检测 ERR_PROXY_CONNECTION_FAILED

google-chrome - 我在 "permissions"中有站点,但不断收到 "Origin chrome-extension://abc is not allowed by Access-Control-Allow-Origin."

javascript - Chrome 扩展程序 : Open new tab on startup

Windows Chrome 扩展 native 消息只能收到第一个响应

nativemessaging 应用程序和 webextension 之间的异步双向消息传递?