Advertisement
jcunews

auto-check-page-archive-in-archive.today.user.js

Sep 1st, 2022
2,254
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Auto-check page archive in archive.today
  3. // @namespace    https://greasyfork.org/en/users/85671-jcunews
  4. // @version      1.0.1
  5. // @license      AGPLv3
  6. // @author       jcunews
  7. // @description  Context: https://www.reddit.com/r/userscripts/comments/x191e2/userscript_to_automatically_rearchive_websites_in/
  8. // @match        *://*/*
  9. // @connect      archive.ph
  10. // @grant        GM_xmlhttpRequest
  11. // ==/UserScript==
  12.  
  13. ((arcDomain, h) => {
  14.  
  15.   //-----CONFIG BEGIN
  16.  
  17.   //the domain name of below setting and the above @connect metadata MUST be identical.
  18.   arcDomain = "archive.ph";
  19.  
  20.   //-----CONFIG END
  21.  
  22.   if (!location.protocol.startsWith("http") || (document.contentType !== "text/html")) return;
  23.   h = `\
  24. <div id=atnb_ujs style="all:revert;position:fixed;z-index:999999999;top:0;right:0;border-left:1px solid #777;border-bottom:1px solid #777;padding:.2em;background:#eeb;
  25.  color:#000;font:10pt/normal sans-serif"
  26. >
  27.   <div style="float:right;margin-left:.2em;border-radius:.2em;width:1.2em;background:#c00;color:#fff;text-align:center;font-weight:bold;cursor:pointer"
  28.     onclick="atnb_ujs.remove()"
  29.   >X</div>
  30.   <span id=atnbm_ujs></span>
  31. </div>`;
  32.   function showError(x, m) {
  33.     document.body.insertAdjacentHTML("beforeend", h);
  34.     atnbm_ujs.textContent = `${m}. ${
  35.       x.status ? `HTTP error code ${x.status}.${ x.statusText ? ` ${x.statusText}.`: ""}` : "Connection failed."
  36.     }`
  37.   }
  38.   function checkFail(x) { showError(x, "Failed on checking page archive") }
  39.   function accessFail(x) {
  40.     alert(`Failed on accessing archive service site. ${
  41.       x ? (
  42.         x.status ? `HTTP error code ${x.status}.${ x.statusText ? ` ${x.statusText}.`: ""}` : "Connection failed."
  43.       ) : "Submission ID is not found."
  44.     }`)
  45.   }
  46.   function elapsed(t, n, s) {
  47.     if ((s = Math.floor(((n = new Date) - t) / 1000)) < 60) {
  48.       return s + " seconds"
  49.     } else if ((s /= 60) < 60) {
  50.       return Math.round(s) + " minutes"
  51.     } else if ((s /= 60) < 60) {
  52.       return Math.round(s) + " hours"
  53.     } else if ((s /= 24) < 7) {
  54.       return Math.round(s) + " days"
  55.     } else if (s < 30) {
  56.       return Math.round(s / 7) + " weeks"
  57.     } else {
  58.       s = ((n.getFullYear() * 12) + n.getMonth() + 1) - ((t.getFullYear() * 12) + t.getMonth() + 1);
  59.       return s < 12 ? s + " months" : Math.round(s / 12) + " years"
  60.     }
  61.   }
  62.   GM_xmlhttpRequest({
  63.     method: "GET",
  64.     url: "https://${arcDomain}/" + location.href.match(/[^#]+/)[0],
  65.     headers: {"Cache-Control": "no-cache", Pragma: "no-cache"},
  66.     onerror: checkFail,
  67.     onload: (x, c, t) => {
  68.       if (x.status < 400) {
  69.         (c = document.createElement("DIV")).innerHTML = x.responseText;
  70.         t = new Date(0);
  71.         c.querySelectorAll('.THUMBS-BLOCK').forEach((e, s) => {
  72.           e = e.querySelectorAll('a div');
  73.           if (e.length && ((s = new Date(e[e.length - 1].textContent + " GMT")) > t)) t = s
  74.         });
  75.         document.body.insertAdjacentHTML("beforeend", h);
  76.         if (t.getTime() > 0) {
  77.           atnbm_ujs.innerHTML = `\
  78. This page was last archived ${elapsed(t)} ago. \
  79. If this snapshot looks obsolete you can <a id=atnbs_ujs href="javascript:void(0)">save</a> this page again.`
  80.         } else atnbm_ujs.innerHTML = `This page has not yet been archived. You can <a id=atnbs_ujs href="javascript:void(0)">save</a> this page.`;
  81.         atnbs_ujs.onclick = () => {
  82.           atnb_ujs.remove();
  83.           GM_xmlhttpRequest({
  84.             method: "GET",
  85.             url: "https://${arcDomain}/",
  86.             onerror: accessFail,
  87.             onload: (x, c, t) => {
  88.               if (x.status < 400) {
  89.                 (c = document.createElement("DIV")).innerHTML = x.responseText;
  90.                 if (c = c.querySelector('#submiturl input[name="submitid"]')) {
  91.                   open(`https://${arcDomain}/submit?submitId=${encodeURIComponent(c.value)}&url=${encodeURIComponent(location.href.match(/[^#]+/)[0])}`)
  92.                 } else accessFail()
  93.               } else accessFail(x)
  94.             }
  95.           })
  96.         }
  97.       } else checkFail(x)
  98.     }
  99.   })
  100. })();
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement