Skip to content

Commit

Permalink
fix: fix newtab to open selected item
Browse files Browse the repository at this point in the history
Signed-off-by: Alan D. Tse <alandtse@gmail.com>
  • Loading branch information
alandtse committed Mar 9, 2021
1 parent f3423b9 commit c0432dc
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions app/scripts/contentscript.js
Expand Up @@ -117,17 +117,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 +286,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 @@ -303,6 +316,8 @@ function displayDiff(html, time = processTime) {
},
false
);
licenseElement.value = selectedLicense;
licenseElement.dispatchEvent(new Event("change"));
}

// This wraps the diff display
Expand Down Expand Up @@ -487,19 +502,13 @@ function createNewLicenseButton(form) {
}

// Add new tab button.
function createNewTabButton(form, spdxid) {
function createNewTabButton(form, selectedLicense) {
if (window.location.href.endsWith("/popup.html")) return;
if ($("#newTabButton").length) {
document
.getElementById("newTabButton")
.addEventListener("click", function () {
chrome.runtime.sendMessage({
command: "newTab",
diffs: diffs,
spdxid: spdxid,
spdx: spdx,
});
});
.removeEventListener("click", newTab);
document.getElementById("newTabButton").addEventListener("click", newTab);
return;
}
var button = document.createElement("button");
Expand All @@ -509,13 +518,16 @@ function createNewTabButton(form, spdxid) {
button.id = "newTabButton";
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

0 comments on commit c0432dc

Please sign in to comment.