Last active
June 18, 2024 18:42
-
-
Save abstraction/a911d375ec10380b2f7794c57e3d3a39 to your computer and use it in GitHub Desktop.
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
| // ==UserScript== | |
| // @name Block Search Results | |
| // @namespace http://tampermonkey.net/ | |
| // @version 1.0 | |
| // @description Hide search results from specific domains. Port of https://github.com/hp27596/secondpage | |
| // @author Your Name | |
| // @match *://*.google.com/* | |
| // @grant GM_xmlhttpRequest | |
| // @grant GM_getValue | |
| // @grant GM_setValue | |
| // @grant GM_addStyle | |
| // @connect gist.github.com | |
| // ==/UserScript== | |
| (async function() { | |
| 'use strict'; | |
| async function fetchBlockedDomains() { | |
| return new Promise((resolve) => { | |
| GM_xmlhttpRequest({ | |
| method: "GET", | |
| // https://stackoverflow.com/a/47175630 -- perma gist url | |
| url: "https://gist.github.com/abstraction/d65949dfe7eac6c8f045274d0a771a33/raw/google-search-blocklist.txt", | |
| onload: function(response) { | |
| if (response.status >= 200 && response.status < 400) { | |
| const text = response.responseText; | |
| const domains = text.split("\n") | |
| .map(domain => domain.trim()) | |
| .filter(domain => domain !== "" && !domain.startsWith("//")); | |
| resolve(domains); | |
| } else { | |
| console.error("Error fetching blocked domains:", response); | |
| resolve([]); | |
| } | |
| }, | |
| onerror: function(error) { | |
| console.error("Error fetching blocked domains:", error); | |
| resolve([]); | |
| } | |
| }); | |
| }); | |
| } | |
| function isBlockedDomain(url, blockedDomains) { | |
| return blockedDomains.some(domain => url.hostname === domain || url.hostname.endsWith(`.${domain}`)); | |
| } | |
| function hideSearchResults(blockedDomains) { | |
| const searchResults = document.querySelectorAll(".g"); | |
| searchResults.forEach((result) => { | |
| const linkElement = result.querySelector("a"); | |
| if (linkElement && linkElement.href) { | |
| try { | |
| const url = new URL(linkElement.href); | |
| if (isBlockedDomain(url, blockedDomains)) { | |
| result.style.display = "none"; | |
| } | |
| } catch (error) { | |
| console.error("Invalid URL:", linkElement.href); | |
| } | |
| } | |
| }); | |
| } | |
| function showSearchResults() { | |
| const searchResults = document.querySelectorAll(".g"); | |
| searchResults.forEach((result) => { | |
| result.style.display = "block"; | |
| }); | |
| } | |
| function showPopupMessage(message) { | |
| const popup = document.createElement('div'); | |
| popup.textContent = message; | |
| popup.style.position = 'fixed'; | |
| popup.style.top = '10px'; | |
| popup.style.left = '50%'; | |
| popup.style.transform = 'translateX(-50%)'; | |
| popup.style.backgroundColor = '#444'; | |
| popup.style.color = '#fff'; | |
| popup.style.padding = '10px'; | |
| popup.style.borderRadius = '5px'; | |
| popup.style.boxShadow = '0px 0px 10px rgba(0,0,0,0.5)'; | |
| popup.style.zIndex = '10000'; | |
| document.body.appendChild(popup); | |
| setTimeout(() => { | |
| popup.style.transition = 'opacity 0.5s'; | |
| popup.style.opacity = '0'; | |
| setTimeout(() => popup.remove(), 500); | |
| }, 1000); | |
| } | |
| function initializeBlocking() { | |
| const isEnabled = GM_getValue("isEnabled", true); | |
| if (isEnabled) { | |
| fetchBlockedDomains().then((blockedDomains) => { | |
| hideSearchResults(blockedDomains); | |
| showPopupMessage("removed meh websites"); | |
| const observer = new MutationObserver(() => { | |
| hideSearchResults(blockedDomains); | |
| }); | |
| observer.observe(document.body, { childList: true, subtree: true }); | |
| }); | |
| } | |
| } | |
| initializeBlocking(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment