404toArchive

Redirects from a 404 page to an archive backup when possible

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

Advertisement:

// ==UserScript==
// @name         404toArchive
// @namespace    https://github.com/aslkeaxn/404toArchive
// @version      0.2
// @description  Redirects from a 404 page to an archive backup when possible
// @author       aslkeaxn
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion:8 */

(function () {
  "use strict";

  async function main() {
    const url = location.href;
    const res1 = await fetch(url, { method: "HEAD" });

    if (res1.status !== 404) return;

    const encodedUrl = encodeURI(url);
    const archiveUrl = `https://archive.org/wayback/available?url=${encodedUrl}`;
    const res2 = await fetch(archiveUrl);
    const json = await res2.json();
    const closestSnapshot = json.archived_snapshots.closest;

    if (!closestSnapshot || !closestSnapshot.available) return;

    window.location.replace(closestSnapshot.url);
  }

  main().catch(console.error);
})();