Files
unshell/unshell-gui/assets/sw.js
T

78 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-11-26 08:55:07 -07:00
var cacheName = "egui-template-pwa";
var filesToCache = [
"./",
"./index.html",
"./eframe_template.js",
"./eframe_template_bg.wasm",
];
/* Start the service worker and cache all of the app's content */
self.addEventListener("install", function (e) {
e.waitUntil(
caches.open(cacheName).then(function (cache) {
return cache.addAll(filesToCache);
}),
);
});
/* Serve cached content when offline */
self.addEventListener("fetch", function (e) {
e.respondWith(
caches.match(e.request).then(function (response) {
return response || fetch(e.request);
}),
);
});
2025-11-28 18:39:14 -07:00
// export function httpGet(theUrl) {
// var xmlHttp = new XMLHttpRequest();
// xmlHttp.open("GET", theUrl, false); // false for synchronous request
// xmlHttp.send(null);
// return xmlHttp.responseText;
// }
2025-11-29 13:15:09 -07:00
function startHttpRequest(callback) {
2025-11-28 18:39:14 -07:00
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState !== 4) return;
2025-11-29 13:15:09 -07:00
if (xmlHttp.status == 200) callback(true, xmlHttp.responseText);
2025-12-01 09:03:17 -07:00
else if (xmlHttp.status == 401) callback(false, "Unauthorized");
else if (xmlHttp.status == 500) callback(false, "Internal Server Error");
2025-11-29 13:15:09 -07:00
else callback(false, xmlHttp.responseText);
2025-11-28 18:39:14 -07:00
};
2025-11-29 13:15:09 -07:00
return xmlHttp;
}
export function httpGet(theUrl, callback) {
var xmlHttp = startHttpRequest(callback);
2025-11-28 18:39:14 -07:00
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.send(null);
}
export function httpPost(url, body, callback) {
2025-11-29 13:15:09 -07:00
var xmlHttp = startHttpRequest(callback);
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/json");
// var data = JSON.stringify({ email: "[email protected]", password: "101010" });
xmlHttp.send(body);
}
2025-11-28 18:39:14 -07:00
2025-11-29 13:15:09 -07:00
export function httpGetAuth(theUrl, auth, callback) {
var xmlHttp = startHttpRequest(callback);
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.setRequestHeader("authorization", auth);
xmlHttp.send(null);
}
2025-11-28 18:39:14 -07:00
2025-11-29 13:15:09 -07:00
export function httpPostAuth(url, auth, body, callback) {
var xmlHttp = startHttpRequest(callback);
2025-11-28 18:39:14 -07:00
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/json");
2025-11-29 13:15:09 -07:00
xmlHttp.setRequestHeader("authorization", auth);
2025-11-28 18:39:14 -07:00
// var data = JSON.stringify({ email: "[email protected]", password: "101010" });
xmlHttp.send(body);
}