Skip to content

Commit

Permalink
Merge pull request #84 from alandtse/3.12
Browse files Browse the repository at this point in the history
fix: fix handling of 3.12
  • Loading branch information
alandtse authored Mar 9, 2021
2 parents f4906bb + cb69362 commit f2c8e99
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 744 deletions.
15 changes: 8 additions & 7 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function handleMessage(request, sender, sendResponse) {
}
case "newTab": {
activeTabId = sender.tab.id;
console.log("tab %s: Creating new tab with %s:", activeTabId, request);
console.log("tab %s: Creating new tab with:", activeTabId, request);
browser.tabs.create({ url: "/pages/popup.html" }).then(
(tab) => {
console.log("Tab %s created", tab.id);
Expand Down Expand Up @@ -909,6 +909,13 @@ chrome.runtime.onInstalled.addListener(function (details) {
function init() {
console.log("Initializing spdx-license-diff " + version);
restoreOptions(loadList);
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
title: "License-Diff selection",
id: "spdxLicenseDiff",
contexts: ["selection"],
});
});
}

init();
Expand All @@ -919,10 +926,4 @@ chrome.tabs.onActivated.addListener(handleActivate);
chrome.windows.onFocusChanged.addListener(handleFocusChanged);
chrome.storage.onChanged.addListener(handleStorageChange);
chrome.tabs.onUpdated.addListener(handleUpdated);

chrome.contextMenus.onClicked.addListener(handleClick);
chrome.contextMenus.create({
title: "License-Diff selection",
contexts: ["selection"],
id: "contextId",
});
15 changes: 12 additions & 3 deletions app/scripts/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const defaultoptions = {
},
};

const baseLicenseUrl = "https://spdx.org/licenses/";

const urls = {
licenses: "https://spdx.org/licenses/licenses.json",
exceptions: "https://spdx.org/licenses/exceptions.json",
licenses: `${baseLicenseUrl}licenses.json`,
exceptions: `${baseLicenseUrl}exceptions.json`,
};

const newLicenseUrl = "https://tools.spdx.org/app/submit_new_license/";
Expand All @@ -36,4 +38,11 @@ const spdxkey = {
text: "licenseExceptionText",
},
};
export { filters, defaultoptions, urls, spdxkey, newLicenseUrl };
export {
filters,
defaultoptions,
urls,
spdxkey,
newLicenseUrl,
baseLicenseUrl,
};
58 changes: 44 additions & 14 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (!diffdisplayed)
displayDiff(diffs[bestspdxid].html, diffs[bestspdxid].time);
updateBubbleText("Diffing done");
document.getElementById("newTabButton").style.visibility = "visible";
} else if (bestspdxid === spdxid) {
console.log("Best diff %s received; we can display", bestspdxid);
if (!diffdisplayed)
Expand All @@ -117,17 +118,20 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
break;
case "newTab":
diffs = request.diffs;
spdxid = request.spdxid;
spdx = request.spdx;
console.log("Received newTab request", diffs, spdxid, spdx);
selectedLicense =
typeof selectedLicense !== "undefined"
? request.selectedLicense
: spdx[0].spdxid;
console.log("Received newTab request", request);
updateProgressBar(1, 1, false);
addSelectFormFromArray(
"licenses",
spdx,
options.showBest === 0 && spdx ? spdx.length : options.showBest,
options.minpercentage
);
displayDiff(diffs[spdxid].html, diffs[spdxid].time);
displayDiff(diffs[selectedLicense].html, diffs[selectedLicense].time);
break;
case "alive?":
console.log("Received ping request");
Expand Down Expand Up @@ -283,8 +287,18 @@ function displayDiff(html, time = processTime) {
}
var spdxid = spdx[0].spdxid;
var details = spdx[0].details;
if (selectedLicense) {
for (var index in spdx) {
if (spdx[index].spdxid === selectedLicense) {
spdxid = selectedLicense;
details = spdx[index].details;
break;
}
}
}
updateBubbleText(prepDiff(spdxid, time, html, details), "#result_text");
document.getElementById("licenses").addEventListener(
var licenseElement = document.getElementById("licenses");
licenseElement.addEventListener(
"change",
function () {
if (this.value !== selectedLicense) {
Expand All @@ -294,11 +308,17 @@ function displayDiff(html, time = processTime) {
time = diffs[spdxid].time;
details = spdx[this.options.selectedIndex].details;
updateBubbleText(prepDiff(spdxid, time, html, details), "#result_text");
createNewTabButton(
document.getElementById("license_form"),
selectedLicense
);
} else {
}
},
false
);
licenseElement.value = selectedLicense;
licenseElement.dispatchEvent(new Event("change"));
}

// This wraps the diff display
Expand Down Expand Up @@ -418,7 +438,7 @@ function addSelectFormFromArray(id, arr, number = arr.length, minimum = 0) {
}
}
createNewLicenseButton(form);
createNewTabButton(form, option.value);
createNewTabButton(form, selectedLicense);
}

