Chrome扩展程序无法使用本机邮件连接到jar

时间:2015-09-03 14:19:31

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

我试着写一个非常基本的(现在)chrome exstension与可执行的jar通信,但似乎我甚至无法得到它们之间的连接。 Chrome调试说端口已断开连接,我不知道为什么并且无法找到有效的解决方案...

我的扩展程序由4个文件组成:

some icon.png

的manifest.json

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "Native",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "nativeMessaging"
  ]
}

popup.html

<!doctype html>
<html>
  <head>
    <title>Moj extension</title>
    <style>
      body {
        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
        font-size: 100%;
        width: 300px
      }
      #status {
        /* avoid an excessively wide status text */
        white-space: pre;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 400px;
      }
    </style>
    <script src="popup.js"></script>
  </head>
  <body>
  <button id="butt" type="button">Click Me!</button>
    <div id="status">test</div>
    <div id="response"></div>
  </body>
</html>

popup.js

document.addEventListener("DOMContentLoaded", function(event) { 
    document.getElementById("butt").addEventListener("click", function(){
        var port = chrome.runtime.connectNative('pl.splaw.nativejavaapp');
        port.onMessage.addListener(function(msg) {
            console.log("Received" + msg);
            document.getElementById('response').text(msg);
        });
        port.onDisconnect.addListener(function() {
            console.log("Disconnected");
        });
        port.postMessage({ text: "test" });
    });
});

爪哇

Java类看起来像这样:

package pl.splaw.nativejavaapp;

import java.io.IOException;
import java.util.Scanner;

public class NativeApp {

    public static void main(String[] args) throws IOException {
        System.out.println("STARTED");
        Scanner sc = new Scanner(System.in);

        try {
            System.in.available();
        } catch (Exception e) {
            return;
        }
        String fullMessage = "";
        while (sc.hasNext()) {
            String line = sc.nextLine();
            fullMessage += line;
            if (fullMessage.equalsIgnoreCase("test")) {
                System.out.write(getBytes("TEST OK".length()));
                System.out.write("TEST OK".getBytes());
            } else {
                System.out.write(getBytes("TEST ERROR".length()));
                System.out.write("TEST ERROR".getBytes());
            }
            break;
        }
    }

    public static 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;
    }
}

的manifest.json

jar文件旁边的

{
    "name": "pl.splaw.nativejavaapp",
    "description": "Test native app",
    "path": "C:\\PROJEKTY\\Chrome native and extension\\native\\native jar i manifest\\NativeApp-jar-with-dependencies.jar",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://ID_of_my_extension_when_added_to_chrome/"
    ]
}

我已经运行命令来添加这样的注册表项:

REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\pl.splaw.nativejavaapp" /ve /t REG_SZ /d "C:\PROJEKTY\Chrome native and extension\native\native jar i manifest\manifest.json" /f

现在,当我在Chrome中点击扩展并点击按钮时,除了控制台中的错误

之外没有任何反应
  

未捕获错误:尝试使用断开连接的端口对象

我做错了什么?

0 个答案:

没有答案