🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Website Redirects

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 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}`);
  }
})();