How to parse JSON from stdin on a Chrome Native Messaging host?

Connected:

Using code in How to use a shell script as a host application for Chrome host for mobile devices to send JSON from the client (application) to the host

 #!/bin/bash # Loop forever, to deal with chrome.runtime.connectNative while IFS= read -r -n1 c; do # Read the first message # Assuming that the message ALWAYS ends with a }, # with no }s in the string. Adopt this piece of code if needed. if [ "$c" != '}' ] ; then continue fi # do stuff message='{"message": "'$c'"}' # Calculate the byte size of the string. # NOTE: This assumes that byte length is identical to the string length! # Do not use multibyte (unicode) characters, escape them instead, eg # message='"Some unicode character:\u1234"' messagelen=${#message} # Convert to an integer in native byte order. # If you see an error message in Chrome stdout with # "Native Messaging host tried sending a message that is ... bytes long.", # then just swap the order, ie messagelen1 <-> messagelen4 and # messagelen2 <-> messagelen3 messagelen1=$(( ($messagelen ) & 0xFF )) messagelen2=$(( ($messagelen >> 8) & 0xFF )) messagelen3=$(( ($messagelen >> 16) & 0xFF )) messagelen4=$(( ($messagelen >> 24) & 0xFF )) # Print the message byte length followed by the actual message. printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \ $messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message" done 

that leads to

 {"message":"}"} 

accepted in the client application.

The loop obviously does not commit the $c variable in the while .

With JSON input from client

 {"text":"abc"} 

using JavaScript, we could get a single JSON string property by checking the characters in the previous and next indexes

 var result = ""; var i = 0; var input = '{"text":"abc"}'; while (i < input.length) { if (input[i - 2] === ":" && input[i - 1] === '"' || result.length) { result += input[i]; } if (input[i + 1] === '"' && input[i + 2] === "}") { // do stuff break; } ++i; }; console.log(result); 

it has not yet been determined exactly how to hide the if conditions to bash from JavaScript to get one JSON property that matches the code above and is combined with the line following How to match the two string variables in the if statement in Bash? and how to combine string variables in bash? . And provided that the JSON object has nested properties using nested or multiple if conditions, you will need to correct the code several times so that the specific JSON is passed to the host.

The documentation describes the protocol.

Private Message Protocol

Chrome starts each own messaging server in a separate process and communicates with it using standard input (stdin) and standard output (Standard output). The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoding and precedes with the length of the 32-bit message in its own byte order.

Could not find answers to SO that specifically described the procedure for parsing JSON from stdin on the Chrome Native Messsaging application host using bash.

How to create a general solution or algorithm (including descriptions of each step necessary for the protocol and language) for parsing JSON sent from the client on the host from stdin , get and set the correct length of the 32-bit message in the byte order of the JSON response message that will be sent (e.g. echo ; printf ) to the client using Bash?

+1
source share

Source: https://habr.com/ru/post/1275056/


All Articles