-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchiveorg_link_restorer.js
76 lines (69 loc) · 2.29 KB
/
archiveorg_link_restorer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const metascraper = require("metascraper")([require("metascraper-date")()]);
const fetch = require("node-fetch");
const targetUrl = window.location.href;
function dateToYMDHH(date) {
var s = date.getSeconds();
var min = date.getMinutes();
var h = date.getHours();
var d = date.getDate();
var m = date.getMonth() + 1; //Month from 0 to 11
var y = date.getFullYear();
return (
"" +
y +
"" +
(m <= 9 ? "0" + m : m) +
"" +
(d <= 9 ? "0" + d : d) +
"" +
(h <= 9 ? "0" + h : h) +
(min <= 9 ? "0" + min : min) +
(s <= 9 ? "0" + s : s)
);
}
if (targetUrl.match(/(.*\.?stackexchange.com)|stackoverflow\.com\//gm)) {
matchStackExchange();
} else {
fallbackMetascraper();
}
function matchStackExchange() {
let answers = document
.getElementById("answers")
.getElementsByClassName("answer");
for (let i = 0; i < answers.length; i++) {
let answer = answers[i];
let answerDate = answer
.getElementsByTagName("time")[0]
.getAttribute("datetime");
let answerLinks = answer.getElementsByTagName("a");
for (let j = 0; j < answerLinks.length; j++) {
let answerLink = answerLinks[j].getAttribute("href");
if (answerLink.startsWith("http")) {
let archiveurl = "https://web.archive.org/web/" +
dateToYMDHH(new Date(answerDate)) +
"/" +
answerLink;
answerLinks[j].setAttribute("href", archiveurl);
}
}
}
}
function fallbackMetascraper() {
(async () => {
fetch(targetUrl)
.then((res) => res.text())
.then((body) => {
const metadata = metascraper({ url: targetUrl, html: body });
metadata.then(function (result) {
var links = document.getElementsByTagName("a");
for (let i = 0; i < links.length; i++) {
let archiveurl = "https://web.archive.org/web/" +
dateToYMDHH(new Date(result.date)) +
"/" +
links[i].getAttribute("href");
links[i].setAttribute("href", archiveurl);
}
});
});
})();
}