Created
April 25, 2022 05:34
-
-
Save guest271314/b9ded17c61323640a051b8574528656d to your computer and use it in GitHub Desktop.
Bash Native Messaging host. Echo JSON string that does not include (escaped) double quotes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Bash Native Messaging host, guest271314 2022 | |
# Echo JSON string that does not include (escaped) double quotes | |
# https://stackoverflow.com/a/24777120 | |
sendMessage() { | |
message=\"$1\" | |
# 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, e.g. | |
# message='"Some unicode character:\u1234"' | |
messagelen=${#message} | |
# Convert to an integer in native byte order. | |
# If you see an error message in Chrome's stdout with | |
# "Native Messaging host tried sending a message that is ... bytes long.", | |
# then just swap the order, i.e. 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" | |
} | |
getMessage() { | |
input="" | |
datacount=0 | |
doublequotecount=0 | |
# Loop forever, to deal with chrome.runtime.connectNative | |
while true; do | |
IFS= read -res -n1 data | |
# Do not process encoded length of JSON. | |
# TODO: Process encoded length of JSON (1st $data read). | |
if ((datacount++)); then | |
# Do not write double quotation marks to variable $input. | |
if [ "$data" == "\"" ]; then | |
# Pass $input to sendMessage to echo when 2d double quotation mark is read | |
if ((doublequotecount++)); then | |
sendMessage "$input" | |
break | |
fi | |
continue | |
fi | |
# Write $data (stdin) that is not double quotation marks to $input. | |
if [ "$data" != "\"" ]; then | |
# 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. | |
input="${input}${data}" | |
fi | |
continue | |
fi | |
done | |
} | |
getMessage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = new String(`Test`); | |
(async()=>{ | |
self.port = chrome.runtime.connectNative('nm_bash') | |
port.onMessage.addListener((message)=>{ | |
if (chrome.runtime.lastError) { | |
console.log(chrome.runtime.lastError.message) | |
} | |
console.log(message); | |
} | |
) | |
port.onDisconnect.addListener(()=>{ | |
if (chrome.runtime.lastError) { | |
console.log(chrome.runtime.lastError.message) | |
} | |
console.log('Disconnected') | |
} | |
) | |
port.postMessage(input) | |
} | |
)(); | |
chrome.runtime.sendNativeMessage('nm_bash', input) | |
.then((msg)=>console.log(msg)) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment