mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Eliminate space counted as message length (#510)
This commit is contained in:
@@ -18,7 +18,12 @@ try:
|
||||
# Encode a message for transmission,
|
||||
# given its content.
|
||||
def encodeMessage(messageContent):
|
||||
encodedContent = json.dumps(messageContent).encode('utf-8')
|
||||
# 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}
|
||||
|
||||
@@ -46,7 +51,12 @@ except AttributeError:
|
||||
# Encode a message for transmission,
|
||||
# given its content.
|
||||
def encodeMessage(messageContent):
|
||||
encodedContent = json.dumps(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}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user