-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from dotproto/native-update
Update native-messaging to use python3, clarify instructions
- Loading branch information
Showing
5 changed files
with
93 additions
and
88 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
"browser_action": { | ||
"default_icon": "icons/message.svg" | ||
}, | ||
|
||
"permissions": ["nativeMessaging"] | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,36 @@ | ||
#!/usr/bin/env python | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
import json | ||
import struct | ||
|
||
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) | ||
|
||
# Encode a message for transmission, | ||
# given its content. | ||
def encodeMessage(messageContent): | ||
# https://docs.python.org/3/library/json.html#basic-usage | ||
# To get the most compact JSON representation, you should specify | ||
# (',', ':') to eliminate whitespace. | ||
# We want the most compact representation because the browser rejects | ||
# messages that exceed 1 MB. | ||
encodedContent = json.dumps(messageContent, separators=(',', ':')).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() | ||
|
||
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): | ||
# https://docs.python.org/3/library/json.html#basic-usage | ||
# To get the most compact JSON representation, you should specify | ||
# (',', ':') to eliminate whitespace. | ||
# We want the most compact representation because the browser rejects | ||
# messages that exceed 1 MB. | ||
encodedContent = json.dumps(messageContent, separators=(',', ':')) | ||
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")) | ||
# 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) | ||
|
||
# Encode a message for transmission, given its content. | ||
def encodeMessage(messageContent): | ||
# https://docs.python.org/3/library/json.html#basic-usage | ||
# To get the most compact JSON representation, you should specify | ||
# (',', ':') to eliminate whitespace. | ||
# We want the most compact representation because the browser rejects | ||
# messages that exceed 1 MB. | ||
encodedContent = json.dumps(messageContent, separators=(',', ':')).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() | ||
|
||
while True: | ||
receivedMessage = getMessage() | ||
if receivedMessage == "ping": | ||
sendMessage(encodeMessage("pong")) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
@echo off | ||
|
||
call python C:\path\to\ping_pong.py | ||
call python3 C:\path\to\ping_pong.py |