File size: 2,258 Bytes
01fcadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
importScripts("/uv/uv.bundle.js");
importScripts("/uv/uv.config.js");
importScripts("/workerware/workerware.js");
importScripts(__uv$config.sw || "/uv/uv.sw.js");
const uv = new UVServiceWorker();
const ww = new WorkerWare({ debug: false });

//me when Firefox (thanks vk6)
if (navigator.userAgent.includes("Firefox")) {
    Object.defineProperty(globalThis, "crossOriginIsolated", {
        value: true,
        writable: true
    });
}

//where we handle our plugins!!!
self.addEventListener("message", function (event) {
    console.log(event.data);
    uv.config.inject = [];
    //loop over the required data (we don't verify here as types will take care of us :D)
    event.data.forEach((data) => {
        if (data.remove) {
            if (data.type === "page") {
                const idx = uv.config.inject.indexOf(data.host);
                uv.config.inject.splice(idx, 1);
            } else if (data.type === "serviceWorker") {
                ww.deleteByName(data.name);
            }
        } else {
            if (data.type === "page") {
                uv.config.inject.push({
                    host: data.host,
                    html: data.html,
                    injectTo: data.injectTo
                });
            } else if (data.type === "serviceWorker") {
                const wwFunction = eval(data.function);
                ww.use({
                    function: wwFunction ? wwFunction : new Function(data.function),
                    name: data.name,
                    events: data.events
                });
            } else {
                console.error("NO type exists for that. Only serviceWorker & page exist.");
                return;
            }
        }
    });
});

self.addEventListener("fetch", function (event) {
    event.respondWith(
        (async () => {
            const wwRes = await ww.run(event)();
            if (wwRes.includes(null)) {
                return;
            }
            if (event.request.url.startsWith(location.origin + __uv$config.prefix)) {
                return await uv.fetch(event);
            } else {
                return await fetch(event.request);
            }
        })()
    );
});