Last active
February 11, 2026 02:42
-
-
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 + Remove Tracking (Fixed) | |
| // @version 1.2 | |
| // @description Intercept "Share > Copy link" on X, strip tracking params, replace domain (no duplicate path bug) | |
| // @author Eszee | |
| // @match https://twitter.com/* | |
| // @match https://x.com/* | |
| // @icon https://abs.twimg.com/favicons/twitter.2.ico | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| /* ============================== | |
| π§ CONFIGURATION | |
| ============================== */ | |
| const REPLACEMENT_DOMAIN = 'm.fxtwitter.com'; | |
| const REPLACEMENT_BASE = `https://${REPLACEMENT_DOMAIN}`; | |
| // Match full Twitter URLs | |
| const FULL_URL_REGEX = /https?:\/\/(www\.)?(x|twitter)\.com\/[^\s]+/ig; | |
| // Tracking parameters to remove | |
| const TRACKING_PARAMS = [ | |
| "s", | |
| "t", | |
| "ref_src", | |
| "ref_url", | |
| "utm_source", | |
| "utm_medium", | |
| "utm_campaign", | |
| "utm_term", | |
| "utm_content" | |
| ]; | |
| /* ============================== | |
| π§Ή REMOVE TRACKING PARAMETERS | |
| ============================== */ | |
| function stripTracking(url) { | |
| try { | |
| const u = new URL(url); | |
| TRACKING_PARAMS.forEach(param => u.searchParams.delete(param)); | |
| return u.toString(); | |
| } catch { | |
| return url; | |
| } | |
| } | |
| /* ============================== | |
| π URL REWRITE (Fixed Version) | |
| ============================== */ | |
| function rewriteUrl(text) { | |
| if (typeof text !== 'string') return text; | |
| return text.replace(FULL_URL_REGEX, url => { | |
| // Clean tracking first | |
| const cleaned = stripTracking(url); | |
| // Replace only the domain | |
| return cleaned.replace(/https?:\/\/(www\.)?(x|twitter)\.com/i, REPLACEMENT_BASE); | |
| }); | |
| } | |
| /* ============================== | |
| π CLIPBOARD PATCH | |
| ============================== */ | |
| function patchClipboard() { | |
| if (!navigator.clipboard || !navigator.clipboard.writeText) return; | |
| const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard); | |
| navigator.clipboard.writeText = async function(text) { | |
| const cleaned = rewriteUrl(stripTracking(text)); | |
| return originalWriteText(cleaned); | |
| }; | |
| } | |
| /* ============================== | |
| π MANUAL COPY EVENT | |
| ============================== */ | |
| function handleCopyEvent(e) { | |
| try { | |
| const sel = document.getSelection(); | |
| const text = sel ? sel.toString() : ''; | |
| if (FULL_URL_REGEX.test(text)) { | |
| const cleaned = rewriteUrl(stripTracking(text)); | |
| e.clipboardData.setData('text/plain', cleaned); | |
| e.preventDefault(); | |
| } | |
| } catch {} | |
| } | |
| /* ============================== | |
| π INIT | |
| ============================== */ | |
| 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