mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 14:59:12 +02:00
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
This commit is contained in:
@@ -1,34 +1,62 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
import json
|
import json
|
||||||
import struct
|
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.
|
# Encode a message for transmission,
|
||||||
def getMessage():
|
# given its content.
|
||||||
rawLength = sys.stdin.read(4)
|
def encodeMessage(messageContent):
|
||||||
if len(rawLength) == 0:
|
encodedContent = json.dumps(messageContent).encode('utf-8')
|
||||||
sys.exit(0)
|
encodedLength = struct.pack('@I', len(encodedContent))
|
||||||
messageLength = struct.unpack('@I', rawLength)[0]
|
return {'length': encodedLength, 'content': encodedContent}
|
||||||
message = sys.stdin.read(messageLength)
|
|
||||||
return json.loads(message)
|
|
||||||
|
|
||||||
|
# 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,
|
while True:
|
||||||
# given its content.
|
receivedMessage = getMessage()
|
||||||
def encodeMessage(messageContent):
|
if receivedMessage == "ping":
|
||||||
encodedContent = json.dumps(messageContent)
|
sendMessage(encodeMessage("pong3"))
|
||||||
encodedLength = struct.pack('@I', len(encodedContent))
|
except AttributeError:
|
||||||
return {'length': encodedLength, 'content': encodedContent}
|
# 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
|
# Send an encoded message to stdout
|
||||||
def sendMessage(encodedMessage):
|
def sendMessage(encodedMessage):
|
||||||
sys.stdout.write(encodedMessage['length'])
|
sys.stdout.write(encodedMessage['length'])
|
||||||
sys.stdout.write(encodedMessage['content'])
|
sys.stdout.write(encodedMessage['content'])
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
receivedMessage = getMessage()
|
receivedMessage = getMessage()
|
||||||
if receivedMessage == 'ping':
|
if receivedMessage == "ping":
|
||||||
sendMessage(encodeMessage('pong'))
|
sendMessage(encodeMessage("pong2"))
|
||||||
|
|||||||
Reference in New Issue
Block a user