重定向X.com到Twitter.com

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

当前为 2025-05-24 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         X.com to Twitter.com Redirect
// @name:zh-CN   重定向X.com到Twitter.com
// @namespace    NTE
// @version      1.0
// @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/*
// @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')) {
        newUrl.hostname = '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 x.com, preparing to redirect to twitter.com.');
    }
    // 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')) {
        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());
    }

})();