bug1151916_driver.html (1663B)
1 <html> 2 <body> 3 <script language="javascript"> 4 function fail(msg) { 5 window.parent.postMessage({ status: "failed", message: msg }, "*"); 6 } 7 8 function success(msg) { 9 window.parent.postMessage({ status: "success", message: msg }, "*"); 10 } 11 12 if (!window.parent) { 13 dump("This file must be embedded in an iframe!"); 14 } 15 16 navigator.serviceWorker.getRegistration() 17 .then(function(reg) { 18 if (!reg) { 19 navigator.serviceWorker.ready.then(function(registration) { 20 if (registration.active.state == "activating") { 21 registration.active.onstatechange = function(e) { 22 registration.active.onstatechange = null; 23 if (registration.active.state == "activated") { 24 success("Registered and activated"); 25 } 26 } 27 } else { 28 success("Registered and activated"); 29 } 30 }); 31 navigator.serviceWorker.register("bug1151916_worker.js", 32 { scope: "." }); 33 } else { 34 // Simply force the sw to load a resource and touch self.caches. 35 if (!reg.active) { 36 fail("no-active-worker"); 37 return; 38 } 39 40 fetch("madeup.txt").then(function(res) { 41 res.text().then(function(v) { 42 if (v == "Hi there") { 43 success("Loaded from cache"); 44 } else { 45 fail("Response text did not match"); 46 } 47 }, fail); 48 }, fail); 49 } 50 }, fail); 51 </script> 52 </body> 53 </html>