diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json diff --git a/index.html b/index.html index 1951b8b..295f788 100644 --- a/index.html +++ b/index.html @@ -4,12 +4,13 @@ EmulatorJS | Demo - + - +

EmulatorJS Demo

- +
- + Drag ROM file or click here
- - +
@@ -245,10 +274,20 @@

Settings

Offline Status: CHECKING

-

Offline Cores will be available soon.

-

Remove All Saved Versions:

-

Install PWA:

+
Offline Cores will only use the stable release. Latest stable release is: +
Select cores to cache: +
+

Loading...

+
+
+ + +
+
+

Install PWA:

+
+
@@ -257,21 +296,125 @@

Settings

- + \ No newline at end of file diff --git a/sw.js b/sw.js index 68f07b5..8d534fe 100644 --- a/sw.js +++ b/sw.js @@ -10,16 +10,16 @@ const OFFLINE_FILES = [ "img/screenshot.png", "img/mobile_screenshot.png", "https://cdn.emulatorjs.org/versions.json", - - // Only cache stable assets offline...? "https://cdn.emulatorjs.org/stable/data/loader.js", "https://cdn.emulatorjs.org/stable/data/emulator.min.js", "https://cdn.emulatorjs.org/stable/data/emulator.min.css", + "https://cdn.emulatorjs.org/stable/data/cores/cores.json", "https://cdn.emulatorjs.org/stable/data/compression/extract7z.js", "https://cdn.emulatorjs.org/stable/data/compression/extractzip.js", "https://cdn.emulatorjs.org/stable/data/compression/libunrar.js", - "https://cdn.emulatorjs.org/stable/data/compression/libunrar.wasm" + "https://cdn.emulatorjs.org/stable/data/compression/libunrar.wasm", ]; +let cacheStatus = false; self.addEventListener("install", (event) => { event.waitUntil( @@ -35,6 +35,34 @@ self.addEventListener("activate", (event) => { self.clients.claim(); }); +self.addEventListener("message", async (event) => { + if (event.data && event.data.type === "CACHE_URLS") { + const urls = event.data.urls; + for (const url of urls) { + while (cacheStatus) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + } + cacheStatus = true; + try { + const cache = await caches.open(CACHE_NAME); + const response = await fetch(url); + if (response && response.status === 200) { + await cache.put(url, response.clone()); + } + } finally { + cacheStatus = false; + } + } + } + if (event.data && event.data.type === "REMOVE_URLS") { + const urls = event.data.urls; + const cache = await caches.open(CACHE_NAME); + for (const url of urls) { + await cache.delete(url); + } + } +}); + function getCacheUrl(url) { if (url === "/") { return "index.html"; @@ -53,6 +81,10 @@ self.addEventListener("fetch", (event) => { let url = (requestURL.hostname === "cdn.emulatorjs.org") ? event.request.url : requestURL.pathname; const cache = await caches.open(CACHE_NAME); if (requestURL.hostname === "cdn.emulatorjs.org" && !OFFLINE_FILES.includes(event.request.url)) { + const cachedResponse = await caches.match(event.request); + if (cachedResponse) { + return cachedResponse; + } return await fetch(event.request); } try {