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



我正在尝试使用本机消息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
}

扩展背景.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。

最新更新