Website Redirects

Redirect specified websites to designated locations while preserving paths and query parameters

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Website Redirects
// @description  Redirect specified websites to designated locations while preserving paths and query parameters
// @version      0.0.1
// @author       0x96EA
// @namespace    https://github.com/0x96EA/userscripts/website-redirects
// @homepageURL https://github.com/0x96EA/userscripts
// @license MIT
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
  'use strict';

  const logPrefix = '[Website Redirects]';
  const logger = {
    error: console.error.bind(console, logPrefix),
    log: console.log.bind(console, logPrefix),
    // NOTE: debug logging is opt in
    info: localStorage.getItem('userscript-addon-logging')
      ? console.info.bind(console, logPrefix)
      : () => {},
    warn: localStorage.getItem('userscript-addon-logging')
      ? console.warn.bind(console, logPrefix)
      : () => {},
  };

  logger.info('Starting...');

  // Define your redirects here
  const redirects = {
    // 'reddit.com': 'https://old.reddit.com',
    // Add more redirects as needed
  };

  // Get the current host and pathname
  const {
    host: currentHost,
    pathname: currentPath,
    search: currentSearch,
    href: currentUrl,
  } = window.location;

  logger.info(`Current host: ${currentHost}`);
  logger.info(`Current URL: ${currentUrl}`);

  // Function to check if current host matches any redirect pattern
  function findRedirectUrl(host) {
    // First try exact match
    if (redirects[host]) {
      return redirects[host];
    }

    // Then try matching without subdomain (e.g., www.reddit.com -> reddit.com)
    for (const redirectHost in redirects) {
      if (host.endsWith(`.${redirectHost}`) || host === redirectHost) {
        return redirects[redirectHost];
      }
    }

    return null;
  }

  // Check if the current host matches any redirect
  const redirectBaseUrl = findRedirectUrl(currentHost);

  if (redirectBaseUrl) {
    // Construct the new URL with the original path and query parameters
    const fullRedirectUrl = `${redirectBaseUrl}${currentPath}${currentSearch}`;

    // Prevent infinite redirects by checking if we're already on the target domain
    const redirectHostname = new URL(redirectBaseUrl).hostname;
    if (currentHost === redirectHostname) {
      logger.info(
        `Already on target domain ${redirectHostname}, skipping redirect`,
      );
      return;
    }

    logger.log(`Redirecting...\nFrom: ${currentUrl}\nTo: ${fullRedirectUrl}`);

    // Redirect to the new location
    window.location.replace(fullRedirectUrl);
  } else {
    logger.warn(`No redirect found for host: ${currentHost}`);
  }
})();