set up mutationobserver to react when more results load
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
"activeTab",
|
"activeTab",
|
||||||
"storage",
|
"storage",
|
||||||
"webNavigation",
|
"webNavigation",
|
||||||
"*://duckduckgo.com/*"
|
"*://*.duckduckgo.com/*"
|
||||||
],
|
],
|
||||||
|
|
||||||
"page_action": {
|
"page_action": {
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"matches": ["*://duckduckgo.com/*"],
|
"matches": ["*://*.duckduckgo.com/*q=*"],
|
||||||
"js": ["scripts/hide-slop.js"]
|
"js": ["scripts/hide-slop.js"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
const ddg_result_selector = "a[data-testid=\"result-title-a\""
|
||||||
|
const ddg_result_list_selector = "ol.react-results--main"
|
||||||
|
|
||||||
|
let result_list_node
|
||||||
|
let result_list_observer
|
||||||
|
const page_links = new Map()
|
||||||
|
|
||||||
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})
|
||||||
@@ -6,19 +13,43 @@ function check_links(links) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_initial_links() {
|
||||||
|
//get links
|
||||||
|
const links = document.querySelectorAll(ddg_result_selector)
|
||||||
|
links.forEach((node) => {
|
||||||
|
page_links.set(node.getAttribute("href"), undefined)
|
||||||
|
})
|
||||||
|
console.log(link_targets)
|
||||||
|
check_links(page_links.keys())
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_links() {
|
||||||
|
// the result list has updated, add new links and check them
|
||||||
|
}
|
||||||
|
|
||||||
|
function setup_result_observer() {
|
||||||
|
const config = { childList: true }
|
||||||
|
result_list_observer = new MutationObserver(update_links)
|
||||||
|
result_list_observer.observe(result_list_node, config)
|
||||||
|
}
|
||||||
|
|
||||||
function onload_handler() {
|
function onload_handler() {
|
||||||
//get links
|
// get results ol node
|
||||||
const links = document.querySelectorAll("a[data-testid=\"result-title-a\"")
|
result_list_node = document.querySelector(ddg_result_list_selector)
|
||||||
let link_targets = new Array()
|
|
||||||
links.forEach((node) => {
|
setup_result_observer()
|
||||||
link_targets.push(node.getAttribute("href"))
|
|
||||||
})
|
get_initial_links()
|
||||||
console.log(link_targets)
|
|
||||||
check_links(link_targets)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(message_listener)
|
browser.runtime.onMessage.addListener(message_listener)
|
||||||
addEventListener("DOMContentLoaded", onload_handler)
|
|
||||||
|
if (document.readyState === "loading") {
|
||||||
|
document.addEventListener("DOMContentLoaded", onload_handler)
|
||||||
|
} else {
|
||||||
|
onload_handler()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user