Redirect fakeptt to Ptt

Redirect some fakeptt pages which couldn't replace by regular expression to Ptt

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Redirect fakeptt to Ptt
// @namespace   Violentmonkey Scripts
// @description Redirect some fakeptt pages which couldn't replace by regular expression to Ptt
// @match       https://ptthito.com/*
// @match       https://pttview.com/*
// @match       http://pttweb.com/*
// @match       https://pttweb.tw/*
// @match       http://pttread.com/*
// @match       http://hotptt.com/*
// @version     0.2.4
// @grant       none
// @run-at      document-start
// ==/UserScript==

"use strict"

let url = window.location.href;
let pattern = /https?:\/\/(?:(?:pttview|pttweb|ptthito|pttread)\.com|pttweb\.tw)\/([\w\-]+)\/m-(\d+)-a-([0-9a-z]{3}).*/i;
let match_group = url.match(pattern);

if (match_group !== null) {
  url = `https://www.ptt.cc/bbs/${match_group[1]}/M.${match_group[2]}.A.${match_group[3].toUpperCase()}.html`;
  window.location.replace(url);
}

document.addEventListener("DOMContentLoaded", function () {
  let pttUrl = '';
  let elements = document.querySelectorAll(".f2");

  for (let i = 0; i < elements.length; i++) {
    let pattern = /(https:\/\/www\.ptt\.cc\/bbs\/[\w\-]+\/M\.\d{10}\.A\.[0-9A-Z]{3}.html)/i;
    let match_group = elements[i].innerText.match(pattern);
    if (match_group) {
      pttUrl = match_group[0];
    }
  }
  if (pttUrl) {
    window.location.replace(pttUrl);
  }
})