Created
May 30, 2025 05:13
-
-
Save flashvnn/d55001e5addc445284211a46b00f1a65 to your computer and use it in GitHub Desktop.
Changes https://khicongydaotoronto.com links to http:// in search engine results.
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 Rewrite khicongydaotoronto.com links in Search Results to HTTP | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description Changes https://khicongydaotoronto.com links to http:// in search engine results. | |
// @author Your Name | |
// @match https://www.google.com/search* | |
// @match https://www.google.com.*/search* | |
// @match https://www.bing.com/search* | |
// @match https://duckduckgo.com/* | |
// @match https://www.baidu.com/s* | |
// @match https://yandex.com/search/* | |
// @match https://yandex.ru/search/* | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const targetDomain = "khicongydaotoronto.com"; | |
const httpsPrefix = `https://${targetDomain}`; | |
const httpPrefix = `http://${targetDomain}`; | |
function modifyLinksInNode(node) { | |
// Select all anchor tags within the given node (or document body if node is null) | |
// that have an href attribute starting with the HTTPS version of the target domain. | |
const links = (node || document.body).querySelectorAll(`a[href^="${httpsPrefix}"]`); | |
links.forEach(link => { | |
if (link.href.startsWith(httpsPrefix)) { | |
const oldHref = link.href; | |
link.href = oldHref.replace(httpsPrefix, httpPrefix); | |
console.log(`Tampermonkey: Rewrote link from ${oldHref} to ${link.href}`); | |
} | |
}); | |
} | |
// Initial modification pass on existing links when the script runs. | |
// 'document-end' ensures most of the initial static content is loaded. | |
modifyLinksInNode(document.body); | |
// Observe DOM changes to handle dynamically loaded search results (e.g., infinite scroll or AJAX updates). | |
const observer = new MutationObserver(mutationsList => { | |
for (const mutation of mutationsList) { | |
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { | |
// If new nodes were added, check them for relevant links. | |
mutation.addedNodes.forEach(newNode => { | |
// Only process element nodes, and specifically check if they are <a> tags or contain <a> tags. | |
if (newNode.nodeType === Node.ELEMENT_NODE) { | |
if (newNode.matches('a')) { // If the added node itself is an anchor | |
if (newNode.href && newNode.href.startsWith(httpsPrefix)) { | |
const oldHref = newNode.href; | |
newNode.href = oldHref.replace(httpsPrefix, httpPrefix); | |
console.log(`Tampermonkey: Rewrote dynamically added link from ${oldHref} to ${newNode.href}`); | |
} | |
} else { // If the added node is a container that might have anchors | |
modifyLinksInNode(newNode); | |
} | |
} | |
}); | |
} | |
} | |
}); | |
// Start observing the document body for additions of child nodes and changes in the subtree. | |
observer.observe(document.body, { | |
childList: true, // Observe direct children additions/removals | |
subtree: true // Observe all descendants | |
}); | |
// Optional: You might want to disconnect the observer if the page is being unloaded, | |
// though for search result pages it's usually not a critical issue. | |
// window.addEventListener('beforeunload', () => { | |
// observer.disconnect(); | |
// }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment