Skip to content

Commit

Permalink
Add ban domain button
Browse files Browse the repository at this point in the history
  • Loading branch information
dthung1602 committed May 20, 2022
1 parent ae7e796 commit a1f575c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
36 changes: 35 additions & 1 deletion scripts/clean-spams.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ const restoreText = "⟲ Restore comment";
const hideText = "⟳ Hide spam"

function createRestoreButton(node, restoreKey) {
const restoreButton = htmlToElement(`<span class="restore-comment">${restoreText}</span>`);
const restoreButton = htmlToElement(`<span class="additional-button">${restoreText}</span>`);
restoreButton.addEventListener("click", () => {
swapStoredContent(node, restoreKey);
restoreButton.innerText = restoreButton.innerText === restoreText ? hideText : restoreText;
});
return restoreButton;
}

function createBlackListButton(spamDomain) {
const button = htmlToElement(`<span class="additional-button">✖ Blacklist domain</span>`);
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) {
Expand Down Expand Up @@ -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(`<span aria-hidden="true"> · </span>`));
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))
Expand Down
4 changes: 2 additions & 2 deletions scripts/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions styles/facebook-comment-plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit a1f575c

Please sign in to comment.