与 Chrome 扩展程序对话的本机应用程序是否永远存在?我的意思是它应该在回发之前出现吗?我找不到要添加到清单文件中的任何配置。
我有一个页面,页面上有一些对象。单击一个对象并使用本机应用程序发送/接收一条消息后,它不再适用于其余对象。
我的确切问题:本机应用程序实例对象的生命周期有多长?该对象是否应该永远响应?或者如果这是一个持续的通信,我是否需要一个循环来从标准输入读取消息?
这是我的背景脚本:
var host_name = "files.mffta.java.nativeapp";
var port = null;
initPort();
function initPort() {
console.log( 'Connecting to native host: ' + host_name );
port = chrome.runtime.connectNative( host_name );
port.onMessage.addListener( onNativeMessage );
port.onDisconnect.addListener( onDisconnected );
}
// Listen for messages that come from the content script.
chrome.runtime.onMessage.addListener(
function( messageData, sender, sendResponse ) {
if( messageData ) {
sendNativeMessage(messageData);
sendResponse( { res: 'done!' } );
}
} );
// Sending a message to the port.
function sendNativeMessage(messageData) {
if( port == null )
initPort();
console.log( 'Sending message to native app: ' + JSON.stringify( messageData ) );
port.postMessage( messageData );
console.log( 'Sent message to native app.' );
}
// Receiving a message back from the Native Client API.
function onNativeMessage( message ) {
console.log( 'recieved message from native app: ' + JSON.stringify( message ) );
alert( "messaged received from Native: " + JSON.stringify( message ) );
//sending a message to Content Script to call a function
if( message.methodName && message.methodName != "" ) {
chrome.tabs.query( { active: true, currentWindow: true }, function( tabs ) {
chrome.tabs.sendMessage( tabs[0].id, message, function( response ) {
// Call native again to return JavaScript callback function results
alert ("calc res received by extension : " + response);
sendNativeMessage({ type: "JSCallbackRes", callbackRes: response });
} );
} );
}
}
// Disconnecting the port.
function onDisconnected() {
console.log( "ERROR: " + JSON.stringify( chrome.runtime.lastError ) );
console.log( 'disconnected from native app.' );
port = null;
}
我的扩展清单:
{
"name": "Files.ChromeExt.Operarations",
"version": "1.0",
"manifest_version": 2,
"description": "This extension calls a Native API which that API calls some x-Files related operations.",
"icons": {
"128": "x-files_icon.png"
},
"permissions": [
"nativeMessaging", "activeTab"
],
"background": {
"persistent": true,
"scripts": ["main.js"]
},
"content_scripts" : [{"matches": ["http://localhost/*","https://localhost/*"],
"js": ["contentscripts/page.js"]}]
}
Java程序: [正如评论中所问的]
import java.io.IOException;
import javax.swing.JOptionPane;
public class Applet {
public Applet(){}
public static void main(String[] args) {
try {
readMessage();
sendMessage("{\"msg\" : \"hello\"}");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public static String readMessage() {
String msg = "";
try {
int c, t = 0;
for (int i = 0; i <= 3; i++) {
t += Math.pow(256.0f, i) * System.in.read();
}
for (int i = 0; i < t; i++) {
c = System.in.read();
msg += (char) c;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "error in reading message from JS");
}
return msg;
}
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");
}
}
}
通过检查 chrome 日志,我可以看到以下消息:
- 本机消息传递主机尝试发送长度为 1936028240 字节的消息。
- {"message":"与本机消息传递主机通信时出错。"}",来源:chrome-extension://XXX
我在 Win 8.0 机器 64 位上测试这些。
更新: 我现在确信如果调用 connectNative 并且端口没有因错误而停止,主机将永远存在。因此,上述错误消息的根本原因一定不是端口生命周期。我的意思是我的通信中的一些错误正在强制停止端口。
我非常感谢您提供的任何意见。