I have a problem with Java application and Google Chrome due to NPAPI deprecation.. Reading some articles and blogs, I've found a "solution" : Native Messaging.
But all the examples were made in C++/C#/Python, and for Java there is nothing.. I would like if someone could help me with this..
Here's my schema (see image): Google chrome Extension -> Native messaging -> Java App (jar)
Problem: The extension calls the Java app, the Java app runs but doesn't receive anything and when it returns, nothing comes to extension.
Here's my 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 by background.js"}); to [' + tabs[0].id + "/" + tabs[0].url + ']');
  chrome.runtime.sendNativeMessage('cnb.digitalsigning',message,function(response) {
    console.log("Received from native " + response);
  });
 
  chrome.tabs.sendMessage(tabs[0].id, {content: "generated by background.js"});
 });
 return true;
  });  
  
port.onDisconnect.addListener(function() {
  console.log("Background.js Port disconnected");
});
});
  //native messaging
content_script.js
window.addEventListener("message", function(event) {
  // We only accept messages from ourselves
  
  if (event.source != window)
    return;
  if (event.data.type && (event.data.type == "DIGITAL_SIGNER_MSG")) {
    console.log("Content script received  from 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 from 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 a Main that captures the message using System.in.read and response to extension using System.out.write..
How could I do this communication? Is this the correct way to start the java app?
                        
At https://stackoverflow.com/a/25617143/865284 you can see how to send data in a proper way. System.in should be the correct way, but by now, i didn't find a way to receive data in java.