From 423c752f06eeebaa1d190751ac91ca3fdcd9cf8d Mon Sep 17 00:00:00 2001
From: hackademix <giorgio@maone.net>
Date: Sep 28 2018 23:54:22 +0000
Subject: [PATCH 1/3] Lenient WebLabels matching (by URL, by id, by name).


---

diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js
index 27d9223..058b651 100644
--- a/bg/ExternalLicenses.js
+++ b/bg/ExternalLicenses.js
@@ -26,10 +26,27 @@
 "use strict";
 
 let licensesByLabel = new Map();
+let licensesByUrl = new Map();
 {
   let {licenses} = require("../license_definitions");
-  for (let l of Object.values(licenses).filter(l => l.identifier)) {
-    licensesByLabel.set(l.identifier, l);
+  for (let [id, l] of Object.entries(licenses)) {
+    let {identifier, canonicalUrl, licenseName} = l;
+    if (identifier) {
+      licensesByLabel.set(identifier, l);
+    } else {
+      l.identifier = id;
+    }
+    if (id !== identifier) {
+      licensesByLabel.set(id, l);
+    }
+    if (licenseName) {
+      licensesByLabel.set(licenseName, l);
+    }
+    if (Array.isArray(canonicalUrl)) {
+      for (let url of canonicalUrl) {
+        licensesByUrl.set(url, l);
+      }
+    }
   }
 }
 
@@ -55,23 +72,26 @@ var ExternalLicenses = {
       return null;
     }
     scriptInfo.licenses = new Set();
-    scriptInfo.allFree = true;
     scriptInfo.toString = function() {
       let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(", ");
       return licenseIds
-         ? (this.allFree ? `Free license${this.licenses.length > 1 ? "s" : ""} (${licenseIds})`
-                         : `Mixed free (${licenseIds}) and unknown licenses`)
+         ? `Free license${this.licenses.size > 1 ? "s" : ""} (${licenseIds})`
          : "Unknown license(s)";
     }
-
-    for (let {label} of scriptInfo.licenseLinks) {
-      if (licensesByLabel.has(label)) {
-        scriptInfo.licenses.add(licensesByLabel.get(label));
-      } else {
-        scriptInfo.allFree = false;
-        break;
+    let match = (map, key) => {
+      if (map.has(key)) {
+        scriptInfo.licenses.add(map.get(key));
+        return true;
       }
+      return false;
+    };
+
+    for (let {label, url} of scriptInfo.licenseLinks) {
+      match(licensesByLabel, label = label.trim()) ||
+        match(licensesByUrl, url) ||
+        match(licensesByLabel, label.replace(/^GNU-|-(?:or-later|only)$/i, ''));
     }
+    scriptInfo.free = scriptInfo.licenses.size > 0;
     return scriptInfo;
   },
 
diff --git a/main_background.js b/main_background.js
index a145cc5..f9c5dac 100644
--- a/main_background.js
+++ b/main_background.js
@@ -862,6 +862,7 @@ var ResponseHandler = {
 		let {request} = response;
 		let {url, type, tabId, frameId, documentUrl} = request;
 
+		let fullUrl = url;
 		url = ListStore.urlItem(url);
 		let site = ListStore.siteItem(url);
 
@@ -891,11 +892,11 @@ var ResponseHandler = {
 						"whitelisted": [url, whitelistedSite ? `User whitelisted ${site}` : "Whitelisted by user"]});
 					return ResponseProcessor.ACCEPT;
 				} else {
-					let scriptInfo = await ExternalLicenses.check({url, tabId, frameId, documentUrl});
+					let scriptInfo = await ExternalLicenses.check({url: fullUrl, tabId, frameId, documentUrl});
 					if (scriptInfo) {
 						let verdict, ret;
 						let msg = scriptInfo.toString();
-						if (scriptInfo.allFree) {
+						if (scriptInfo.free) {
 							verdict = "accepted";
 							ret = ResponseProcessor.ACCEPT;
 						} else {

From ffe9a7ab8625aba7a60e21f4c7f2c39081414697 Mon Sep 17 00:00:00 2001
From: hackademix <giorgio@maone.net>
Date: Sep 29 2018 22:12:17 +0000
Subject: [PATCH 2/3] WebLabels matching by id made case-insensitive.


---

diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js
index 058b651..33bcbf6 100644
--- a/bg/ExternalLicenses.js
+++ b/bg/ExternalLicenses.js
@@ -29,18 +29,19 @@ 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) {
-      licensesByLabel.set(identifier, l);
+      mapByLabel(identifier, l);
     } else {
       l.identifier = id;
     }
     if (id !== identifier) {
-      licensesByLabel.set(id, l);
+      mapByLabel(id, l);
     }
     if (licenseName) {
-      licensesByLabel.set(licenseName, l);
+      mapByLabel(licenseName, l);
     }
     if (Array.isArray(canonicalUrl)) {
       for (let url of canonicalUrl) {
@@ -87,9 +88,9 @@ var ExternalLicenses = {
     };
 
     for (let {label, url} of scriptInfo.licenseLinks) {
-      match(licensesByLabel, label = label.trim()) ||
+      match(licensesByLabel, label = label.trim().toUpperCase()) ||
         match(licensesByUrl, url) ||
-        match(licensesByLabel, label.replace(/^GNU-|-(?:or-later|only)$/i, ''));
+        match(licensesByLabel, label.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, ''));
     }
     scriptInfo.free = scriptInfo.licenses.size > 0;
     return scriptInfo;

From eae399dfd0ade951e9bc87897247bcffffee614f Mon Sep 17 00:00:00 2001
From: hackademix <giorgio@maone.net>
Date: Oct 02 2018 21:13:24 +0000
Subject: [PATCH 3/3] Match scripts to weblabels also without query strings.


---

diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js
index 181e3f9..516057e 100644
--- a/content/externalLicenseChecker.js
+++ b/content/externalLicenseChecker.js
@@ -79,7 +79,7 @@
     async checkLicensedScript(m) {
       let {url, cache} = m;
       if (!licensedScripts) licensedScripts = await fetchLicenseInfo(cache);
-      return licensedScripts.get(url);
+      return licensedScripts.get(url) || licensedScripts.get(url.replace(/\?.*/, ''));
     }
   }
 