// Display helper functions for modifying the DOM
Expand Down Expand Up @@ -483,23 +503,33 @@ function createNewLicenseButton(form) {
}

// Add new tab button.
function createNewTabButton(form, spdxid) {
if ($("#newTabButton").length) return;
function createNewTabButton(form, selectedLicense) {
if (window.location.href.endsWith("/popup.html")) return;
if ($("#newTabButton").length) {
document
.getElementById("newTabButton")
.removeEventListener("click", newTab);
document.getElementById("newTabButton").addEventListener("click", newTab);
return;
}
var button = document.createElement("button");
button.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18"><path d="M4.5 11H3v4h4v-1.5H4.5V11zM3 7h1.5V4.5H7V3H3v4zm10.5 6.5H11V15h4v-4h-1.5v2.5zM11 3v1.5h2.5V7H15V3h-4z"/></svg>';
button.type = "button";
button.id = "newTabButton";
button.style.visibility = "hidden";
form.appendChild(button);
form.appendChild(document.createElement("br"));
button.addEventListener("click", function () {
chrome.runtime.sendMessage({
command: "newTab",
diffs: diffs,
spdxid: spdxid,
spdx: spdx,
});
button.addEventListener("click", newTab);
}

function newTab() {
var license = selectedLicense;
chrome.runtime.sendMessage({
command: "newTab",
diffs: diffs,
selectedLicense: license,
spdx: spdx,
});
}

Expand Down
13 changes: 11 additions & 2 deletions app/scripts/worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: Alan D. Tse <[email protected]>
// SPDX-License-Identifier: (GPL-3.0-or-later AND Apache-2.0)
import { urls, spdxkey } from "./const.js";
import { baseLicenseUrl, urls, spdxkey } from "./const.js";
import DiffMatchPatch from "diff-match-patch";
import * as fastestlevenshtein from "fastest-levenshtein";
import dice from "fast-dice-coefficient";
Expand Down Expand Up @@ -76,8 +76,17 @@ async function getSPDXlist() {
});
console.log(id, "Updating: ", result);
result[type] = result[type].map(async (item) => {
url = item.detailsUrl;
var urlRegex = new RegExp("^(?:[a-z]+:)?//", "i");
if (urlRegex.test(url)) {
url = url.replace("http:", "https:");
} else {
url = `${baseLicenseUrl}${url}`;
}
if (url.split(".").pop().toLowerCase() === "html")
url = url.replace(".html", ".json");
try {
return await getJSON(item.detailsUrl.replace("http:", "https:"));
return await getJSON(url);
} catch (err) {
console.log("Error in Update");
throw err;
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"prettier": "^2.0.4",
"semantic-release": "^17",
"semantic-release-chrome": "^1.1.3",
"semantic-release-firefox-add-on": "^0.2.5",
"semantic-release-firefox": "^2.0.3",
"standard": "^14.3.3",
"web-ext": "^4.1.0",
"webextension-toolbox": "latest"
Expand All @@ -50,7 +50,6 @@
"fast-dice-coefficient": "^1.0.3",
"fastest-levenshtein": "^1.0.12",
"jquery": "^3.2.1",
"md5-jkmyers": "^0.0.1",
"underscore": "^1.9.1"
},
"release": {
Expand Down Expand Up @@ -103,6 +102,15 @@
"asset": "spdx-license-diff.chrome.zip",
"extensionId": "kfoadicmilbgnicoldjmccpaicejacdh"
}
],
[
"semantic-release-firefox",
{
"distFolder": "dist/firefox",
"xpiPath": "spdx-license-diff.firefox.xpi.zip",
"addOnSlug": "spdx-license-diff",
"notesToReviewer": "Please see https://github.com/spdx/spdx-license-diff/blob/master/DEVELOPERS.md for compile instructions."
}
]
]
}
Expand Down
Loading

0 comments on commit f2c8e99

Please sign in to comment.