Created
September 19, 2020 11:51
-
-
Save CptPotato/6233d3ded9deaaf342f4bcbdc9817a50 to your computer and use it in GitHub Desktop.
Search result filter for duckduckgo.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inject_filter( | |
blacklist, | |
scroll_threshold = 512 | |
) | |
{ | |
let lastscroll = 0; | |
function err(msg) | |
{ | |
document.onscroll = null; | |
alert("Error:\n" + msg); | |
} | |
function update(first_pass) | |
{ | |
try | |
{ | |
let e = document.querySelector("div.results"); | |
if(e === null || e === undefined) return; | |
let results = e.querySelectorAll("div.result"); | |
if(results.length <= 0) return; | |
let delete_these = [ ]; | |
let count = blacklist.length; | |
results.forEach((n) => { | |
if(n.hasAttribute("ignore")) return; | |
let e = n.querySelector("span.result__url__domain"); | |
if(e == null) return; | |
let domain = e.innerHTML; | |
//e = n.querySelector("span.result__url__full"); | |
//if(e == null) return; | |
//let url = e.innerHTML; | |
let str = domain; // search only domain name for now | |
for(let i = 0; i < count; i++) | |
{ | |
if(str.search(blacklist[i]) != -1) | |
{ | |
delete_these.push(n); | |
break; | |
} | |
} | |
}); | |
count = delete_these.length; | |
if(count <= 0) return; | |
let insert_info = delete_these[0].previousSibling; | |
let restore = document.createElement("div"); | |
restore.style["margin-top"] = "10px"; | |
restore.style["padding-left"] = "8px"; | |
restore.style["padding-right"] = "8px"; | |
for(let i = 0; i < count; i++) | |
{ | |
let e = delete_these[i]; | |
e.setAttribute("ignore", "true"); | |
restore.appendChild(e); | |
} | |
let info = document.createElement("div"); | |
info.style["padding-left"] = "3px"; | |
info.style["color"] = "#d7b973"; | |
info.style["background"] = "#dd631044"; | |
info.style["border"] = "1px solid #a6691b55"; | |
info.style["border-radius"] = "4px"; | |
info.style["cursor"] = "pointer"; | |
info.setAttribute("title", "click to restore"); | |
info.onclick = () => { | |
try | |
{ | |
info.onclick = null; | |
info.style["cursor"] = null; | |
info.appendChild(restore); | |
} | |
catch (e) | |
{ | |
err(e); | |
return; | |
} | |
}; | |
if(first_pass) | |
{ | |
info.innerHTML = "<span style=\"opacity: 0.8\">⚠️</span> removed " + count + " search results"; | |
let el = document.querySelector("div.results--main"); | |
el.insertBefore(info, el.children[1]); | |
} | |
else | |
{ | |
info.style["margin-bottom"] = "16px"; | |
info.innerHTML = "<span style=\"opacity: 0.8\">⚠️</span> removed " + count + " more search results"; | |
insert_info.parentElement.insertBefore(info, insert_info.nextSibling); | |
} | |
} | |
catch (ex) | |
{ | |
err(ex); | |
} | |
} | |
function scroll() | |
{ | |
let currscroll = document.documentElement.scrollTop; | |
if(Math.abs(lastscroll - currscroll) > scroll_threshold) | |
{ | |
lastscroll = currscroll; | |
update(false); | |
} | |
} | |
function main() | |
{ | |
update(true); | |
if(scroll_threshold > 0) | |
document.onscroll = scroll; | |
} | |
window.onload = main; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can be used as a greasemonkey userscript like this: