tor-browser

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

prerender-state.html (2997B)


      1 <!DOCTYPE html>
      2 <script src="/common/utils.js"></script>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="utils.js"></script>
      6 <script>
      7 
      8 const params = new URLSearchParams(location.search);
      9 
     10 // Take a key used for storing a test result in the server.
     11 const key = params.get('key');
     12 
     13 // The main test page (state-and-event.html in the parent directory) will load
     14 // this page only with the "key" parameter. This page will then prerender
     15 // itself with the "run-test" parameter. When "run-test" is in the URL we'll
     16 // actually start the test process and record the results to send back to the
     17 // main test page. We do this because the main test page cannot navigate itself
     18 // but it also cannot open a popup to a prerendered browsing context so the
     19 // prerender triggering and activation must both happen in this popup.
     20 const run_test = params.has('run-test');
     21 if (!run_test) {
     22  assert_false(document.prerendering);
     23 
     24  // Generate a new stash key so we can communicate with the prerendered page
     25  // about when to activate it.
     26  const activate_key = token();
     27  const url = new URL(document.URL);
     28  url.searchParams.append('run-test', '');
     29  url.searchParams.append('activate-key', activate_key);
     30  startPrerendering(url.toString());
     31 
     32  // Wait until the prerendered page signals us it's time to activate, then
     33  // navigate to it.
     34  nextValueFromServer(activate_key).then(() => {
     35    window.location = url.toString();
     36  });
     37 } else {
     38  assert_true(document.prerendering);
     39 
     40  const activate_key = params.get('activate-key');
     41  const result = {
     42    // Check the types of the members on document.
     43    prerenderingTypeOf: typeof(document.prerendering),
     44    onprerenderingChangeTypeOf: typeof(document.onprerenderingchange),
     45 
     46    // Check the value of document.prerendering now and after activation.
     47    prerenderingValueBeforeActivate: document.prerendering,
     48    prerenderingValueAfterActivate: null,
     49 
     50    // Track when the prerenderingchange event is fired.
     51    onprerenderingchangeCalledBeforeActivate: false,
     52    onprerenderingchangeCalledAfterActivate: false,
     53 
     54    // Tracks the properties on the prerenderingchange event.
     55    eventBubbles: null,
     56    eventCancelable: null
     57  };
     58 
     59  let did_load = false;
     60 
     61  addEventListener('load', () => {
     62    did_load = true;
     63 
     64    // Tell the harness we've finished loading so we can proceed to activation.
     65    writeValueToServer(activate_key, 'did_load');
     66  });
     67 
     68  document.addEventListener('prerenderingchange', (e) => {
     69    assert_false(document.prerendering);
     70    result.eventBubbles = e.bubbles;
     71    result.eventCancelable = e.cancelable;
     72 
     73    if (did_load) {
     74      result.onprerenderingchangeCalledAfterActivate = true;
     75      result.prerenderingValueAfterActivate = document.prerendering;
     76      writeValueToServer(key, JSON.stringify(result)).then(() => {
     77        window.close();
     78      });
     79    } else {
     80      result.onprerenderingchangeCalledBeforeActivate = true;
     81    }
     82  });
     83 }
     84 
     85 </script>