Web Archive Helper

Add options to search or save pages on web.archive.org and archive.today

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Web Archive Helper
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Add options to search or save pages on web.archive.org and archive.today
// @author       maanimis
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_openInTab
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  function showWebArchive(url) {
    GM_openInTab("https://web.archive.org/web/*/" + url, { active: true });
  }

  function saveWebArchive(url) {
    if (confirm("Do you want to save this page to Web Archive?")) {
      let form = document.createElement("form");
      form.method = "POST";
      form.action = "https://web.archive.org/save/";
      form.target = "_blank";

      let inputs = [
        { name: "url", value: url },
        { name: "capture_outlinks", value: "1" },
        { name: "capture_all", value: "on" },
        { name: "capture_screenshot", value: "on" },
      ];

      inputs.forEach((data) => {
        let input = document.createElement("input");
        input.type = "hidden";
        input.name = data.name;
        input.value = data.value;
        form.appendChild(input);
      });

      document.body.appendChild(form);
      form.submit();
      document.body.removeChild(form);
    }
  }

  function showArchiveToday(url) {
    GM_openInTab("https://archive.today/search/?q=" + encodeURIComponent(url), {
      active: true,
    });
  }

  function saveArchiveToday(url) {
    GM_openInTab(
      "https://archive.today/?run=1&url=" + encodeURIComponent(url),
      { active: true }
    );
  }

  function saveInBoth(url) {
    saveWebArchive(url);
    saveArchiveToday(url);
  }

  function searchInBoth(url) {
    showWebArchive(url);
    showArchiveToday(url);
  }

  // Web Archive Commands
  GM_registerMenuCommand("Web Archive: Show this page", function () {
    showWebArchive(window.location.href);
  });

  GM_registerMenuCommand("Web Archive: Save this page", function () {
    saveWebArchive(window.location.href);
  });

  // Archive.today Commands
  GM_registerMenuCommand("Archive.today: Search this page", function () {
    showArchiveToday(window.location.href);
  });

  GM_registerMenuCommand("Archive.today: Save this page", function () {
    saveArchiveToday(window.location.href);
  });

  GM_registerMenuCommand("===============", () => {});

  GM_registerMenuCommand("Save on Both Archives", function () {
    saveInBoth(window.location.href);
  });

  GM_registerMenuCommand("Search on Both Archives", function () {
    searchInBoth(window.location.href);
  });
})();