tor-browser

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

helpers.mjs (1771B)


      1 export function createIframe(t) {
      2  return new Promise((resolve, reject) => {
      3    const iframe = document.createElement("iframe");
      4    iframe.onload = () => resolve(iframe);
      5    iframe.onerror = () => reject(new Error("Could not load iframe"));
      6    iframe.src = "/common/blank.html";
      7 
      8    t.add_cleanup(() => iframe.remove());
      9    document.body.append(iframe);
     10  });
     11 }
     12 
     13 export function delay(t, ms) {
     14  return new Promise(resolve => t.step_timeout(resolve, ms));
     15 }
     16 
     17 export function waitForLoad(obj) {
     18  return new Promise(resolve => {
     19    obj.addEventListener("load", resolve, { once: true });
     20  });
     21 }
     22 
     23 export function waitForHashchange(obj) {
     24  return new Promise(resolve => {
     25    obj.addEventListener("hashchange", resolve, { once: true });
     26  });
     27 }
     28 
     29 export function waitForPopstate(obj) {
     30  return new Promise(resolve => {
     31    obj.addEventListener("popstate", resolve, { once: true });
     32  });
     33 }
     34 
     35 // This is used when we want to end the test by asserting some load doesn't
     36 // happen, but we're not sure how long to wait. We could just wait a long-ish
     37 // time (e.g. a second), but that makes the tests slow. Instead, assume that
     38 // network loads take roughly the same time. Then, you can use this function to
     39 // wait a small multiple of the duration of a separate iframe load; this should
     40 // be long enough to catch any problems.
     41 export async function waitForPotentialNetworkLoads(t) {
     42  const before = performance.now();
     43 
     44  // Sometimes we're doing something, like a traversal, which cancels our first
     45  // attempt at iframe loading. In that case we bail out after 100 ms and try
     46  // again. (Better ideas welcome...)
     47  await Promise.race([createIframe(t), delay(t, 100)]);
     48  await createIframe(t);
     49 
     50  const after = performance.now();
     51  await delay(t, after - before);
     52 }