PTT 相關網站自動轉址到 pttweb.cc

PTT相關網站,自動轉跳到 www.pttweb.cc

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         PTT 相關網站自動轉址到 pttweb.cc
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  PTT相關網站,自動轉跳到 www.pttweb.cc
// @author       RzChan
// @match        *://www.ptt.cc/bbs/*
// @match        *://ptthito.com/*
// @match        *://disp.cc/b/*
// @icon         https://www.google.com/s2/favicons?domain=ptt.cc
// @grant        none
// @license      MIT
// ==/UserScript==

(function ()
{
	'use strict';

	const doRedirect = () =>
	{
		let pathname = window.location.pathname;
		switch (window.location.host)
		{
			case "www.ptt.cc":
				{
					break;
				}
			case "ptthito.com":
				{
					pathname = pathname.replaceAll("-", ".");
					const secondPathReg = new RegExp(/(?<=\/.+\/.*).+(?=.*\/)/g);
					const secondPath = secondPathReg.exec(pathname)[0];
					pathname = pathname.replace(/(?<=\/.+\/.*).+(?=.*\/)/g, secondPath.toUpperCase())
					pathname = pathname.replace(/\/$/g, "");
					pathname = `/bbs${pathname}`;
					break;
				}
			case "disp.cc":
				{
					const linkElement = document.evaluate("//span[contains(text(),'※ 文章網址: ')]/a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
					if (linkElement !== null)
					{
						const link = linkElement.href;
						link = link.replace("www.ptt.cc", "www.pttweb.cc");
						window.location.href = link;
					}
					return;
				}
		}

		window.location.href = `https://www.pttweb.cc${pathname}`;
	}
	doRedirect();
})();