Redirect fakeptt to Ptt

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
  }
})