moderator-toolbox-nxg-for-reddit / util/data/encoding

util/data/encoding

Functions

base64ToBytes()

base64ToBytes(text): Uint8Array

Defined in: extension/data/util/data/encoding.ts:123

Decodes a base64 string into raw bytes.

Parameters

text

string

Returns

Uint8Array


byteLength()

byteLength(text): number

Defined in: extension/data/util/data/encoding.ts:132

Returns the UTF-8 byte length of a string - the size it occupies once serialized, which is what Reddit’s wiki page limits are measured against.

Parameters

text

string

Returns

number


bytesToBase64()

bytesToBase64(bytes): string

Defined in: extension/data/util/data/encoding.ts:112

Encodes raw bytes as a base64 string. Chunked so large arrays don’t blow the argument-spread call stack limit.

Parameters

bytes

Uint8Array

Returns

string


htmlDecode()

htmlDecode(value): string

Defined in: extension/data/util/data/encoding.ts:67

Gets the text content of an HTML string. Uses the DOM where available (handles every entity); in a DOM-less context such as the background service worker it falls back to decoding the entities an HTML serializer emits plus numeric entities, so callers like util/data/purify!purify don’t throw when sanitizing API responses off the main thread.

Parameters

value

string

Returns

string


htmlEncode()

htmlEncode(value): string

Defined in: extension/data/util/data/encoding.ts:54

Escapes text for HTML using the browser’s DOM engine, which handles all HTML entities. Unlike util/data/string!escapeHTML, which uses a fixed char map for six common characters, this encodes comprehensively via textContent/innerHTML.

Parameters

value

string

Returns

string


~~legacyEscape()~~

legacyEscape(s): string

Defined in: extension/data/util/data/encoding.ts:32

Encodes a string with legacy escape() semantics for the classic (schema v1) wiki mirror. 6.x reads removal reason text, macro text, and the removal header/footer with an unconditional unescape(), so those fields must be written escape()-encoded - and specifically with escape() rather than encodeURIComponent(), because unescape() decodes %XX as Latin-1 and would mangle UTF-8 sequences; escape()’s %uXXXX form round-trips.

Parameters

s

string

Returns

string

Deprecated

Remove in 9.x along with tbDecode once classic-schema wiki mirrors are no longer written.


~~tbDecode()~~

tbDecode(s): string

Defined in: extension/data/util/data/encoding.ts:13

Decodes a string previously encoded with the legacy escape() function. Tries decodeURIComponent first (correct for new data encoded with encodeURIComponent), then falls back to unescape to handle old wiki data that may contain non-standard %uXXXX sequences.

Parameters

s

string

Returns

string

Deprecated

Remove in 9.x once legacy escape()-encoded wiki configs are no longer supported.


unescapeJSON()

unescapeJSON(val): string

Defined in: extension/data/util/data/encoding.ts:40

Reverses the HTML entity encoding Reddit applies to its JSON responses so the payload can be parsed.

Parameters

val

string

Returns

string


zlibDeflate()

zlibDeflate(objThing): string

Defined in: extension/data/util/data/encoding.ts:100

Compresses data with zlib and returns it as a base64-encoded string.

Parameters

objThing

string

Returns

string


zlibInflate()

zlibInflate(stringThing): string

Defined in: extension/data/util/data/encoding.ts:87

Decompresses a base64-encoded, zlib-compressed string back into its data.

Parameters

stringThing

string

Returns

string