404 to Archive Redirecter

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

目前为 2025-04-09 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 404 to Archive Redirecter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Detects 404/Not Found pages and redirects to the archived version on archive.org
  6. // @author Patryk Kordisch
  7. // @match *://*/*
  8. // @run-at document-end
  9. // @exclude *://web.archive.org/*
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. function is404Page() {
  18.  
  19. // Check HTTP Status Code using Performance API
  20. const navEntries = window.performance.getEntriesByType('navigation');
  21. if (navEntries.length > 0 && navEntries[0].responseStatus === 404) {
  22. return true;
  23. }
  24. return false;
  25. }
  26.  
  27.  
  28. // Confirmation dialog
  29. function offerRedirect(archiveUrl) {
  30. if (confirm(`This appears to be a missing page.\n\nWould you like to view an archived version?`)) {
  31. window.location.href = archiveUrl;
  32. }
  33. }
  34.  
  35.  
  36. // If the current page qualifies as a 404 and user confirms it, he gets redirected to the archive
  37. setTimeout(() => {
  38. if (is404Page()) {
  39. // Get URL
  40. const currentUrl = window.location.href;
  41. const archiveUrl = "https://web.archive.org/web/" + currentUrl;
  42.  
  43. setTimeout(() => offerRedirect(archiveUrl), 500);
  44. }
  45. }, 2000);
  46.  
  47. })();