Skip to content

Commit

Permalink
Fix bug in migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dthung1602 committed May 15, 2022
1 parent b8b037e commit 32d42db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
18 changes: 10 additions & 8 deletions scripts/clean-spams.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ function getAncestor(node, num, tillEncounterClass) {
return node;
}

function log(nodes) {
console.log(`Found ${nodes.length} link in ${window.document.title}`);
}

let counter = 0;
const AVATAR_DATA_URI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TS0VaHOwg4pChOlkQFRUnrUIRKpRaoVUHk0u/oElDkuLiKLgWHPxYrDq4OOvq4CoIgh8gjk5Oii5S4v+SQosYD4778e7e4+4dIDQqTDW7RgFVs4x0Ii5mc6ti8BUBCAhjCjMSM/W5VCoJz/F1Dx9f72I8y/vcnyOs5E0G+ETiWaYbFvEG8eSmpXPeJ46wkqQQnxOPGHRB4keuyy6/cS46LPDMiJFJzxNHiMViB8sdzEqGSjxBHFVUjfKFrMsK5y3OaqXGWvfkLwzltZVlrtMcRAKLWEIKImTUUEYFFmK0aqSYSNN+3MM/4PhT5JLJVQYjxwKqUCE5fvA/+N2tWRgfc5NCcSDwYtsfQ0BwF2jWbfv72LabJ4D/GbjS2v5qA5j+JL3e1qJHQO82cHHd1uQ94HIH6H/SJUNyJD9NoVAA3s/om3JA3y3Qs+b21trH6QOQoa6SN8DBITBcpOx1j3d3d/b275lWfz+69HLE88IV9wAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB+YFAg07FECQvdIAABWxSURBVHja3Zt5jNzned8/7/s759zZ2Zk9uVxyl+JNiYcZy9RlWVIlW7LsuHESJK3rNkEKw0EDJEWatEmDukkbqEHTIwYSA0ZcB/AVGbYlRbYOW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Qs5nYBdIxqf+5U8qwb7pjoHPvhQJr92/yUBAJg9/cOhRmni++VyedMzP6nwtadqNNtFV8oS/MlnR9g0nCGRjiNsHWFqCF0DCSEBAsGSO8MPJx7Bp9IOHyZKeSihWJt6gMnqS9ExpUAIDJFBF0k2dN7PVO0Q884rACS1EXoT25iqvkIutoXe+Hr8wGUwtZkYSZQToFoBi/MlTo6P8tKbAY+/3FiWLBuHzzyY5vZdCZKZ/sOZvh2f1qzcyXRHZ3BZAABKM4e6aqXxb7bKo/cUKy77jjR5/rUGB0cdPvNAhsHuOn25HvqyWaSw0DUL09LQDBCahtKh4s9TcmdBSvKpQWreEqEK6Uuuo9iaYqL2JikzT9buI2V1oQsTEer4OJTq06BCOow8urJRoQe+QPmKwBM4LY8wdPADh4n5BaYW5kklbV7cL/nRGzU+sMHmzp1xbtkQJ52yVCwz8mgyv/nzQsYL6UxneHFj7xKrUl7UW0ujn2qWx/7Qa8xvDwKXagMKSx61hkfgr6jVhSQeM8gkY6TjFnFLQxcShIzMONrI0j5XLLfVzscCtaL6i1KfEkFk/oT4QUjdCSnXmizVmjiu387WKzY7GRqphEYuY5OIhUjNVka894VEdvjP9eTQ05mOdHD5zuYVVrlcSgbN+d2N8sQnAqeyRyl/Zxi0NPEO7A06nwvegQ0UQiC1mCuE9rJud/40nhn8Flb2QKYje8XR8f8HZnquLNuLgC0AAAAASUVORK5CYII=";

Expand Down Expand Up @@ -114,22 +110,28 @@ async function cleanSpams() {
const action = actionMapping.get(cleanSpamAction);
const filter = (node) => containBanDomain(node, banDomains);

let spamCount = 0;
const count = () => spamCount += 1;

// replace links in nested comments
const linksInNestedComments = window.document.querySelectorAll(".UFIImageBlockContent .UFIImageBlockContent ._5mdd a");
log(linksInNestedComments);
Array.from(linksInNestedComments)
.filter(filter)
.map((node) => getAncestor(node, 11).querySelector("._3-8m"))
.forEach(action);
.map(action)
.map(count);

// replace text spams in comments & nested comments
const uncheckedComments = window.document.querySelectorAll("._3-8m:not(.spam-free)");
Array.from(uncheckedComments)
.filter(filter)
.forEach((node) => {
.map((node) => {
node.classList.add("spam-free");
action(node)
});
})
.map(count);

console.log(`Found ${spamCount} spams`);
}

cleanSpams().catch(console.error);
Expand Down
20 changes: 14 additions & 6 deletions scripts/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,31 @@ const NEW_BAN_DOMAINS_1 = [
"/www[a-z0-9-.]*cash[a-z0-9-.]*com/",
]

async function migrationToVer1_0_1() {
let { banDomains } = await browser.storage.local.get(["banDomains"]);
DEFAULT_TEXT = "<i>Just a spam, nothing to see here</i>";

async function migrationToVer1_1_0() {
let { banDomains, cleanSpamText, clearSpamInterval } = await browser.storage.local.get([
"banDomains", "cleanSpamText", "clearSpamInterval"
]);
banDomains = Array.from(new Set([...banDomains, ...NEW_BAN_DOMAINS_1]));
cleanSpamText = cleanSpamText || DEFAULT_TEXT;
clearSpamInterval = clearSpamInterval === 1500 ? 1000 : clearSpamInterval;
await browser.storage.local.set({
version: "1.0.1",
banDomains
version: "1.1.0",
banDomains,
cleanSpamText,
clearSpamInterval,
})
}

const migrations = [
["1.0", migrateToVer1],
["1.0.1", migrationToVer1_0_1],
["1.1.0", migrationToVer1_1_0],
];

browser.runtime.onInstalled.addListener(async () => {
try {
let { version: installedVersion } = browser.storage.local.get("version");
let { version: installedVersion } = await browser.storage.local.get("version");
installedVersion = installedVersion || "0.1";
for (let [ver, migrate] of migrations) {
if (installedVersion < ver) {
Expand Down

0 comments on commit 32d42db

Please sign in to comment.