Added service worker support, needs work

You must manually enable support, I'm not having it on untill it'll work without issues all of the time :P
This commit is contained in:
MathMan05 2024-06-17 16:16:57 -05:00
parent 26f106c517
commit c794ae46c7
9 changed files with 126 additions and 14 deletions

View file

@ -4,7 +4,15 @@ const express = require('express');
const fs = require('fs');
const app = express();
app.use("/getupdates",(req, res) => {
const out=fs.statSync(`${__dirname}/webpage`);
res.send(out.mtimeMs+"");
});
let debugging=true;
app.use('/', (req, res) => {
if(debugging&&req.path.startsWith("/service.js")){
res.send("console.log(\"Hi :3\")");
}
if(fs.existsSync(`${__dirname}/webpage${req.path}`)) {
res.sendFile(`./webpage${req.path}`, {root: __dirname});
}
@ -16,6 +24,7 @@ app.use('/', (req, res) => {
}
});
const PORT = process.env.PORT || +process.argv[1] || 8080;
app.listen(PORT, () => {});
console.log("this ran :P");

View file

@ -59,7 +59,6 @@ class group extends channel{
this.owner=owner;
this.headers=this.owner.headers;
this.messages=[];
console.log(JSON.recipients,JSON)
this.name=JSON.recipients[0]?.username;
if(JSON.recipients[0]){
this.user=new user(JSON.recipients[0]);
@ -137,7 +136,6 @@ class group extends channel{
current=thing;
}
}
console.log(current,this.hasunreads);
if(this.hasunreads){
if(current){current.noti.innerText=this.mentions;return;}
const div=document.createElement("div");
@ -159,10 +157,10 @@ class group extends channel{
this.all.getHTML();
}
}else if(current){
console.log("remove")
current.remove();
}else{
console.log(sentdms.children)
}
}
}

View file

@ -30,7 +30,6 @@ class guild{
if(JSON===-1){
return;
}
console.log(JSON);
this.owner=owner;
this.headers=this.owner.headers;
if(!this.owner){
@ -139,7 +138,6 @@ class guild{
break;
}
}
console.log(read);
if(!html){return;}
if(read){
html.children[0].classList.remove("notiunread");

View file

@ -10,7 +10,7 @@
<p id="verify"></p>
<input type="text" placeholder="Instance URL" name="instance" value="https://spacebar.chat/" id="instancein" required><br><br>
<label for="uname"><b>Username:</b></label><br>
<label for="uname"><b>Email:</b></label><br>
<input type="text" placeholder="Enter Username" name="uname" required><br><br>
<label for="psw"><b>Password:</b></label><br>

View file

@ -34,8 +34,8 @@ function setDefaults(){
setDefaults();
class specialuser{
constructor(json){
if(typeof json==="specialuser"){
return json;
if(json instanceof specialuser){
console.error("specialuser can't construct from another specialuser");
}
this.serverurls=json.serverurls;
this.serverurls.api=new URL(this.serverurls.api);
@ -184,3 +184,27 @@ async function check(e){
if(document.getElementById("form")){
document.getElementById("form").addEventListener("submit", check);
}
//Service workers :3
if ("serviceWorker" in navigator){
navigator.serviceWorker.register("/service.js", {
scope: "/",
}).then((registration) => {
let serviceWorker;
if (registration.installing) {
serviceWorker = registration.installing;
console.log("installing");
} else if (registration.waiting) {
serviceWorker = registration.waiting;
console.log("waiting");
} else if (registration.active) {
serviceWorker = registration.active;
console.log("active");
}
if (serviceWorker) {
console.log(serviceWorker.state);
serviceWorker.addEventListener("statechange", (e) => {
console.log(e.target.state);
});
}
})
}

View file

@ -37,14 +37,12 @@ class cmessage{
},null,_=>{return _.author.id==READY.d.user.id});
}
constructor(messagejson,owner){
console.log(owner)
this.owner=owner;
this.headers=this.owner.headers;
for(const thing of Object.keys(messagejson)){
this[thing]=messagejson[thing];
}
this.author=new user(this.author);
console.log(this.type)
}
messageevents(obj){
cmessage.contextmenu.bind(obj,this)
@ -182,7 +180,6 @@ class cmessage{
messagedwrap.append(time);
texttxt.appendChild(messagedwrap)
console.log(div)
}
div.userid=this.author.id;
div.all=this;

87
webpage/service.js Normal file
View file

@ -0,0 +1,87 @@
function deleteoldcache(){
caches.delete("cache");
console.log("this ran :P")
}
async function putInCache(request, response){
console.log(request,response);
const cache = await caches.open('cache');
console.log("Grabbed")
try{
console.log(await cache.put(request, response));
}catch(error){
console.error(error);
}
};
console.log("test");
let lastcache
self.addEventListener("activate", async (event) => {
console.log("test2");
checkCache();
})
async function checkCache(){
if(checkedrecently){
return;
}
promise=await caches.match("/getupdates");
if(promise){
lastcache= await promise.text();
}
console.log(lastcache);
fetch("/getupdates").then(async data=>{
text=await data.clone().text();
console.log(text,lastcache)
if(lastcache!==text){
deleteoldcache();
putInCache("/getupdates",data.clone());
}
checkedrecently=true;
setTimeout(_=>{checkedrecently=false},1000*60*30);
})
}
var checkedrecently=false;
function samedomain(url){
return new URL(url).origin===self.origin;
}
function isindexhtml(url){
console.log(url);
if(new URL(url).pathname.startsWith("/channels")){
return true;
}
return false;
}
async function getfile(event){
checkCache();
if(!samedomain(event.request.url)){
return await fetch(event.request.clone());
}
const responseFromCache = await caches.match(event.request.url);
console.log(responseFromCache,caches);
if (responseFromCache) {
console.log("cache hit")
return responseFromCache;
}
if(isindexhtml(event.request.url)){
console.log("is index.html")
const responseFromCache = await caches.match("/index.html");
if (responseFromCache) {
console.log("cache hit")
return responseFromCache;
}
const responseFromNetwork = await fetch("/index.html");
await putInCache("/index.html",responseFromNetwork.clone());
return responseFromNetwork;
}
const responseFromNetwork = await fetch(event.request.clone());
console.log(event.request.clone());
await putInCache(event.request.clone(),responseFromNetwork.clone());
try{
return responseFromNetwork;
}catch(e){console.error(e)}
}
self.addEventListener('fetch', (event) => {
try{
event.respondWith(getfile(event));
}catch(e){console.error(e)}
})

View file

@ -229,7 +229,7 @@ p {
#channels {
background-color: var(--channels-bg);
height: calc(100% - .6in);
height: calc(100vh - .9in);
width: 2.5in;
overflow: auto;
user-select: none;

View file

@ -12,7 +12,6 @@ class user{
}
constructor(userjson,dontclone=false){
if(dontclone){
console.log("new user")
for(const thing of Object.keys(userjson)){
this[thing]=userjson[thing];
}