a little refactoring of hide-slop

This commit is contained in:
Jack Case
2025-10-22 14:17:44 +00:00
parent 8cea8a7785
commit 554f1082f9

View File

@@ -5,16 +5,25 @@ let result_list_node
let result_list_observer let result_list_observer
const page_links = new Map() const page_links = new Map()
class SearchLink {
constructor(link_node) {
this.node = link_node
this.target = link_node.getAttribute("href")
this.checked = false
this.result = undefined
}
}
function check_links(links) { function check_links(links) {
// send a message to background script with a list of URLs to check // send a message to background script with a list of URLs to check
browser.runtime.sendMessage({type: "check", urls: links}) browser.runtime.sendMessage({type: "check", urls: links})
links.forEach((link) => {page_links.set(link, {checked: true})}) links.forEach((link) => {page_links.get(link).checked = true})
} }
async function message_listener(message) { async function message_listener(message) {
if(message.type === "check_result") { if(message.type === "check_result") {
console.log(message.url, message.result) console.log(message.url, message.result)
page_links.set(message.url, message.result) page_links.get(message.url).result = message.result
} }
} }
@@ -22,10 +31,10 @@ function get_initial_links() {
//get links //get links
const links = document.querySelectorAll(ddg_result_selector) const links = document.querySelectorAll(ddg_result_selector)
links.forEach((node) => { links.forEach((node) => {
page_links.set(node.getAttribute("href"), undefined) const link = new SearchLink(node)
page_links.set(link.target, link)
}) })
const link_targets = page_links.keys().toArray() const link_targets = page_links.keys().toArray()
console.log(link_targets)
check_links(link_targets) check_links(link_targets)
} }
@@ -33,12 +42,12 @@ function update_links() {
// the result list has updated, add new links and check them // the result list has updated, add new links and check them
const links = document.querySelectorAll(ddg_result_selector) const links = document.querySelectorAll(ddg_result_selector)
links.forEach((node) => { links.forEach((node) => {
let url = node.getAttribute("href") const link = new SearchLink(node)
if (page_links.has(url)) return if (page_links.has(link.target)) return
page_links.set(url, undefined) page_links.set(link.target, link)
}) })
const link_arr = page_links.keys().filter((key) => { const link_arr = page_links.keys().filter((key) => {
return !(page_links.get(key)) return !(page_links.get(key).checked)
}).toArray() }).toArray()
check_links(link_arr) check_links(link_arr)
@@ -54,9 +63,8 @@ function onload_handler() {
// get results ol node // get results ol node
result_list_node = document.querySelector(ddg_result_list_selector) result_list_node = document.querySelector(ddg_result_list_selector)
setup_result_observer()
get_initial_links() get_initial_links()
setup_result_observer()
} }
browser.runtime.onMessage.addListener(message_listener) browser.runtime.onMessage.addListener(message_listener)