From ae1c2176ea0d7df04778ee2d46503223ca436289 Mon Sep 17 00:00:00 2001 From: evolighting <405327973@qq.com> Date: Fri, 28 Apr 2017 04:22:07 +0800 Subject: [PATCH 1/2] Fix native message examples with python3 (#157) Provide a single .py that handles both Python 2.x and Python 3.x * Fix python3 * Use one Python file to handle both Python 3.x and Python 2.x * Actually add the new file this time * Remove unnecessary parentheses --- native-messaging/app/ping_pong.py | 76 +++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/native-messaging/app/ping_pong.py b/native-messaging/app/ping_pong.py index 00d8697..c987f60 100755 --- a/native-messaging/app/ping_pong.py +++ b/native-messaging/app/ping_pong.py @@ -1,34 +1,62 @@ #!/usr/bin/env python + +import sys import json import struct -import sys +try: + # Python 3.x version + # Read a message from stdin and decode it. + def getMessage(): + rawLength = sys.stdin.buffer.read(4) + if len(rawLength) == 0: + sys.exit(0) + messageLength = struct.unpack('@I', rawLength)[0] + message = sys.stdin.buffer.read(messageLength).decode('utf-8') + return json.loads(message) -# 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).encode('utf-8') + encodedLength = struct.pack('@I', len(encodedContent)) + return {'length': encodedLength, 'content': encodedContent} + # Send an encoded message to stdout + def sendMessage(encodedMessage): + sys.stdout.buffer.write(encodedMessage['length']) + sys.stdout.buffer.write(encodedMessage['content']) + sys.stdout.buffer.flush() -# 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} + 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() + # 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('pong')) + while True: + receivedMessage = getMessage() + if receivedMessage == "ping": + sendMessage(encodeMessage("pong2")) From cf7b2a6499c6d0b169f72f4f2787763791410fec Mon Sep 17 00:00:00 2001 From: kabir-plod Date: Sun, 30 Apr 2017 12:44:14 +0800 Subject: [PATCH 2/2] Fix headline formatting in mocha-client-tests README (#213) --- mocha-client-tests/README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mocha-client-tests/README.md b/mocha-client-tests/README.md index ee72c2a..d66c3b4 100644 --- a/mocha-client-tests/README.md +++ b/mocha-client-tests/README.md @@ -1,12 +1,12 @@ -#Mocha client tests for WebExtensions -##Introduction +# Mocha client tests for WebExtensions +## Introduction This example shows two methods of testing a WebExtension: * Running tests from within the addon * Running tests from the commandline using Karma See https://github.com/Standard8/example-webextension for a more complete example of WebExtension test configuration. -##Install Dependencies: +## Install Dependencies: ``` npm install ``` @@ -16,11 +16,11 @@ To run tests from within the addon: npm install ``` -##Testing within the Addon +## Testing within the Addon This gives you the possibility to run client tests inside the addon with the mocha UI. If you don't want to use the mocha UI, you can install [WebConsole-reporter](https://github.com/eeroan/WebConsole-reporter). -###Run with web-ext cli +### Run with web-ext cli Just run `npm run web-ext` (will work with FF dev edition), if you have error with web-ext cli please add path for FF binary file with `--firefox-binary /path/to/firefox-bin` [(web-ext docs)](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/web-ext_command_reference). @@ -30,10 +30,10 @@ When the addon starts, click on the mocha icon in your browser bar to run the te This will test `./addon/background.js` with `./addon/tests/lib/background-messaging.test.js`. -##Testing from the Commandline +## Testing from the Commandline This uses [Karma](http://karma-runner.github.io) to run tests from the commandline. Just type `npm test` to test `./addon/background.js` with `./tests/lib/background.test.js`. -###Debug Mode +### Debug Mode Use `npm run test:debug` to run Karma in watch mode. Whenever you modify a Javascript file, the tests will automatically rerun. You can install [karma-notification-reporter](https://www.npmjs.com/package/karma-notification-reporter) to display test results in a desktop notification. You'll need to add `--reporters=dots,notification` to the `test:debug` command line of