1const { 2 currentScript 3} = document; 4if ('serviceWorker' in navigator && currentScript !== null && 'src' in currentScript) { 5 const currentScriptUrl = new URL(currentScript.src); 6 const currentLocation = Object.fromEntries(currentScriptUrl.searchParams.entries()).current_location; 7 if (currentLocation) { 8 const serviceWorkerUrl = new URL('service_worker.js', currentLocation.endsWith('/') ? currentLocation : currentLocation + '/'); 9 10 // Extendable via add-ons 11 for (const [searchKey, searchValue] of currentScriptUrl.searchParams) { 12 serviceWorkerUrl.searchParams.set(searchKey, searchValue); 13 } 14 const serviceWorkerUrlHref = serviceWorkerUrl.href; 15 const isRegistered = async () => (await navigator.serviceWorker.getRegistrations()).some(reg => reg.active && new URL(reg.active.scriptURL).pathname === serviceWorkerUrl.pathname); 16 const onReady = () => { 17 if (navigator.serviceWorker.controller !== null) { 18 navigator.serviceWorker.controller.postMessage('page_loaded'); 19 } 20 }; 21 const checkAndRegisterServiceWorker = async () => { 22 if (await isRegistered()) { 23 if (document.readyState === 'loading') { 24 document.addEventListener('DOMContentLoaded', onReady); 25 } else { 26 onReady(); 27 } 28 return; 29 } 30 31 // service_worker.js MUST be located in the root. Absolute path required 32 navigator.serviceWorker.register(serviceWorkerUrlHref); 33 }; 34 checkAndRegisterServiceWorker(); 35 } 36}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.