NativeMessaging addon with error in console: "Attempt to postMessage on disconnected port"

BTW I can do it: Python script gets the ping, repasses to the Bash script, and pong: the bash script repasses to the Python script that repasses to the addon.

So, the browser/addon and the Bash script are still the transmitters/receivers, and the Python script is the medium.

Hi folks, this is a gentle reminder that we expect respectful interaction in this space as part of our Community Participation Guidelines. Personal attacks are not welcome.

Not necessarily. An understanding of the API is necessary though. From your post, it wasn’t clear whether you’ve understood the example that you’re modifying, because the logic of your “native messaging host” (the shell script) was far from a correct implementation of the documented “stdio” protocol of the nativeMessaging API.

Note that even in that post that I wrote 6+ years ago (and is still valid today), the first line of the answer is “I suggest to not use (bash) shell scripts as a native messaging host, because bash is too limited to be useful.”. The shown example has several caveats (read the inline code comments!) to show under which limited circumstances it works.

You were using while read line; do if [ "${line}" = "ping" ] ; then. This has two issues: (1) read reads line by line. The native messaging protocol does not use line breaks, but <integer of message length><message here>. The read call never assigns the input to line. (2) even if the loop body was entered, then your if-condition would be false.

Please read my Stack Overflow answer carefully. The demo script “works”, but only under very limited circumstances (stdin discarded, message in a specific format, output not using multibyte characters). The majority of the answer is filled with warnings and explanations on why Bash is not a good choice as the language to write a native messaging host.

The start of your answer is a better use of bash: as an intermediary. It’s not uncommon to have platform-specific shell scripts to wrap the actual program. But your insistence on using Bash for primary control flow is strange. It makes the programming task more difficult, and the resulting program performs worse than a pure-Python script.

If you really want to end up using a Bash script, then I recommend to still start with a Python script (or any other language/program) that behaves as desired. Once it works, commit the changes, and then create your desired new Bash script to reproduce the logic. If the Bash script doesn’t work as expected, then you can compare its behavior with the other program to see if it’s an issue with your Bash script, or with your extension.