404 to Archive Redirecter

Redirects to the most recent Internet Archive snapshot if a page shows 404 or Not Found error.

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         404 to Archive Redirecter
// @namespace    https://github.com/MathiasHM
// @version      1.1
// @description  Redirects to the most recent Internet Archive snapshot if a page shows 404 or Not Found error.
// @author       Mathias H. M.
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const currentURL = window.location.href;
    const archiveAPI = 'https://archive.org/wayback/available?url=';

    // Prevent redirect loop
    if (window.location.hostname.includes('web.archive.org')) return;

    const notFoundIndicators = [
        '404', 'not found', 'page not found', 'error 404',
        'requested url was not found', 'this page does not exist',
        'sorry, we couldn’t find', 'file not found', 'oops!', 'cannot be found'
    ];

    function isPage404() {
        const text = document.body?.innerText?.toLowerCase() || '';
        const title = document.title?.toLowerCase() || '';
        const url = window.location.href.toLowerCase();

        return notFoundIndicators.some(k =>
            text.includes(k) || title.includes(k) || url.includes(k)
        );
    }

    function redirectToArchiveSnapshot(snapshotUrl) {
        console.log(`[404 to Archive Redirecter] Redirecting to snapshot: ${snapshotUrl}`);
        window.location.href = snapshotUrl;
    }

    function showNoSnapshotFound() {
        alert("🚫 This page appears to be missing, and no Internet Archive snapshot was found.");
        console.warn("[404 to Archive Redirecter] No archive snapshot available.");
    }

    function checkArchive() {
        fetch(`${archiveAPI}${encodeURIComponent(currentURL)}`)
            .then(res => res.json())
            .then(data => {
                if (data?.archived_snapshots?.closest?.url) {
                    redirectToArchiveSnapshot(data.archived_snapshots.closest.url);
                } else {
                    showNoSnapshotFound();
                }
            })
            .catch(err => {
                console.error("[404 to Archive Redirecter] Error querying archive:", err);
            });
    }

    // Wait until the page loads before checking
    window.addEventListener('load', () => {
        setTimeout(() => {
            if (isPage404()) {
                console.log("[404 to Archive Redirecter] Page appears to be missing. Checking archive...");
                checkArchive();
            }
        }, 1000);
    });
})();