javascript - Java 和 Chrome 扩展、本地消息

标签 javascript java google-chrome google-chrome-extension chrome-native-messaging

我尝试做一个 chrome 扩展来调用我的 PC 中的 java 代码。调用工作正常,代码执行,但我尝试将变量返回到 chrome 扩展,但不起作用。我在控制台中看到监听器 onDisconect 写入控制台消息,但监听器 onMessage 不写入。我不知道问题所在。

这是我在 chrome 扩展中的代码:

list JSON

{
    "name": "Prueba native message",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Chrome extension interacting with Native Messaging and     localhost.",
    "app": {
    "background": {
        "scripts": ["background.js"]
    }
},
    "icons": {
    },
    "permissions": [
        "nativeMessaging"
    ]
}

背景.js

var port = chrome.runtime.connectNative('com.app.native');

function message(msg) {
    console.warn("Received" + msg);
}

function disconect() {
    console.warn("Disconnected");
}

console.warn("se ha conectado");

port.onMessage.addListener(message);
port.onDisconnect.addListener(disconect);
port.postMessage({text: "Hello, my_application"});

console.warn("message send");

这是我的本地文件。

.bat

cd C:\Users\pc\IdeaProjects\eDNI\out\production\code && java Main

Main.java

public class Main {
    public static void main(String argv[]) throws IOException {
        System.out.println("{\"m\":\"hi\"");
    }
}

在此代码中,我仅尝试向扩展程序返回一条简单的消息。

最佳答案

Native messaging protocol

Chrome starts each native messaging host in a separate process and communicates with it using standard input (stdin) and standard output (stdout). The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order. The maximum size of a single message from the native messaging host is 1 MB, mainly to protect Chrome from misbehaving native applications. The maximum size of the message sent to the native messaging host is 4 GB.

来源: Native Messaging Protocol

前四个字节必须是消息的长度。您需要将消息长度(整数)转换为字节数组:

选项 1:使用 java.nio.ByteBuffer

public byte[] getBytes(int length) {
    ByteBuffer b = ByteBuffer.allocate(4);
    b.putInt(length);
    return b.array();
}

选项 2:手动:

public byte[] getBytes(int length) {
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (length & 0xFF);
    bytes[1] = (byte) ((length >> 8) & 0xFF);
    bytes[2] = (byte) ((length >> 16) & 0xFF);
    bytes[3] = (byte) ((length >> 24) & 0xFF);
    return bytes;
}

写出消息长度,然后写出消息内容(以字节为单位)。

String message = "{\"m\":\"hi\"}";
System.out.write(getBytes(message.length())); 
System.out.write(message.getBytes("UTF-8"));
System.out.flush();

更新:

您似乎还缺少需要在 list 文件中指定的接口(interface)类型。

添加此:“type”:“stdio”

关于javascript - Java 和 Chrome 扩展、本地消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096419/

相关文章:

javascript - Angular 下的 ng-click 下的 ng-repeat 下

javascript - 我怎样才能填充已经穿过的svg路径?

java - 如何在 Java 中高效地编写大量/多个敌人的生成和碰撞检测代码

java - Java 如何使用 "+"进行字符串连接?

javascript - chrome 在 WebSocket 握手期间出现新错误

javascript - Laravel-Vue 单元测试错误 "SyntaxError: Unexpected token {"

javascript - 快速子域路由无法正常工作?

java - 驾驶员考试多项选择数组while循环提示学生答题方法

javascript - 如何确定视频是否在 Chrome 中以全屏模式播放

css - 使用 chrome 开发者工具注入(inject) CSS?