From e56023afced93d5620a07fcbdfc0075d1016ba14 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 22 Sep 2022 16:36:44 +1000 Subject: refactor editHtml --- content/externalLicenseChecker.js | 2 +- main_background.js | 69 ++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js index 91377b3..cfcc897 100644 --- a/content/externalLicenseChecker.js +++ b/content/externalLicenseChecker.js @@ -37,7 +37,7 @@ if (!response.ok) throw `${response.status} ${response.statusText}`; const doc = new DOMParser().parseFromString( await response.text(), - "text/html" + 'text/html' ); const link = a => ({ url: a.href, label: a.textContent }); const firstLink = parent => link(parent.querySelector("a")); diff --git a/main_background.js b/main_background.js index fd11753..0cdb1c1 100644 --- a/main_background.js +++ b/main_background.js @@ -625,31 +625,31 @@ function showConditionalElements(doc) { * Tests to see if the intrinsic events on the page are free or not. * returns true if they are, false if they're not */ -function read_metadata(meta_element) { +function readMetadata(metaElement) { - if (meta_element === undefined || meta_element === null) { - return; + if (metaElement === undefined || metaElement === null) { + return false; } console.log('metadata found'); - var metadata = {}; + let metadata = {}; try { - metadata = JSON.parse(meta_element.innerHTML); + metadata = JSON.parse(metaElement.innerHTML); } catch (error) { console.log('Could not parse metadata on page.') return false; } - var license_str = metadata['intrinsic-events']; - if (license_str === undefined) { + const licenseStr = metadata['intrinsic-events']; + if (licenseStr === undefined) { console.log('No intrinsic events license'); return false; } - console.log(license_str); + console.log(licenseStr); - var parts = license_str.split(' '); + const parts = licenseStr.split(' '); if (parts.length != 2) { console.log('invalid (>2 tokens)'); return false; @@ -663,52 +663,47 @@ function read_metadata(meta_element) { } } /** - -* Reads/changes the HTML of a page and the scripts within it. -*/ + * Reads/changes the HTML of a page and the scripts within it. + * Returns string or null. + */ async function editHtml(html, documentUrl, tabId, frameId, whitelisted) { - var parser = new DOMParser(); - var html_doc = parser.parseFromString(html, 'text/html'); + const htmlDoc = new DOMParser().parseFromString(html, 'text/html'); // moves external licenses reference, if any, before any