X.com to Twitter.com Redirect

Redirects X.com URLs to Twitter.com and ensures the 'mx=1' parameter is present.

Stan na 24-05-2025. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         X.com to Twitter.com Redirect
// @name:zh-CN   重定向X.com到Twitter.com
// @namespace    NTE
// @version      1.2
// @description  Redirects X.com URLs to Twitter.com and ensures the 'mx=1' parameter is present.
// @description:zh-cn 重定向X.com到Twitter.com并确保后面有“mx=1"参数
// @author       NTE
// @match        *://x.com/*
// @match        *://twitter.com/*
// @match        *://mobile.x.com/*
// @match        *://mobile.twitter.com/*
// @match        *://pro.x.com/*
// @match        *://pro.twitter.com/*
// @exclude      *://x.com/i/tweetdeck*
// @grant        none
// @license      AGPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    console.log('Tampermonkey script is running.');

    const currentUrl = new URL(window.location.href);
    let newUrl = new URL(window.location.href); // Start with a copy of the current URL

    let shouldRedirect = false;

    // Case 1: If the hostname ends with x.com
    if (currentUrl.hostname.endsWith('x.com')) {
        // Replace '.x.com' with '.twitter.com' to preserve subdomains
        // For example, mobile.x.com becomes mobile.twitter.com
        // x.com becomes twitter.com
        newUrl.hostname = currentUrl.hostname.replace(/\.x\.com$/, '.twitter.com');

        newUrl.searchParams.set('mx', '1');

        // Handle the root path redirection specifically for x.com
        if (currentUrl.pathname === '/') {
            newUrl.pathname = ''; // Remove the trailing slash for the root path
        }
        shouldRedirect = true;
        console.log(`Detected ${currentUrl.hostname}, preparing to redirect to ${newUrl.hostname}.`);
    }
    // Case 2: If the hostname ends with twitter.com and 'mx' parameter is missing
    else if (currentUrl.hostname.endsWith('twitter.com') && !currentUrl.searchParams.has('mx')) {
        // No hostname change needed here, just add the parameter
        newUrl.searchParams.set('mx', '1');
        shouldRedirect = true;
        console.log('Detected twitter.com without mx=1, preparing to add parameter.');
    }

    // Perform redirection if needed
    if (shouldRedirect && newUrl.toString() !== currentUrl.toString()) {
        console.log('Redirecting from:', currentUrl.toString());
        console.log('Redirecting to:', newUrl.toString());
        window.location.replace(newUrl.toString());
    } else {
        console.log('No redirection needed for:', currentUrl.toString());
    }

})();