404 to Archive Redirecter

Detects 404/Not Found pages and redirects to the archived version on archive.org

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         404 to Archive Redirecter
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Detects 404/Not Found pages and redirects to the archived version on archive.org
// @author       Patryk Kordisch
// @match        *://*/*
// @run-at       document-end
// @exclude      *://web.archive.org/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function is404Page() {

        // Check HTTP Status Code using Performance API
        const navEntries = window.performance.getEntriesByType('navigation');
        if (navEntries.length > 0 && navEntries[0].responseStatus === 404) {
            return true;
        }
        return false;
    }


    // Confirmation dialog
    function offerRedirect(archiveUrl) {
        if (confirm(`This appears to be a missing page.\n\nWould you like to view an archived version?`)) {
            window.location.href = archiveUrl;
        }
    }


    // If the current page qualifies as a 404 and user confirms it, he gets redirected to the archive
    setTimeout(() => {
        if (is404Page()) {
            // Get URL
            const currentUrl = window.location.href;
            const archiveUrl = "https://web.archive.org/web/" + currentUrl;

            setTimeout(() => offerRedirect(archiveUrl), 500);
        }
    }, 2000);

})();