Swap x.com copy link

Replace `x` to `vxtwitter` when clicking 'Copy Link'

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Swap x.com copy link
// @namespace    https://github.com/gjing/xittershare
// @version      1.1
// @description  Replace `x` to `vxtwitter` when clicking 'Copy Link'
// @author       gjing
// @match        https://twitter.com/*
// @match        https://mobile.twitter.com/*
// @match        https://tweetdeck.twitter.com/*
// @match        https://x.com/*
// @icon         https://abs.twimg.com/favicons/twitter.2.ico
// @license      MIT
// ==/UserScript==

const replacement = 'vxtwitter';

(function() {
    'use strict';

    document.addEventListener("copy", (e) => {
        let selected = e.target.innerText;
        setTimeout(async()=>await replace_x(selected), 0);
    });

    function replace_x(selected) {
        if (selected.match(/^https+:\/\/((.+)\.)?(twitter|x)\.com\/(.+)\/status\/(\d+)(\?.+)?$/)) {
            let newUrl = selected.replace(/^https+:\/\/((.+)\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.com\/(.+)\/status\/(\d+)(\?.+)?$/, ['https://', replacement, '.com/$3/status/$4'].join(''));
            navigator.clipboard.writeText(newUrl);
            return;
        }
    }
})();