PTT 404 Redirect to PTTWeb

PTT 網頁 404 的時候 導頁到 PTTWeb

// ==UserScript==
// @name         PTT 404 Redirect to PTTWeb
// @namespace    https://github.com/livinginpurple
// @version      2023.01.31.01
// @description  PTT 網頁 404 的時候 導頁到 PTTWeb
// @license      WTFPL
// @author       livinginpurple
// @match        https://*.ptt.cc/*
// @run-at       document-start
// @grant        none
// @grant        GM.xmlHttpRequest
// ==/UserScript==

(function() {
    'use strict';
    const scriptName = GM_info.script.name;
    console.log(`${scriptName} is loading.`);

    function urlExists(url, callback) {
        fetch(url, { method: 'head' })
            .then(function (status) {
                callback(status.ok)
            });
    }

    let url = document.location.href;

    urlExists(url, function (exists) {
        if (exists) {
            // it exists, do something
        } else {
            location.href = url.replace('ptt', 'pttweb');
        }
    });

    console.log(`${scriptName} is running.`);
})();