Skip to content

Instantly share code, notes, and snippets.

@EIIisD
Last active December 2, 2025 19:39
Show Gist options
  • Select an option

  • Save EIIisD/80ff22e4959a82bd78ab2ee75adc0d76 to your computer and use it in GitHub Desktop.

Select an option

Save EIIisD/80ff22e4959a82bd78ab2ee75adc0d76 to your computer and use it in GitHub Desktop.
TamperMonkey
// ==UserScript==
// @name Smart Redirector
// @version 1.5
// @description Redirect specific sites based on a configuration array using smart asterisks.
// @author Ellis Donovan
// @match *://*/*
// @run-at document-start
// @grant none
// @updateURL https://gist.githubusercontent.com/EIIisD/80ff22e4959a82bd78ab2ee75adc0d76/raw/smart_redirect.js
// @downloadURL https://gist.githubusercontent.com/EIIisD/80ff22e4959a82bd78ab2ee75adc0d76/raw/smart_redirect.js
// ==/UserScript==
(function () {
"use strict";
/**
* Configuration: Array of [Match Pattern, Destination URL]
* - Use * as a wildcard to match any sequence of characters.
* - Use $1, $2, etc. in the destination to insert the text matched by each * wildcard.
* - Patterns are matched against the full URL (including protocol).
* - Matches are case-insensitive.
* - Trailing slashes are ignored in matching.
*/
const REDIRECTS = [
// Kraken
["*www.kraken.com/", "https://www.kraken.com/c"],
["*www.kraken.com/en-gb", "https://www.kraken.com/c"],
// Discord
[
"*discord.com/",
"https://discord.com/channels/1405350618880540773/1429704856951586836",
],
// Example using capture groups:
// ["*://twitter.com/*/status/*", "https://nitter.net/$2/status/$3"],
];
const currentUrl = window.location.href;
// Normalize URL for comparison (remove trailing slash)
const normalize = (url) => url.replace(/\/$/, "");
function getRedirect(url) {
const cleanUrl = normalize(url);
for (const [pattern, destination] of REDIRECTS) {
const cleanPattern = normalize(pattern);
// Escape regex characters, then replace * with (.*) to capture the wildcard content
const escapeRegex = (s) =>
s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
const regexBody = cleanPattern.split("*").map(escapeRegex).join("(.*)");
// Case-insensitive match against the full string
const regex = new RegExp("^" + regexBody + "$", "i");
if (regex.test(cleanUrl)) {
// Use replace to substitute $1, $2, etc. with captured groups
return cleanUrl.replace(regex, destination);
}
}
return null;
}
// Find matching redirect
const targetUrl = getRedirect(currentUrl);
if (targetUrl) {
// Prevent infinite redirection loop:
// 1. Exact match
// 2. Destination is same as current URL but with/without trailing slash
if (
currentUrl !== targetUrl &&
normalize(currentUrl) !== normalize(targetUrl)
) {
console.log(`[Smart Redirector] Redirecting to: ${targetUrl}`);
window.location.replace(targetUrl);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment