diff options
author | hackademix <giorgio@maone.net> | 2019-03-11 22:35:46 +0100 |
---|---|---|
committer | hackademix <giorgio@maone.net> | 2019-03-11 22:35:46 +0100 |
commit | 2a272e97c076c8a0f13a1f0f5140ad5d72cbd890 (patch) | |
tree | 68f21afb0693f6464ba12cad3d08621590c01a30 /bg/ResponseProcessor.js | |
parent | 42a2d31b272a2934824e038e8d9c73f19bab006c (diff) |
Correctly decode using the original document charset and force re-encoding via UTF-8 BOM only when needed.
Diffstat (limited to 'bg/ResponseProcessor.js')
-rw-r--r-- | bg/ResponseProcessor.js | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js index 4443d90..d10d46d 100644 --- a/bg/ResponseProcessor.js +++ b/bg/ResponseProcessor.js @@ -90,8 +90,6 @@ class ResponseTextFilter { }; filter.onstop = async event => { - - let params = {stream: true}; // concatenate chunks let size = buffer.reduce((sum, chunk, n) => sum + chunk.byteLength, 0) let allBytes = new Uint8Array(size); @@ -119,19 +117,19 @@ class ResponseTextFilter { } catch(e) { console.error(e); } - if (editedText !== null && - (metaData.forcedUTF8 && request.type !== "script" || - response.text !== editedText)) { - // if we changed the charset, the text or both, let's re-encode - filter.write(new TextEncoder().encode(editedText)); - } else { - // ... otherwise pass all the raw bytes through - filter.write(allBytes); + if (editedText !== null) { + // we changed the content, let's re-encode + let encoded = new TextEncoder().encode(editedText); + // pre-pending the UTF-8 BOM will force the charset per HTML 5 specs + allBytes = new Uint8Array(encoded.byteLength + 3); + allBytes.set(new Uint8Array([0xEF, 0xBB, 0xBF]), 0); // UTF-8 BOM + allBytes.set(encoded, 3); } + filter.write(allBytes); filter.close(); } - return metaData.forceUTF8() ? {responseHeaders} : ResponseProcessor.ACCEPT;; + return ResponseProcessor.ACCEPT; } } |