From 475737bd99487edf5a5423ce8fe06ec811ff1b98 Mon Sep 17 00:00:00 2001 From: Joscha Henningsen <44805696+joschahenningsen@users.noreply.github.com> Date: Sat, 14 Oct 2023 10:40:03 +0200 Subject: [PATCH] Don't allow duplicated pins. Fixes #277 (#279) --- js/pins.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/js/pins.js b/js/pins.js index 24da0aca..43750021 100644 --- a/js/pins.js +++ b/js/pins.js @@ -1,16 +1,21 @@ let pins = []; function removePin(elem) { - let remove = elem.parentElement.children[1].innerHTML - pins = pins.filter(pin => pin.n !== remove) + let removeN = elem.parentElement.children[1].innerHTML + let removeL = elem.parentElement.children[2].children[0].href + pins = pins.filter(pin => pin.n !== removeN && pin.l !== removeL) localStorage.setItem("pins", JSON.stringify(pins)) render() } function addPin(elem) { const parentContent = elem.parentElement.innerText - let pinName = parentContent.substr(0, parentContent.lastIndexOf(" — ")) - let pinUrl = parentContent.substr(parentContent.lastIndexOf(" — ") + 3, parentContent.length - (13 + parentContent.lastIndexOf(" — "))) + let pinName = parentContent.substring(0, parentContent.lastIndexOf(" — ")) + let pinUrl = parentContent.substring(parentContent.lastIndexOf(" — ") + 3, parentContent.length - (13 + parentContent.lastIndexOf(" — "))) + // make sure the pin is not already in the list + if (pins.filter(pin => pin.n === pinName && pin.l === pinUrl ).length > 0) { + return + } pins.push({"n": pinName, "l": pinUrl}) localStorage.setItem("pins", JSON.stringify(pins)) elem.parentElement.removeChild(elem)