Triggering a command line run with Java based Native Messaging Host for chrome extension

150 Views Asked by At

I am trying to build a chrome extension which can run a command on the user desktop. For this I am trying to use native messaging to send the trigger from extension to a native messaging host which can then run the command.

Chrome extension's manifest.json

{
    "manifest_version": 3,
    "name": "chrome extension",
    "description": "Initial base version",
    "version": "1.0",
    "content_scripts": [
        {
          "matches": ["http://*/*", "https://*/*"],
          "js": ["contentscript.js"]
        }
      ],
    "background": {
        "service_worker": "background.js"
    },
    "action": {
        "default_popup": "hello.html"
    },
    "permissions": [
        "nativeMessaging"
    ]
}

native-messaging-host manifest

{
    "name" : "com.xx.xx.xx.xx.nativemessaginghost",
    "description" : "POC",
    "path" : "chrome-extension.bat",
    "type" : "stdio",
    "allowed_origins" : [
        "chrome-extension://kfnopggaelngaopgpiogficbbdhmlpia/"
    ]
}

chrome-extension.bat

@echo OFF

java -jar "%~dp0/native-messaging-host-java-1.0-SNAPSHOT.jar" %*

I am launching a java application which is responding to the extension.( Followed the example code from chrome-native-messaging-java )


final Application app = new Application();
ConnectableObservable<String> obs = app.getObservable();
obs.observeOn(Schedulers.computation()).subscribe(new Observer<String>() {
            public void onCompleted() {
            }

            public void onError(Throwable throwable) {
            }

            public void onNext(String s) {

                log("Host received " + s);

                JSONObject obj = new JSONObject(s);
//                executeCommand();
                log("Message is " + obj.getString("text"));

                String sl = "{\"success\":true," + "\"message\":\"" + "response from app" + "\"}";
                try {
                    app.sendMessage(sl);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
obs.connect();
while (!app.interrompe.get()) {
     try {
           Thread.sleep(1000)
         } catch (InterruptedException e) {
           break;
         }
    }
System.exit(0);

Before responding back to extension I am trying to make the host execute a command using ProcessBuilder class the following way. I am redirecting all the output of the ProcessBuilder to a file so that it doesn't overload the extension which is listening to the stdout.

ProcessBuilder processBuilder = new ProcessBuilder();      
processBuilder.redirectErrorStream(true);
File log1 = new File("command-output.log");
processBuilder.redirectOutput(log1);
builderList.add("cmd.exe");
builderList.add("/C");
builderList.add("tasklist | findstr chrome");
processBuilder.command(builderList);
Process process = processBuilder.start();

the connection to the extension is broken with the following error

Error: Native application tried to send a message of 1734439797 bytes

Am I missing anything while making the call with ProcessBuilder in native messaging host ?

The only solution I can think of is to run another local server and make an Asynchronous rest call to this server from the native messaging host. The server can then execute the command and its stdout won't affect the extension.

0

There are 0 best solutions below