3

我创建了一个 Chrome 扩展程序,并将 nativeMessaging 权限添加到清单中。

我正在为本地消息传递主机打开一个端口。

我的本机消息传递主机的清单指向一个可执行的 jar 文件。

当我启动我的扩展时,没有错误,连接似乎很好,但我的 jar 文件中的代码似乎永远不会运行。(为了测试,我展示了一个在 main() 方法中创建的新 JFrame。当双击 jar 文件时,它会显示 JFrame。但是当通过 Chrome Native Messaging 运行 jar 文件时,JFrame 不会出现。

有什么我想念的吗?

4

3 回答 3

1

你的主要名称中有大写字母吗?我有这个问题+相同的症状,这为我修复了它(虽然我现在得到一个不同的错误)

{“名称”:“com.google.chrome.example.echoUppercase”,<-不会工作....

“名称”:“com.google.chrome.example.echouppercase”,<- 有效 }

于 2015-05-05T14:08:51.407 回答
1

我设法通过让 chrome 启动具有以下内容的 bat 文件来使其工作:

@echo off
java -jar your_file.jar %*

回声关闭是一个细节,我花了一些时间才弄清楚。

于 2015-09-09T12:55:12.907 回答
0

我已经设法使用 Java 可执行包装器(我使用了 Launch4j)来做到这一点。

这是清单文件:

{
    "name": "com.your.application",
    "description": "Your description.",
    "path": "path\\to\\wrapped\\java\\host.exe",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://idofyourchromeextension/"
    ]
}

这是我包装的本机主机应用程序:

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URISyntaxException;

public class ChromeHandler
{
    static public void main(String[] args)
    {
        InputStream input = System.in;
        try
        {
            System.in.available();
        }
        catch (Exception e)
        {
            return;
        }
        char receivedChar;
        try{
            FileOutputStream output = new FileOutputStream("PathToTextFile");
            while((receivedChar = (char) input.read()) != -1)
            {
                output.write(receivedChar);
                //My messages only have the '}' character at the end.
                if(receivedChar == '}')
                {
                    return;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2015-08-14T20:06:57.413 回答