X.com to Twitter.com Redirect

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

目前為 2025-05-24 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name X.com to Twitter.com Redirect
  3. // @name:zh-CN 重定向X.com到Twitter.com
  4. // @namespace NTE
  5. // @version 1.4
  6. // @description Redirects X.com URLs to Twitter.com and ensures the 'mx=1' parameter is present.
  7. // @description:zh-cn 重定向X.com到Twitter.com并确保后面有“mx=1"参数
  8. // @author NTE
  9. // @match *://x.com/*
  10. // @match *://twitter.com/*
  11. // @match *://mobile.x.com/*
  12. // @match *://mobile.twitter.com/*
  13. // @match *://pro.x.com/*
  14. // @match *://pro.twitter.com/*
  15. // @exclude *://x.com/i/tweetdeck*
  16. // @grant none
  17. // @license AGPL-3.0-or-later
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22.  
  23. console.log('Tampermonkey script is running. Version 1.4'); // 脚本启动日志
  24.  
  25. const currentUrl = new URL(window.location.href);
  26. let targetHostname = currentUrl.hostname;
  27. let targetPathname = currentUrl.pathname;
  28. let targetSearchParams = new URLSearchParams(currentUrl.searchParams); // 复制当前URL的查询参数
  29.  
  30. let shouldRedirect = false;
  31.  
  32. console.log('Current URL (当前URL):', currentUrl.toString());
  33. console.log('Current Hostname (当前主机名):', currentUrl.hostname);
  34. console.log('Current Pathname (当前路径):', currentUrl.pathname);
  35. console.log('Current Search Params (当前查询参数):', currentUrl.searchParams.toString());
  36.  
  37.  
  38. // 情况1: 如果主机名以 x.com 结尾
  39. if (currentUrl.hostname.endsWith('x.com')) {
  40. console.log('Condition met: Hostname ends with x.com (条件满足: 主机名以 x.com 结尾)');
  41.  
  42. // 使用新的正则表达式替换主机名。
  43. // `^(.*?)x\.com$` 会匹配:
  44. // - `x.com` -> 捕获组 $1 为空字符串,替换为 `twitter.com`
  45. // - `mobile.x.com` -> 捕获组 $1 为 `mobile.`,替换为 `mobile.twitter.com`
  46. targetHostname = currentUrl.hostname.replace(/^(.*?)x\.com$/, '$1twitter.com');
  47. console.log('Target Hostname after replacement (替换后的目标主机名):', targetHostname);
  48.  
  49. targetSearchParams.set('mx', '1'); // 设置 mx=1 参数
  50. console.log('Target Search Params after setting mx=1 (设置mx=1后的目标查询参数):', targetSearchParams.toString());
  51.  
  52. // 处理根路径重定向
  53. if (currentUrl.pathname === '/') {
  54. targetPathname = ''; // 移除根路径的斜杠
  55. console.log('Root path detected, target pathname (检测到根路径,目标路径):', targetPathname);
  56. }
  57. shouldRedirect = true;
  58. console.log(`Detected ${currentUrl.hostname}, preparing to redirect to ${targetHostname}. (检测到 ${currentUrl.hostname},准备重定向到 ${targetHostname}。)`);
  59. }
  60. // 情况2: 如果主机名以 twitter.com 结尾且缺少 'mx' 参数
  61. else if (currentUrl.hostname.endsWith('twitter.com') && !currentUrl.searchParams.has('mx')) {
  62. console.log('Condition met: Hostname ends with twitter.com and mx is missing. (条件满足: 主机名以 twitter.com 结尾且缺少 mx 参数。)');
  63. targetSearchParams.set('mx', '1');
  64. shouldRedirect = true;
  65. console.log('Target Search Params after setting mx=1 (设置mx=1后的目标查询参数):', targetSearchParams.toString());
  66. } else {
  67. console.log('No specific redirection condition met based on hostname/params. (没有满足特定重定向条件,基于主机名/参数。)');
  68. }
  69.  
  70. // 手动构建新的 URL 字符串
  71. let newUrlString = `${currentUrl.protocol}//${targetHostname}${targetPathname}`;
  72. if (targetSearchParams.toString()) {
  73. newUrlString += `?${targetSearchParams.toString()}`;
  74. }
  75. // 处理 URL 中的哈希部分(#后面的内容)
  76. if (currentUrl.hash) {
  77. newUrlString += currentUrl.hash;
  78. }
  79.  
  80.  
  81. console.log('shouldRedirect (是否需要重定向):', shouldRedirect);
  82. console.log('newUrlString (新URL字符串):', newUrlString);
  83. console.log('currentUrl.toString() (当前URL字符串):', currentUrl.toString());
  84. console.log('Are URLs different? (URL是否不同?):', newUrlString !== currentUrl.toString());
  85.  
  86. // 如果需要重定向且新旧URL不同,则执行重定向
  87. if (shouldRedirect && newUrlString !== currentUrl.toString()) {
  88. console.log('Performing redirection... (正在执行重定向...)');
  89. console.log('Redirecting from (从):', currentUrl.toString());
  90. console.log('Redirecting to (到):', newUrlString);
  91. window.location.replace(newUrlString);
  92. } else {
  93. console.log('No redirection performed. (未执行重定向。)');
  94. }
  95.  
  96. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址