From af903283f05e628ca2e25dfb8e2745baee945b7f Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 22 Sep 2022 12:54:07 +1000 Subject: clean up external license checking a bit --- bg/ExternalLicenses.js | 7 +------ content/externalLicenseChecker.js | 13 ++++++++++++- main_background.js | 5 ++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js index 1196602..01894b1 100644 --- a/bg/ExternalLicenses.js +++ b/bg/ExternalLicenses.js @@ -48,6 +48,7 @@ const ExternalLicenses = { cachedHrefs.delete(tabId); }, + // Checks external script using web labels async check(script) { const { url, tabId, frameId, documentUrl } = script; const tabCache = cachedHrefs.get(tabId); @@ -69,12 +70,6 @@ const ExternalLicenses = { licensesByLabel.get(uLabel.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); return license ? [license] : []; }).flat()); - scriptInfo.toString = () => { - const licenseIds = [...this.licenses].map(l => l.identifier).sort().join(', '); - return licenseIds - ? `Free license${this.licenses.size > 1 ? 's' : ''} (${licenseIds})` - : 'Unknown license(s)'; - } scriptInfo.free = scriptInfo.licenses.size > 0; return scriptInfo; }, diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js index 70a763c..91377b3 100644 --- a/content/externalLicenseChecker.js +++ b/content/externalLicenseChecker.js @@ -21,8 +21,14 @@ */ "use strict"; { + // Find and fetch the url to the web labels table, and returns the + // license info in the table as a map from the script url to + // { script: {url, label}, + // licenseLinks: [{url, label}], + // sources: [{url, label}] } + // + // see https://www.gnu.org/software/librejs/free-your-javascript.html#step3 const fetchWebLabels = async args => { - // see https://www.gnu.org/software/librejs/free-your-javascript.html#step3 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); @@ -69,6 +75,11 @@ } const handlers = { + // Look up the script url in the web labels and return it if found, + // otherwise return undefined. The found value is of the format + // { script: {url, label}, + // licenseLinks: [{url, label}], + // sources: [{url, label}] } async checkLicensedScript(m) { const { url, cache } = m; const licensedScripts = await fetchLicenseInfo(cache); diff --git a/main_background.js b/main_background.js index 4f164f4..c9ed6f2 100644 --- a/main_background.js +++ b/main_background.js @@ -858,7 +858,10 @@ var ResponseHandler = { const scriptInfo = await ExternalLicenses.check({ url: fullUrl, tabId, frameId, documentUrl }); if (scriptInfo) { const [verdict, ret] = scriptInfo.free ? ['accepted', ResponseProcessor.ACCEPT] : ['blocked', ResponseProcessor.REJECT]; - const msg = scriptInfo.toString(); + const licenseIds = [...scriptInfo.licenses].map(l => l.identifier).sort().join(', '); + const msg = licenseIds + ? `Free license${scriptInfo.licenses.size > 1 ? 's' : ''} (${licenseIds})` + : 'Unknown license(s)'; addReportEntry(tabId, { url, [verdict]: [url, msg] }); return ret; } -- cgit v1.2.3