From 459e8342887ef0f986b52a9287916e5af9289a81 Mon Sep 17 00:00:00 2001 From: tdro Date: Thu, 27 Apr 2023 16:10:15 -0400 Subject: static/js/refresh -> static/js/update --- static/js/index.ts | 2 +- static/js/refresh.ts | 40 ---------------------------------------- static/js/update.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 41 deletions(-) delete mode 100644 static/js/refresh.ts create mode 100644 static/js/update.ts (limited to 'static') diff --git a/static/js/index.ts b/static/js/index.ts index 6de3166..af805ae 100644 --- a/static/js/index.ts +++ b/static/js/index.ts @@ -1,5 +1,5 @@ import "./pager.ts"; -import "./refresh.ts"; +import "./update.ts"; import "./plumber.ts"; import "./instantpage.ts"; import "./contextmenu.ts"; diff --git a/static/js/refresh.ts b/static/js/refresh.ts deleted file mode 100644 index 2b96751..0000000 --- a/static/js/refresh.ts +++ /dev/null @@ -1,40 +0,0 @@ -(function () { - function check(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]; - check(url, "HEAD", function (request) { - const local = document.querySelector('meta[name="last-modified"]').content; - const remote = request.getResponseHeader("last-modified") || local; - const modified = Date.parse(document.lastModified) !== Date.parse(remote); - const drift = Date.parse(remote) - Date.parse(local); - - if (modified) { - const indicator = document.querySelector("a[data-update]"); - check(url, "GET"); - indicator.href = url; - indicator.removeAttribute("id"); - indicator.dataset.update = "refresh"; - self.removeEventListener("blur", update); - console.log("R: " + remote); - console.log("L: " + local); - console.log("D: " + drift); - console.log("M: " + modified); - } - }); - } - - self.addEventListener("blur", update); -})(); diff --git a/static/js/update.ts b/static/js/update.ts new file mode 100644 index 0000000..2b96751 --- /dev/null +++ b/static/js/update.ts @@ -0,0 +1,40 @@ +(function () { + function check(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]; + check(url, "HEAD", function (request) { + const local = document.querySelector('meta[name="last-modified"]').content; + const remote = request.getResponseHeader("last-modified") || local; + const modified = Date.parse(document.lastModified) !== Date.parse(remote); + const drift = Date.parse(remote) - Date.parse(local); + + if (modified) { + const indicator = document.querySelector("a[data-update]"); + check(url, "GET"); + indicator.href = url; + indicator.removeAttribute("id"); + indicator.dataset.update = "refresh"; + self.removeEventListener("blur", update); + console.log("R: " + remote); + console.log("L: " + local); + console.log("D: " + drift); + console.log("M: " + modified); + } + }); + } + + self.addEventListener("blur", update); +})(); -- cgit v1.2.3