tor-browser

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

unload.js (1340B)


      1 const syncDelay = ms => {
      2  const start = performance.now();
      3  let elapsedTime;
      4  do {
      5    elapsedTime = performance.now() - start;
      6  } while (elapsedTime < ms);
      7 };
      8 
      9 const markTime = (docName, lifecycleEventName) => {
     10  // Calculating these values before the below `mark` invocation ensures that delays in
     11  // reaching across to the other window object doesn't interfere with the correctness
     12  // of the test.
     13  const dateNow = Date.now();
     14  const performanceNow = performance.now();
     15 
     16  window.opener.mark({
     17    docName,
     18    lifecycleEventName,
     19    performanceNow: performanceNow,
     20    dateNow: dateNow
     21  });
     22 };
     23 
     24 const setupUnloadPrompt = (docName, msg) => {
     25  window.addEventListener("beforeunload", ev => {
     26    markTime(docName, "beforeunload");
     27    return ev.returnValue = msg || "Click OK to continue test."
     28  });
     29 };
     30 
     31 const setupListeners = (docName, nextDocument) => {
     32  window.addEventListener("load", () => {
     33    markTime(docName, "load");
     34    document.getElementById("proceed").addEventListener("click", ev => {
     35      ev.preventDefault();
     36      if (nextDocument) {
     37        document.location = nextDocument;
     38      } else {
     39        window.close();
     40      }
     41    })
     42  });
     43 
     44  setupUnloadPrompt(docName);
     45 
     46  window.addEventListener("unload", () => {
     47    markTime(docName, "unload");
     48    if (docName !== "c") { syncDelay(1000); }
     49  });
     50 };