404 to Archive Redirecter

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

目前為 2025-04-15 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    });
})();