Website Redirects

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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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}`);
  }
})();