-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (94 loc) · 3.37 KB
/
script.js
File metadata and controls
103 lines (94 loc) · 3.37 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(function (win, doc, nav) {
function initConsentBanner_() {
var consentBanner = doc.getElementById("consent-banner");
if (consentBanner) {
var button = consentBanner.querySelector("button");
if (button) {
button.onclick = function () {
// 7 days * 24 hours * 60 minutes * 60 seconds = 604800.
doc.cookie =
"consent=1; max-age=604800; path=/; samesite=strict; secure";
consentBanner.style.display = "none";
initIOSInstallPrompt_(consentBanner);
};
}
if (!doc.cookie.includes("consent=1")) {
consentBanner.style.display = "block";
}
}
return consentBanner;
}
function initIOSInstallPrompt_(consentBanner) {
var isStandalone =
("standalone" in nav && nav.standalone) ||
win.matchMedia("(display-mode: standalone)").matches;
if (/ipad|iphone|ipod/.test(nav.userAgent.toLowerCase()) && !isStandalone) {
if (!(consentBanner && consentBanner.style.display === "block")) {
var installPrompt = doc.getElementById("ios-pwa-prompt");
if (installPrompt) {
installPrompt.style.display = "block";
}
}
}
}
function initArchiveImages_(pathname) {
if (pathname.startsWith("/archive/")) {
var re = /^\/archive\/(\d+\/\d+\/\d+)/i;
var date = (pathname.match(re) || [])[1];
function onerror(e) {
// e.target.parentNode.style.display = "none";
e.target.parentNode.style.opacity = ".75";
e.target.parentNode.style.background = "darkgray";
}
if (date) {
var uri = "https://web.archive.org/web/";
// https://majordigest.com/assets/images/category/YYYY/MM/DD/
var assets = "https://majordigest.com/assets/images/";
var prefix = uri + date.replace(/\D+/g, "") + "im_/";
document.querySelectorAll(".card .image img").forEach(function (img) {
img.onerror = onerror;
if (img.src.startsWith(assets)) {
img.src = prefix + img.src;
} else if (img.complete && img.naturalHeight === 0) {
onerror({ target: img });
}
});
}
}
}
function updateHeader_() {
if (!location.pathname.startsWith("/archive/")) {
var format = win.Intl && Intl.DateTimeFormat && Intl.DateTimeFormat();
var options =
format && format.resolvedOptions && format.resolvedOptions();
var timeZone = (options && options.timeZone) || "";
var city = timeZone.split("/").pop().replace(/_+/g, " ");
city && doc.querySelector("header span").append(city);
doc.querySelector("header time").textContent =
new Date().toLocaleDateString("en-us", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
});
}
}
function init_() {
updateHeader_();
if ("majordigest.com" === location.hostname) {
initIOSInstallPrompt_(initConsentBanner_());
"serviceWorker" in nav && nav.serviceWorker.register("/sw.js");
}
doc
.querySelectorAll('a[href="' + location.pathname + '"]')
.forEach(function (link) {
link.className = "active";
});
initArchiveImages_(location.pathname);
var year = new Date().getFullYear();
doc.querySelectorAll(".year").forEach(function (node) {
node.textContent = year;
});
}
init_();
})(window, document, navigator);