blob: 94a509a419f963faa4f95f3ab98df12fe8c06600 (
plain)
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
|
(function () {
function fetch(url, method, callback) {
const http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
if (callback) callback(http);
}
};
http.open(method, url);
http.setRequestHeader("Pragma", "no-cache");
http.setRequestHeader("Cache-Control", "no-cache");
http.send();
return http;
}
function update() {
const url = self.location.href.split("#")[0].split("?")[0];
fetch(url, "HEAD", function (request) {
const local = document.querySelector('meta[name="last-modified"]').content;
const remote = request.getResponseHeader("last-modified") || '';
const modified = Date.parse(document.lastModified) !== Date.parse(remote);
const drift = Date.parse(remote || local) - Date.parse(local);
let indicator = document.querySelector("a[data-update]");
const anchor = indicator.cloneNode();
function reset() {
indicator.href = anchor.href;
indicator.setAttribute("id", anchor.id);
indicator.dataset.update = anchor.dataset.update;
}
if (drift === 0) return;
if (remote && modified) {
fetch(url, "GET");
indicator.href = url;
indicator.removeAttribute("id");
indicator.dataset.update = "refresh";
self.removeEventListener("blur", update);
console.log("INFO: R: " + remote);
console.log("INFO: L: " + local);
console.log("INFO: D: " + drift);
console.log("INFO: M: " + modified);
}
});
}
self.addEventListener("blur", update);
})();
|