Problems creating a NativeMessagingHost extension

have been trying hundreds of possible fixes but couldn’t find anything so decided to ask.

I’m trying to create an extension that adds a context menu option to open a url using mpv.

I’ve done 90%, manifest.json, the .json inside NativeMessagingHosts, background.js and a launch_mpv.bat.

The problem is, if I run the .bat manually using echo "C:\mpv\mpv.exe --mute=yes --screen=0 --autofit=800x450 https://example.com" | .\launch_mpv.bat , it works, but when I run the extension, I get the message Command sent to native app: C:\mpv\mpv.exe --mute=yes --screen=0 --autofit=800x450 https://example.com on the inspect extension window, but nothing happens, if I check the logs.txt, which logs what the .bat received, it says The following command was captured: l (the l is always a ‘random’ character, it changes with the url, but sometimes it’s a character that’s not present in the url or the command as a whole)

I have no idea what else to do. I’ve tried changing the .bat for a .ps1 or .py, but it’s the same characters, meaning that the background.js is the problem, but I have no idea what to change, tried a bunch of stuff with the help of chatgpt but nothing worked.

launch_mvp.bat is terrifying. Your current approach essentially exposes arbitrary code injection at a system level to a compromised browser process. Even when testing, I’d strongly encourage you to:

  1. Have your native messaging host define the CLI command rather than the extension
  2. Treat any data coming from outside the extension as hostile
  3. Sanitize your inputs

Okay, security concerns aside, it looks like you might have the wrong idea about how native messaging works. When you try to connect to your native app from the extension, Firefox will run the application declared in the "path" property of .json file in NativeMessagingHosts. That application should stay alive until the connection closes and use STDIN and STDOUT to communicate with the extension. Right now you’re assuming that the message from the extension will be sent in the first message.

I’d recommend staring from a working example and tweaking it to do what you want. If you haven’t yet, take a look at native-messaging example on MDN.