From a1f575cdb01752fcd496d1a5bde011218267e087 Mon Sep 17 00:00:00 2001 From: dthung1602 Date: Fri, 20 May 2022 22:03:47 +0700 Subject: [PATCH] Add ban domain button --- scripts/clean-spams.js | 36 +++++++++++++++++++++++++++++- scripts/service-worker.js | 4 ++-- styles/facebook-comment-plugin.css | 4 ++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/scripts/clean-spams.js b/scripts/clean-spams.js index b2f89ac..ff5a850 100644 --- a/scripts/clean-spams.js +++ b/scripts/clean-spams.js @@ -65,7 +65,7 @@ const restoreText = "⟲ Restore comment"; const hideText = "⟳ Hide spam" function createRestoreButton(node, restoreKey) { - const restoreButton = htmlToElement(`${restoreText}`); + const restoreButton = htmlToElement(`${restoreText}`); restoreButton.addEventListener("click", () => { swapStoredContent(node, restoreKey); restoreButton.innerText = restoreButton.innerText === restoreText ? hideText : restoreText; @@ -73,6 +73,16 @@ function createRestoreButton(node, restoreKey) { return restoreButton; } +function createBlackListButton(spamDomain) { + const button = htmlToElement(`✖ Blacklist domain`); + button.addEventListener("click", async () => { + let { banDomains } = await browser.storage.local.get(["banDomains"]); + banDomains = Array.from(new Set([...banDomains, spamDomain])); + await browser.storage.local.set({ banDomains }); + }); + return button; +} + // Actions function remove(node) { @@ -170,8 +180,32 @@ async function cleanSpams() { .map(count); console.log(`Found ${spamCount} spams`); + + addBlackListButtons(); } +function addBlackListButtons() { + let links = window.document.querySelectorAll("._3-8y:not(.spam-comment-root) ._5mdd a"); + links = Array.from(new Set(links)); + links + .forEach((node) => { + try { + const spamLink = new URL(node.href).searchParams.get("u"); + const spamDomain = spamLink ? new URL(spamLink).host : node.href; + + const buttonContainer = getAncestor(node, 9, "_3-8y").querySelector("._2vq9"); + if (!buttonContainer.querySelector(".additional-button")) { + buttonContainer.appendChild(htmlToElement(``)); + buttonContainer.appendChild(createBlackListButton(spamDomain)); + } + } catch (e) { + console.error(e); + console.log(node.href); + } + }) +} + + cleanSpams().catch(console.error); browser.storage.local.get(["clearSpamInterval"]) .then(({ clearSpamInterval }) => setInterval(cleanSpams, clearSpamInterval)) diff --git a/scripts/service-worker.js b/scripts/service-worker.js index c7b8fb0..d48cce0 100644 --- a/scripts/service-worker.js +++ b/scripts/service-worker.js @@ -105,8 +105,8 @@ browser.runtime.onInstalled.addListener(async () => { installedVersion = installedVersion || "0.1"; for (let [ver, migrate] of migrations) { if (installedVersion < ver) { - console.log(`Migrating to version ${ver}`) - await migrate() + console.log(`Migrating to version ${ver}`); + await migrate(); } } } catch (e) { diff --git a/styles/facebook-comment-plugin.css b/styles/facebook-comment-plugin.css index c7b864e..25473f3 100644 --- a/styles/facebook-comment-plugin.css +++ b/styles/facebook-comment-plugin.css @@ -2,12 +2,12 @@ background-color: #ececec; } -.restore-comment { +.additional-button { cursor: pointer; font-weight: bold; color: #3b64b6; } -.restore-comment:hover { +.additional-button:hover { text-decoration: underline; }