Chrome 与 C 原生应用程序的扩展通信

Chrome extension communication with C native app

我正在尝试使用本机消息 API 从我的 C 本机应用程序向我的 chrome 扩展发送一个字符串。但是我无法在分机上收到它。

扩展行为非常简单,当我单击扩展图标时,我想在浏览器控制台上接收我的 C 程序字符串,但我收到以下内容:

Received[object Object]                    background.js:7 
Disconnected                               _generated_background_page.html:1 
Unchecked runtime.lastError: Native host has exited.

C 应用程序清单:

{
  "name": "com.wasm.test",
  "description": "My Application",
  "path": "/home/chrome-native-message-test/app",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://oegageepidiekaeopfojnglhkmdjaomi/"
  ]
}

C 应用程序:

 #include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
    // Define our message
    char message[] = "{\"text\": \"This is a response message\"}";
    // Collect the length of the message
    unsigned int len = strlen(message);
    // We need to send the 4 bytes of length information
    printf("%c%c%c%c", (char) (len & 0xff),
                       (char) ((len>>8) & 0xFF),
                       (char) ((len>>16) & 0xFF),
                       (char) ((len>>24) & 0xFF));
    // Now we can output our message
    printf("%s", message);
    return 0;
}

扩展清单:

{
    "name": "extension test",
    "version": "1.0",
    "description": "native message testing",
    "permissions": ["nativeMessaging"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "browser_action": {
        "default_title": "receive native message",
    },
    "manifest_version": 2
}

分机 background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    var port = chrome.runtime.connectNative('com.wasm.test');
    port.onMessage.addListener(function(msg) {
        console.log("Received" + msg);
    });
    port.onDisconnect.addListener(function() {
        console.log("Disconnected");
    });
});

我错过了什么吗? 谢谢

正在运行。我正在尝试打印 RAW json..

使用 console.log(JSON.stringify(result)) 以字符串格式获取 JSON。