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

时间:2014-12-25 03:05:29

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

我尝试通过原生消息从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中的代码从我的主机发送和接收。现在,当我查看我的扩展程序日志时,我可以看到主机的正确响应。我正在使用&#34; no-port&#34;简单地调用chrome.runtime.sendNativeMessage(...)的方法,这对我来说很好。不幸的是,我没有收到来自主机扩展的消息。此外,即使在收到消息后,我的host.exe也永远不会退出。我不确定为什么,但是如果有人可以提供帮助,我将不胜感激。

在我的主机中,我试图将传入的消息写入文件,以测试我收到的消息:

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

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

问题是:

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

1 个答案:

答案 0 :(得分:1)

为了让我的扩展程序正常工作,我按照以下步骤操作:

  1. 我删除了我的注册表项,然后将其重新添加到我的HKLM注册表中。
  2. 虽然没必要,host.exe实际上只需要从扩展程序接收一条消息,所以我改变了以前的#34;开放端口&#34;发送到&#34; no port&#34;消息,即

    chrome.runtime.sendNativeMessage(...);

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

相关问题