From 89985360b002aba1e6a9725109e37e38265f0096 Mon Sep 17 00:00:00 2001 From: hackademix Date: Sun, 2 Sep 2018 18:09:49 +0200 Subject: Internal refactoring of htmlEdit() using idiomatic asynchronous ES for better maintainability and fixing problematic Promise usage. --- bg/ResponseProcessor.js | 5 +- main_background.js | 159 ++++++++++++++++++++++-------------------------- 2 files changed, 75 insertions(+), 89 deletions(-) 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