google chrome - Java + Native messaging doesn't work good -
i have problem java application , google chrome due npapi deprecation.. reading articles , blogs, i've found "solution" : native messaging.
but examples made in c++/c#/python, , java there nothing.. if me this..
here's schema (see image): google chrome extension -> native messaging -> java app (jar)
problem: extension calls java app, java app runs doesn't receive , when returns, nothing comes extension.
here's code:
chrome extension
background.js:
chrome.runtime.onconnect.addlistener(function(port) { port.onmessage.addlistener( function(message) { chrome.tabs.query({active: true, currentwindow: true}, function(tabs) { console.log( 'background.js received msg [' + message.text + ']'); console.log( 'port.postmessage({content: "generated background.js"}); [' + tabs[0].id + "/" + tabs[0].url + ']'); chrome.runtime.sendnativemessage('cnb.digitalsigning',message,function(response) { console.log("received native " + response); }); chrome.tabs.sendmessage(tabs[0].id, {content: "generated background.js"}); }); return true; }); port.ondisconnect.addlistener(function() { console.log("background.js port disconnected"); }); }); //native messaging
content_script.js
window.addeventlistener("message", function(event) { // accept messages ourselves if (event.source != window) return; if (event.data.type && (event.data.type == "digital_signer_msg")) { console.log("content script received webpage: " + event.data.text); console.log('calling port.postmessage(event.data.text); ...' + event.data.text ); port = chrome.runtime.connect(); port.postmessage({text: event.data.text}); } }, false); chrome.runtime.onmessage.addlistener( function(response, sender, sendresponse) { alert("receiving response extension" + response.content ); });
manifest.json
{ "name": "test", "description": "test", "version": "0.1", "manifest_version": 2, "background": { "persistent" : true, "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["*://*/*"], "js": ["content_script.js"] } ], "permissions": [ "nativemessaging" ] }
html
<html> <script> function myfunc(){ console.log('call func'); window.postmessage({ type: "digital_signer_msg", text: "do it, baby!" }, "*"); console.log('func called'); } </script> <body> <div>page</div> <form> <button id="caller" onclick="myfunc()">call extension</button> <div id="reponsediv" style="border: 3px; border-color: blue;">no response loaded</div> </form> </body> </html>
host
installreg.bat
reg add "hkcu\software\google\chrome\nativemessaginghosts\digitalsigning" /ve /t reg_sz /d "%~dp0digitalsigning.json" /f pause
launch.bat
c:\location\to\java\jre\bin\java.exe -cp c:\component\target\java.jar com.org.app pause
digitalsigning.json
{ "name": "digitalsigning", "description": "digitalsigning", "path": "c:\\place\\launch.bat", "type": "stdio", "allowed_origins": [ "chrome-extension://<google extension id generated>/" ] }
the aplication contains main captures message using system.in.read
, response extension using system.out.write
..
how communication? correct way start java app?
at https://stackoverflow.com/a/25617143/865284 can see how send data in proper way. system.in should correct way, now, didn't find way receive data in java.
Comments
Post a Comment