tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

prompt-by-before-unload.html (1545B)


      1 <!DOCTYPE html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="/speculation-rules/prerender/resources/utils.js"></script>
      5 <div id="target"></div>
      6 <iframe id="i" srcdoc="<html><body>Hello</body></html>"></iframe>
      7 <script>
      8 
      9 assert_true(document.prerendering);
     10 
     11 const params = new URLSearchParams(location.search);
     12 const uid = params.get('uid');
     13 
     14 i.contentWindow.onbeforeunload = function(e) {
     15  // Call preventDefault() or set `returnValue` to trigger the prompt
     16  // on beforeunload event.
     17  // The prompt actually doesn't show up in a prerendered page and
     18  // unload proceeds.
     19  e.preventDefault();
     20  e.returnValue = 'You have a return value.';
     21 }
     22 
     23 async function navigateWindowLocation() {
     24  const bc = new PrerenderChannel('inner-channel', uid);
     25  const promise = new Promise(resolve => {
     26    bc.addEventListener('message', e => {
     27      resolve(e.data);
     28      bc.close();
     29    }, {
     30      once: true
     31    });
     32  });
     33  i.contentWindow.location.href = `prompt-by-before-unload-inner-frame.html?uid=${uid}`;
     34  return promise;
     35 }
     36 
     37 async function asyncPromptOnBeforeUnload() {
     38  const bc = new PrerenderChannel('prerender-channel', uid);
     39  try {
     40    const result = await navigateWindowLocation();
     41    if (result == 'a new page is loaded')
     42      bc.postMessage('unloaded without the prompt by beforeunload.');
     43    else
     44      bc.postMessage('unexpected result.');
     45  } catch (err) {
     46    bc.postMessage(err);
     47  } finally {
     48    bc.close();
     49  }
     50 }
     51 
     52 asyncPromptOnBeforeUnload();
     53 </script>