Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Last active July 10, 2024 22:33
Show Gist options
  • Save SmugZombie/9893896549a15940fc8023eb77453584 to your computer and use it in GitHub Desktop.
Save SmugZombie/9893896549a15940fc8023eb77453584 to your computer and use it in GitHub Desktop.
Google to DuckDuckGo Redirector
// ==UserScript==
// @name Google/Bing to DuckDuckGo Redirector
// @namespace http://smugzombie.com
// @version 1.1
// @description Redirects Google/Bing searches to DuckDuckGo.
// @author Ron Egli <ron@r-egli.com> (github.com/smugzombie)
// @match *://www.google.com/*
// @match *://www.bing.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const googleQueryPattern = /^https:\/\/www\.google\.com\/search\?q=(.*)$/;
const bingQueryPattern = /^https:\/\/www\.bing\.com\/search\?q=(.*)$/;
function redirectIfNeeded() {
const currentUrl = window.location.href;
let match;
if (currentUrl.match(googleQueryPattern)) {
match = currentUrl.match(googleQueryPattern);
if (match) {
const duckDuckGoUrl = `https://duckduckgo.com/?q=${match[1]}`;
window.location.replace(duckDuckGoUrl);
} else if (currentUrl === "https://www.google.com/") {
window.location.replace("https://duckduckgo.com");
}
} else if (currentUrl.match(bingQueryPattern)) {
match = currentUrl.match(bingQueryPattern);
if (match) {
const duckDuckGoUrl = `https://duckduckgo.com/?q=${match[1]}`;
window.location.replace(duckDuckGoUrl);
} else if (currentUrl === "https://www.bing.com/") {
window.location.replace("https://duckduckgo.com");
}
}
}
// Run the redirect check when the script is loaded
redirectIfNeeded();
// Observe changes in the URL to handle single-page navigation in Google and Bing
const observer = new MutationObserver(redirectIfNeeded);
observer.observe(document, { childList: true, subtree: true });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment