about summary refs log tree commit diff stats
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/pico-cam/index.html14
-rw-r--r--js/pico-cam/service-worker.js45
2 files changed, 59 insertions, 0 deletions
diff --git a/js/pico-cam/index.html b/js/pico-cam/index.html
index b524b39..7b8798a 100644
--- a/js/pico-cam/index.html
+++ b/js/pico-cam/index.html
@@ -78,5 +78,19 @@
         <canvas id="ditheredOutput"></canvas>
     </div>
     <script src="video.js"></script>
+    <script>
+        // Check if Service Workers are supported
+        if ('serviceWorker' in navigator) {
+            window.addEventListener('load', function() {
+                navigator.serviceWorker.register('service-worker.js').then(function(registration) {
+                    // Registration was successful
+                    console.log('ServiceWorker registration successful with scope: ', registration.scope);
+                }, function(err) {
+                    // Registration failed
+                    console.log('ServiceWorker registration failed: ', err);
+                });
+            });
+        }
+    </script>
 </body>
 </html>
\ No newline at end of file
diff --git a/js/pico-cam/service-worker.js b/js/pico-cam/service-worker.js
new file mode 100644
index 0000000..75027ed
--- /dev/null
+++ b/js/pico-cam/service-worker.js
@@ -0,0 +1,45 @@
+var CACHE_NAME = 'pico-cam-v1';
+var urlsToCache = [
+  '/',
+  '/index.html',
+  '/video.js'
+];
+
+self.addEventListener('install', function(event) {
+    event.waitUntil(
+        caches.open(CACHE_NAME)
+            .then(function(cache) {
+                console.log('Opened cache');
+                return cache.addAll(urlsToCache);
+            })
+    );
+});
+
+self.addEventListener('fetch', function(event) {
+    event.respondWith(
+        caches.match(event.request)
+            .then(function(response) {
+                // Cache hit - return response
+                if (response) {
+                    return response;
+                }
+                return fetch(event.request);
+            }
+        )
+    );
+});
+
+self.addEventListener('activate', function(event) {
+    var cacheWhitelist = ['pico-cam-v1'];
+    event.waitUntil(
+        caches.keys().then(function(cacheNames) {
+            return Promise.all(
+                cacheNames.map(function(cacheName) {
+                    if (cacheWhitelist.indexOf(cacheName) === -1) {
+                        return caches.delete(cacheName);
+                    }
+                })
+            );
+        })
+    );
+});
\ No newline at end of file