Files
dsh-web/internal/api/runtime_session.go
T

53 lines
2.0 KiB
Go

package api
import "net/http"
func runtimeSessionScript(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
fail(w, 405, "method not allowed")
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Write([]byte(runtimeSessionJS))
}
const runtimeSessionJS = `(function () {
const prefix = location.pathname.match(/^\/dsh\/[^/]+(?=\/|$)/);
if (prefix) {
const originalFetch = window.fetch;
const bridge = /^\/api\/dsh-free-search-settings\/(describe|mutate|credentials-status|credentials-set|credentials-unset|check-update|update|raw-search)$/;
window.fetch = async function (input, init) {
let url;
try { url = new URL(input instanceof Request ? input.url : input, document.baseURI); } catch (_) {}
if (url && url.origin === location.origin && bridge.test(url.pathname)) {
url.pathname = prefix[0] + url.pathname;
if (input instanceof Request) {
const request = new Request(input, init);
return originalFetch.call(this, new Request(url, request));
}
input = url;
}
return originalFetch.call(this, input, init);
};
}
let pending;
async function renew() {
if (pending) return pending;
pending = (async function () {
const csrf = document.cookie.split("; ").find(c => c.startsWith("dsh_csrf="));
if (!csrf) return;
const response = await fetch("/api/refresh", {method:"POST", credentials:"same-origin", headers:{"X-CSRF-Token":csrf.slice(9)}});
if (response.status === 401 || response.status === 403) {
const check = await fetch("/api/me", {credentials:"same-origin"});
if (check.status === 401) location.reload();
}
})().catch(function () {}).finally(function () { pending = undefined; });
return pending;
}
setInterval(renew, 240000);
window.addEventListener("pageshow", renew);
window.addEventListener("online", renew);
document.addEventListener("visibilitychange", function () { if (!document.hidden) renew(); });
})();`