0

JavaScript無法接收從Java到JavaScript的EiD值。我該如何解決它?JavaScript無法通過Google Chrome擴展程序接收Java數據包

JavaScript中,谷歌Chrome擴展:

port.onMessage.addListener(function(message) { // from .exe 
    sendToWebSite(JSON.stringify(message)); 
}); 

enter image description here

Java中的EID數據發送給JavaScript:

public static void sendMessage(String msgdata) { 
    try { 
     int dataLength = msgdata.length(); 
     System.out.write((byte) (dataLength & 0xFF)); 
     System.out.write((byte) ((dataLength >> 8) & 0xFF)); 
     System.out.write((byte) ((dataLength >> 16) & 0xFF)); 
     System.out.write((byte) ((dataLength >> 24) & 0xFF)); 

     // Writing the message itself 
     System.out.write(msgdata.getBytes()); 
     System.out.flush(); 
    } catch (IOException e) { 
     JOptionPane.showMessageDialog(null, "error in sending message to JS"); 
    } 
    } 

MSGDATA如下=

enter image description here

回答

0

以下作品。

String tmp = Beid.bid(); //big string with \n and hash picture: 
    String[] output = tmp.split("picture:"); 
    tmp = output[0]; //how to avoid split picture:? 

    tmp = tmp.replaceAll("(\r\n|\n)", "<br/>"); 
    sendMessage("{\"msg\" : \"" + tmp + "\"}"); 

編輯:

  • 的Windows 10它不工作,因爲它有老的Java,安裝Java 8,它的工作之後。
相關問題