From d67e8f8bf761b2428ad403613c31f257c871820a Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 1 May 2022 09:15:07 -0700 Subject: [PATCH 1/5] Substitute about:debugging#/runtime/this-firefox for about:debugging Extensions are loaded at about:debugging#/runtime/this-firefox not about:debugging --- native-messaging/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native-messaging/README.md b/native-messaging/README.md index 6cd7287d..3933b32f 100644 --- a/native-messaging/README.md +++ b/native-messaging/README.md @@ -25,7 +25,7 @@ To assist in troubleshooting on Windows, there is a script called `check_config_ ## Testing the example ## -Then just install the add-on as usual, by visiting about:debugging, clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json". +Then just install the add-on as usual, by visiting about:debugging#/runtime/this-firefox, clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json". Now, open the extension's console using the "Inspect" button - this is where you'll see communication between the browser and native app. From 621a24f4a0288989821549667636be7c98b2cfd0 Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 1 May 2022 10:05:28 -0700 Subject: [PATCH 2/5] Drop Python 2 support. Fixes #489 --- native-messaging/app/ping_pong.py | 43 +++++++++---------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/native-messaging/app/ping_pong.py b/native-messaging/app/ping_pong.py index c987f602..a4b498d7 100755 --- a/native-messaging/app/ping_pong.py +++ b/native-messaging/app/ping_pong.py @@ -1,4 +1,9 @@ -#!/usr/bin/env python +#!/usr/bin/env -S python3 -u +# https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging +# https://github.com/mdn/webextensions-examples/pull/157 +# Note that running python with the `-u` flag is required on Windows, +# in order to ensure that stdin and stdout are opened in binary, rather +# than text, mode. import sys import json @@ -13,7 +18,7 @@ def getMessage(): sys.exit(0) messageLength = struct.unpack('@I', rawLength)[0] message = sys.stdin.buffer.read(messageLength).decode('utf-8') - return json.loads(message) + return json.loads(message)["message"] # Encode a message for transmission, # given its content. @@ -31,32 +36,8 @@ def sendMessage(encodedMessage): while True: receivedMessage = getMessage() if receivedMessage == "ping": - sendMessage(encodeMessage("pong3")) -except AttributeError: - # Python 2.x version (if sys.stdin.buffer is not defined) - # Read a message from stdin and decode it. - def getMessage(): - rawLength = sys.stdin.read(4) - if len(rawLength) == 0: - sys.exit(0) - messageLength = struct.unpack('@I', rawLength)[0] - message = sys.stdin.read(messageLength) - return json.loads(message) - - # Encode a message for transmission, - # given its content. - def encodeMessage(messageContent): - encodedContent = json.dumps(messageContent) - encodedLength = struct.pack('@I', len(encodedContent)) - return {'length': encodedLength, 'content': encodedContent} - - # Send an encoded message to stdout - def sendMessage(encodedMessage): - sys.stdout.write(encodedMessage['length']) - sys.stdout.write(encodedMessage['content']) - sys.stdout.flush() - - while True: - receivedMessage = getMessage() - if receivedMessage == "ping": - sendMessage(encodeMessage("pong2")) + sendMessage(encodeMessage({"message":"pong3"})) +except Exception as e: + sys.stdout.buffer.flush() + sys.stdin.buffer.flush() + sys.exit(0) From 95b9d160b4b1306795ccc3a30cd13c375b1f029e Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 1 May 2022 17:32:43 -0700 Subject: [PATCH 3/5] Update README.md --- native-messaging/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native-messaging/README.md b/native-messaging/README.md index 3933b32f..15ae6def 100644 --- a/native-messaging/README.md +++ b/native-messaging/README.md @@ -25,7 +25,7 @@ To assist in troubleshooting on Windows, there is a script called `check_config_ ## Testing the example ## -Then just install the add-on as usual, by visiting about:debugging#/runtime/this-firefox, clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json". +Then just install the add-on as usual, by visiting `about:debugging#/runtime/this-firefox` or click on the button to debug "This Firefox" , clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json". Now, open the extension's console using the "Inspect" button - this is where you'll see communication between the browser and native app. From b41c86e3c92f607654e08499159e6b09925a3413 Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sun, 1 May 2022 17:42:08 -0700 Subject: [PATCH 4/5] Revert to https://github.com/mdn/webextensions-examples/commit/ae1c2176ea0d7df04778ee2d46503223ca436289 --- native-messaging/app/ping_pong.py | 43 ++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/native-messaging/app/ping_pong.py b/native-messaging/app/ping_pong.py index a4b498d7..c987f602 100755 --- a/native-messaging/app/ping_pong.py +++ b/native-messaging/app/ping_pong.py @@ -1,9 +1,4 @@ -#!/usr/bin/env -S python3 -u -# https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging -# https://github.com/mdn/webextensions-examples/pull/157 -# Note that running python with the `-u` flag is required on Windows, -# in order to ensure that stdin and stdout are opened in binary, rather -# than text, mode. +#!/usr/bin/env python import sys import json @@ -18,7 +13,7 @@ def getMessage(): sys.exit(0) messageLength = struct.unpack('@I', rawLength)[0] message = sys.stdin.buffer.read(messageLength).decode('utf-8') - return json.loads(message)["message"] + return json.loads(message) # Encode a message for transmission, # given its content. @@ -36,8 +31,32 @@ def sendMessage(encodedMessage): while True: receivedMessage = getMessage() if receivedMessage == "ping": - sendMessage(encodeMessage({"message":"pong3"})) -except Exception as e: - sys.stdout.buffer.flush() - sys.stdin.buffer.flush() - sys.exit(0) + sendMessage(encodeMessage("pong3")) +except AttributeError: + # Python 2.x version (if sys.stdin.buffer is not defined) + # Read a message from stdin and decode it. + def getMessage(): + rawLength = sys.stdin.read(4) + if len(rawLength) == 0: + sys.exit(0) + messageLength = struct.unpack('@I', rawLength)[0] + message = sys.stdin.read(messageLength) + return json.loads(message) + + # Encode a message for transmission, + # given its content. + def encodeMessage(messageContent): + encodedContent = json.dumps(messageContent) + encodedLength = struct.pack('@I', len(encodedContent)) + return {'length': encodedLength, 'content': encodedContent} + + # Send an encoded message to stdout + def sendMessage(encodedMessage): + sys.stdout.write(encodedMessage['length']) + sys.stdout.write(encodedMessage['content']) + sys.stdout.flush() + + while True: + receivedMessage = getMessage() + if receivedMessage == "ping": + sendMessage(encodeMessage("pong2")) From 5b7f5678dee11721f1ed0452dd11a0965e7f1e7c Mon Sep 17 00:00:00 2001 From: guest271314 Date: Mon, 5 Jun 2023 17:04:51 -0700 Subject: [PATCH 5/5] Update native-messaging/README.md Co-authored-by: rebloor --- native-messaging/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native-messaging/README.md b/native-messaging/README.md index 15ae6def..60fce8a0 100644 --- a/native-messaging/README.md +++ b/native-messaging/README.md @@ -25,7 +25,7 @@ To assist in troubleshooting on Windows, there is a script called `check_config_ ## Testing the example ## -Then just install the add-on as usual, by visiting `about:debugging#/runtime/this-firefox` or click on the button to debug "This Firefox" , clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json". +First, install the add-on. Visit `about:debugging#/runtime/this-firefox` or, from `about:debugging` click "This Firefox" (or "This Nightly" in the Nightly version of Firefox), click "Load Temporary Add-on", and open the add-on's "manifest.json". Now, open the extension's console using the "Inspect" button - this is where you'll see communication between the browser and native app.