From 136cb7354a7cb3dbc684c2b28c80ed9351c0146c Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 28 Jul 2022 16:50:14 +1000 Subject: Clean up externalLicenseChecker.js Tests still pass --- content/externalLicenseChecker.js | 50 ++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 29 deletions(-) (limited to 'content/externalLicenseChecker.js') diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js index 600a501..11feeec 100644 --- a/content/externalLicenseChecker.js +++ b/content/externalLicenseChecker.js @@ -20,35 +20,27 @@ */ "use strict"; { - let licensedScripts = null; - - let fetchWebLabels = async args => { + const fetchWebLabels = async args => { // see https://www.gnu.org/software/librejs/free-your-javascript.html#step3 - let { map, cache } = args; - let link = document.querySelector(`link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]`); - let baseURL = link ? link.href : cache.webLabels && new URL(cache.webLabels.href, document.baseURI); + const { map, cache } = args; + const link = document.querySelector(`link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]`); + const baseURL = link ? link.href : cache.webLabels && new URL(cache.webLabels.href, document.baseURI); if (baseURL) try { - let response = await fetch(baseURL); + const response = await fetch(baseURL); if (!response.ok) throw `${response.status} ${response.statusText}`; - let doc = new DOMParser().parseFromString( + const doc = new DOMParser().parseFromString( await response.text(), "text/html" ); - let base = doc.querySelector("base"); - if (base) { - base.href = base.href; - } else { - doc.head.appendChild(doc.createElement("base")).href = baseURL; - } - let link = a => ({ url: a.href, label: a.textContent }); - let firstLink = parent => link(parent.querySelector("a")); - let allLinks = parent => Array.prototype.map.call(parent.querySelectorAll("a"), link); - for (let row of doc.querySelectorAll("table#jslicense-labels1 > tbody > tr")) { + const link = a => ({ url: a.href, label: a.textContent }); + const firstLink = parent => link(parent.querySelector("a")); + const allLinks = parent => Array.prototype.map.call(parent.querySelectorAll("a"), link); + for (const row of doc.querySelectorAll("table#jslicense-labels1 > tbody > tr")) { try { - let cols = row.querySelectorAll("td"); - let script = firstLink(cols[0]); - let licenseLinks = allLinks(cols[1]); - let sources = cols[2] ? allLinks(cols[2]) : []; + const cols = row.querySelectorAll("td"); + const script = firstLink(cols[0]); + const licenseLinks = allLinks(cols[1]); + const sources = cols[2] ? allLinks(cols[2]) : []; map.set(script.url, { script, licenseLinks, sources }); } catch (e) { console.error("LibreJS: error parsing Web Labels at %s, row %s", baseURL, row.innerHTML, e); @@ -60,9 +52,9 @@ return map; } - let fetchLicenseInfo = async cache => { - let map = new Map(); - let args = { map, cache }; + const fetchLicenseInfo = async cache => { + const map = new Map(); + const args = { map, cache }; // in the fetchXxx methods we add to a map whatever license(s) // URLs and source code references we can find in various formats // (WebLabels is currently the only implementation), keyed by script URLs. @@ -75,10 +67,10 @@ return map; } - let handlers = { + const handlers = { async checkLicensedScript(m) { - let { url, cache } = m; - if (!licensedScripts) licensedScripts = await fetchLicenseInfo(cache); + const { url, cache } = m; + const licensedScripts = await fetchLicenseInfo(cache); return licensedScripts.get(url) || licensedScripts.get(url.replace(/\?.*/, '')); } } @@ -86,7 +78,7 @@ browser.runtime.onMessage.addListener(async m => { if (m.action in handlers) try { debug("Received message", m); - let result = await handlers[m.action](m); + const result = await handlers[m.action](m); return result; } catch (e) { console.error(e); -- cgit v1.2.3