Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Auto-check page archive in archive.today
- // @namespace https://greasyfork.org/en/users/85671-jcunews
- // @version 1.0.1
- // @license AGPLv3
- // @author jcunews
- // @description Context: https://www.reddit.com/r/userscripts/comments/x191e2/userscript_to_automatically_rearchive_websites_in/
- // @match *://*/*
- // @connect archive.ph
- // @grant GM_xmlhttpRequest
- // ==/UserScript==
- ((arcDomain, h) => {
- //-----CONFIG BEGIN
- //the domain name of below setting and the above @connect metadata MUST be identical.
- arcDomain = "archive.ph";
- //-----CONFIG END
- if (!location.protocol.startsWith("http") || (document.contentType !== "text/html")) return;
- h = `\
- <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;
- color:#000;font:10pt/normal sans-serif"
- >
- <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"
- onclick="atnb_ujs.remove()"
- >X</div>
- <span id=atnbm_ujs></span>
- </div>`;
- function showError(x, m) {
- document.body.insertAdjacentHTML("beforeend", h);
- atnbm_ujs.textContent = `${m}. ${
- x.status ? `HTTP error code ${x.status}.${ x.statusText ? ` ${x.statusText}.`: ""}` : "Connection failed."
- }`
- }
- function checkFail(x) { showError(x, "Failed on checking page archive") }
- function accessFail(x) {
- alert(`Failed on accessing archive service site. ${
- x ? (
- x.status ? `HTTP error code ${x.status}.${ x.statusText ? ` ${x.statusText}.`: ""}` : "Connection failed."
- ) : "Submission ID is not found."
- }`)
- }
- function elapsed(t, n, s) {
- if ((s = Math.floor(((n = new Date) - t) / 1000)) < 60) {
- return s + " seconds"
- } else if ((s /= 60) < 60) {
- return Math.round(s) + " minutes"
- } else if ((s /= 60) < 60) {
- return Math.round(s) + " hours"
- } else if ((s /= 24) < 7) {
- return Math.round(s) + " days"
- } else if (s < 30) {
- return Math.round(s / 7) + " weeks"
- } else {
- s = ((n.getFullYear() * 12) + n.getMonth() + 1) - ((t.getFullYear() * 12) + t.getMonth() + 1);
- return s < 12 ? s + " months" : Math.round(s / 12) + " years"
- }
- }
- GM_xmlhttpRequest({
- method: "GET",
- url: "https://${arcDomain}/" + location.href.match(/[^#]+/)[0],
- headers: {"Cache-Control": "no-cache", Pragma: "no-cache"},
- onerror: checkFail,
- onload: (x, c, t) => {
- if (x.status < 400) {
- (c = document.createElement("DIV")).innerHTML = x.responseText;
- t = new Date(0);
- c.querySelectorAll('.THUMBS-BLOCK').forEach((e, s) => {
- e = e.querySelectorAll('a div');
- if (e.length && ((s = new Date(e[e.length - 1].textContent + " GMT")) > t)) t = s
- });
- document.body.insertAdjacentHTML("beforeend", h);
- if (t.getTime() > 0) {
- atnbm_ujs.innerHTML = `\
- This page was last archived ${elapsed(t)} ago. \
- If this snapshot looks obsolete you can <a id=atnbs_ujs href="javascript:void(0)">save</a> this page again.`
- } 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.`;
- atnbs_ujs.onclick = () => {
- atnb_ujs.remove();
- GM_xmlhttpRequest({
- method: "GET",
- url: "https://${arcDomain}/",
- onerror: accessFail,
- onload: (x, c, t) => {
- if (x.status < 400) {
- (c = document.createElement("DIV")).innerHTML = x.responseText;
- if (c = c.querySelector('#submiturl input[name="submitid"]')) {
- open(`https://${arcDomain}/submit?submitId=${encodeURIComponent(c.value)}&url=${encodeURIComponent(location.href.match(/[^#]+/)[0])}`)
- } else accessFail()
- } else accessFail(x)
- }
- })
- }
- } else checkFail(x)
- }
- })
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement