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

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

// ==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();
})();