sw-controlled-iframe.html (1167B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>iframe used in clientId test</title> 4 <script> 5 6 self.onmessage = async event => { 7 try { 8 if (event.data === 'get_sw_client_id') { 9 // Use the controlling service worker to determine 10 // this client's id according to the Service Worker. 11 const response = await fetch('/clientId'); 12 const data = await response.json(); 13 window.parent.postMessage(data.clientId, '*'); 14 return; 15 } 16 17 if (event.data === 'get_lock_client_id') { 18 // Grab a lock, then query the lock manager for state to 19 // determine this client's id according to the lock manager. 20 await navigator.locks.request('lock-name', async lock => { 21 const lock_state = await navigator.locks.query(); 22 const held_lock = lock_state.held.filter(l => l.name === lock.name)[0]; 23 window.parent.postMessage(held_lock.clientId, '*'); 24 }); 25 return; 26 } 27 28 window.parent.postMessage(`unknown request: ${event.data}`, '*'); 29 } catch (ex) { 30 // In case of test failure, don't leave parent window hanging. 31 window.parent.postMessage(`${ex.name}: ${ex.message}`, '*'); 32 } 33 }; 34 35 </script>