Last active
November 1, 2025 16:06
-
-
Save Suznyan/7e9443c88570a02e257887f259570641 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 Replace x.com Copy Link Domain | |
| // @version 1.0 | |
| // @description Intercept "Share > Copy link" on X (Twitter) and replace domain (x.com/twitter.com → custom mirror) | |
| // @author Eszee | |
| // @match https://twitter.com/* | |
| // @match https://x.com/* | |
| // @icon https://abs.twimg.com/favicons/twitter.2.ico | |
| // @updateURL https://gist.github.com/Suznyan/7e9443c88570a02e257887f259570641/raw/f644af9b3ab68e2613db06981460c889774e29bb/replace-x-copy-link.user.js | |
| // @downloadURL https://gist.github.com/Suznyan/7e9443c88570a02e257887f259570641/raw/f644af9b3ab68e2613db06981460c889774e29bb/replace-x-copy-link.user.js | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| /* ============================== | |
| 🔧 CONFIGURATION | |
| ============================== */ | |
| const REPLACEMENT_DOMAIN = 'fxtwitter.com'; | |
| // 👉 Change this to whatever mirror you want, e.g.: | |
| // const REPLACEMENT_DOMAIN = 'cunnyx.com'; | |
| const MATCH_REGEX = /https?:\/\/(www\.)?(x|twitter)\.com/ig; | |
| const REPLACEMENT_BASE = `https://${REPLACEMENT_DOMAIN}`; | |
| /* ============================== | |
| 🚀 CORE LOGIC | |
| ============================== */ | |
| // Replace x.com/twitter.com → chosen mirror domain | |
| function rewriteUrl(text) { | |
| if (typeof text !== 'string') return text; | |
| return text.replace(MATCH_REGEX, REPLACEMENT_BASE); | |
| } | |
| // Patch clipboard API used by “Copy link” | |
| function patchClipboard() { | |
| if (!navigator.clipboard || !navigator.clipboard.writeText) return; | |
| const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard); | |
| navigator.clipboard.writeText = async function(text) { | |
| const newText = rewriteUrl(text); | |
| return originalWriteText(newText); | |
| }; | |
| } | |
| // Also handle manual copy (Ctrl+C / context menu) | |
| function handleCopyEvent(e) { | |
| try { | |
| const sel = document.getSelection(); | |
| const text = sel ? sel.toString() : ''; | |
| if (MATCH_REGEX.test(text)) { | |
| const replaced = rewriteUrl(text); | |
| e.clipboardData.setData('text/plain', replaced); | |
| e.preventDefault(); | |
| } | |
| } catch {} | |
| } | |
| function init() { | |
| patchClipboard(); | |
| document.addEventListener('copy', handleCopyEvent, true); | |
| } | |
| if (document.readyState === 'loading') { | |
| document.addEventListener('DOMContentLoaded', init); | |
| } else { | |
| init(); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment