diff options
-rw-r--r-- | bg/ExternalLicenses.js | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js index 1a8e58c..fa02ae5 100644 --- a/bg/ExternalLicenses.js +++ b/bg/ExternalLicenses.js @@ -25,45 +25,43 @@ 'use strict'; -let licensesByLabel = new Map(); -let licensesByUrl = new Map(); -{ - let { licenses } = require('../license_definitions'); - let mapByLabel = (label, license) => licensesByLabel.set(label.toUpperCase(), license); - for (let [id, l] of Object.entries(licenses)) { - let { identifier, canonicalUrl, licenseName } = l; - if (identifier) { - mapByLabel(identifier, l); - } else { - l.identifier = id; - } - if (id !== identifier) { - mapByLabel(id, l); - } - if (licenseName) { - mapByLabel(licenseName, l); - } - if (Array.isArray(canonicalUrl)) { - for (let url of canonicalUrl) { - licensesByUrl.set(url, l); - } - } +const { licenses } = require('../license_definitions') +const licensesByLabel = Object.entries(licenses).reduce((acc, [id, license]) => { + return { + ...acc, + [license.identifier.toUpperCase()]: license, + [id.toUpperCase()]: license, + [license.licenseName.toUpperCase()]: license + }; +}, {}); +const licensesByUrl = Object.entries(licenses).reduce((acc, [_, license]) => { + return { + ...acc, + ...(license.canonicalUrl.reduce((result, url) => { + return { ...result, [url]: license } + }, {})) + }; +}, {}); + +for (const [id, license] of Object.entries(licenses)) { + if (!license.identifier) { + license.identifier = id; } } -let cachedHrefs = new Map(); +const cachedHrefs = new Map(); -var ExternalLicenses = { +const ExternalLicenses = { purgeCache(tabId) { cachedHrefs.delete(tabId); }, async check(script) { - let { url, tabId, frameId, documentUrl } = script; - let tabCache = cachedHrefs.get(tabId); - let frameCache = tabCache && tabCache.get(frameId); - let cache = frameCache && frameCache.get(documentUrl); - let scriptInfo = await browser.tabs.sendMessage(tabId, { + const { url, tabId, frameId, documentUrl } = script; + const tabCache = cachedHrefs.get(tabId); + const frameCache = tabCache && tabCache.get(frameId); + const cache = frameCache && frameCache.get(documentUrl); + const scriptInfo = await browser.tabs.sendMessage(tabId, { action: 'checkLicensedScript', url, cache, @@ -73,13 +71,14 @@ var ExternalLicenses = { return null; } scriptInfo.licenses = new Set(); - scriptInfo.toString = function() { - let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(', '); + 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)'; } - let match = (map, key) => { + const match = (map, key) => { + console.debug(map); if (map.has(key)) { scriptInfo.licenses.add(map.get(key)); return true; @@ -87,10 +86,11 @@ var ExternalLicenses = { return false; }; - for (let { label, url } of scriptInfo.licenseLinks) { - match(licensesByLabel, label = label.trim().toUpperCase()) || + for (const { label, url } of scriptInfo.licenseLinks) { + const uLabel = label.trim().toUpperCase(); + match(licensesByLabel, uLabel) || match(licensesByUrl, url) || - match(licensesByLabel, label.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); + match(licensesByLabel, uLabel.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); } scriptInfo.free = scriptInfo.licenses.size > 0; return scriptInfo; |