From a8d4d4b9ff1076e0755fa357870845323ae6c4a7 Mon Sep 17 00:00:00 2001 From: hackademix Date: Sep 02 2018 16:06:36 +0000 Subject: [PATCH 1/4] Fixes serialization of modified HTML documents erases DOCTYPE and root element information, possibly causing rendering issues. --- diff --git a/main_background.js b/main_background.js index e2c3635..8e4665a 100644 --- a/main_background.js +++ b/main_background.js @@ -931,6 +931,22 @@ async function handle_script(response, whitelisted){ } /** +* Serializes HTMLDocument objects including the root element and +* the DOCTYPE declaration +*/ +function doc2HTML(doc) { + let s = doc.documentElement.outerHTML; + if (doc.doctype) { + let dt = doc.doctype; + let sDoctype = `\n${s}`; + } + return s; +} + +/** * Removes noscript tags with name "librejs-path" leaving the inner content to load. */ function remove_noscripts(html_doc){ @@ -940,7 +956,7 @@ function remove_noscripts(html_doc){ } } - return html_doc.documentElement.innerHTML; + return doc2HTML(html_doc); } /** From 89985360b002aba1e6a9725109e37e38265f0096 Mon Sep 17 00:00:00 2001 From: hackademix Date: Sep 02 2018 16:09:49 +0000 Subject: [PATCH 2/4] Internal refactoring of htmlEdit() using idiomatic asynchronous ES for better maintainability and fixing problematic Promise usage. --- diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js index e3d6c45..1af8cd7 100644 --- a/bg/ResponseProcessor.js +++ b/bg/ResponseProcessor.js @@ -101,8 +101,9 @@ class ResponseTextFilter { } catch(e) { console.error(e); } - if (metaData.forcedUTF8 && request.type !== "script" || - editedText !== null && response.text !== editedText) { + 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 { diff --git a/main_background.js b/main_background.js index 8e4665a..8d732e4 100644 --- a/main_background.js +++ b/main_background.js @@ -1012,106 +1012,91 @@ function read_metadata(meta_element){ * Reads/changes the HTML of a page and the scripts within it. */ -function edit_html(html, documentUrl, tabId, frameId, whitelisted){ +async function editHtml(html, documentUrl, tabId, frameId, whitelisted){ + var parser = new DOMParser(); + var html_doc = parser.parseFromString(html, "text/html"); + + // moves external licenses reference, if any, before any