-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (62 loc) · 2.61 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function main(muted_words) {
const pagespace = document.querySelector('#pagespace')
// The next element after pagespace is a table row
const tableRow = pagespace.nextElementSibling
const submissions = Array.from(tableRow.querySelectorAll('.titleline > a'))
const tableBody = tableRow.querySelector('tbody')
// A spacer is present after every submission
const spacerElem = document.querySelector('.spacer')
// Last two elements in the table body are responsible for the "More" link that goes to the next page
const morespace = tableBody.children[tableBody.children.length - 2]
const more = tableBody.children[tableBody.children.length - 1]
// Create a new table body that will eventually replace the original
const newTableBody = document.createElement('tbody')
// Variable used to specify the rank in submissions
let count = 0
submissions.forEach((submission) => {
// Case insensitive matching
const title = submission.innerText.toLowerCase()
// true if the submission contains a word that's muted
const wordInSubmission = muted_words.some((word) => {
const re = new RegExp(`\\b${word}\\b`)
return title.match(re)
})
// ok submissions, ie, the title doesn't contain a muted word
if (!wordInSubmission) {
// Element with the upvote button, title and url
const titleParent = submission.closest('tr')
// Element with the points, time and other details
const descParent = titleParent.nextElementSibling
// Updating the rank so the final list are in order
const rank = titleParent.querySelector('.rank')
rank.innerText = ++count
// Appending to the new table body
newTableBody.appendChild(titleParent)
newTableBody.appendChild(descParent)
// spacerElem is cloned because we want a new element to be added on each iteration
newTableBody.appendChild(spacerElem.cloneNode(false))
}
})
// Add valid submissions are added to the new table body at this point
// Adding the elements that create the "More" link
newTableBody.appendChild(morespace)
newTableBody.appendChild(more)
// Finally replacing the current table children with the new table
tableBody.replaceChildren(newTableBody)
}
// Get the person's preference from storage
const getOptions = browser.storage.sync.get('words')
async function init() {
try {
const options = await browser.storage.sync.get('words')
const muted_words =
options.words
?.split('\n')
.filter((line) => line.trim() !== '')
.map((line) => line.trim()) || []
main(muted_words)
} catch (err) {
console.error(err)
}
}
init()