tor-browser

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

focus-without-user-activation-iframe-tentative.html (1571B)


      1 <!doctype html>
      2 <input autofocus onfocus="autofocus_onfocus()"/>
      3 <script>
      4  let autofocused = false;
      5  function autofocus_onfocus() {
      6    autofocused = true;
      7  }
      8 
      9  /**
     10   * @param target object: Target to call |focus()| with.
     11   * @param timeout integer | undefined: Timeout to wait for the focus event.
     12   * If unspecified, a timeout will not be set.
     13   * @param focus_target boolean | undefined: Wether to focus the target after
     14   * listening for |onfocus| event.
     15   */
     16  function wait_focus_event(target, timeout, focus_target) {
     17    return new Promise((resolve) => {
     18      if (timeout)
     19        setTimeout(() => resolve(false), timeout);
     20 
     21      target.onfocus = () => resolve(true);
     22      if (focus_target)
     23        target.focus();
     24    });
     25  }
     26 
     27  function post_result(destination, result) {
     28    destination.postMessage({focused: result}, "*");
     29  }
     30 
     31  window.addEventListener("message", (e) => {
     32    if (e.data.event === "autofocus") {
     33      if (autofocused)
     34        post_result(e.source, true);
     35 
     36      wait_focus_event(document.querySelector("input"), e.data.timeout)
     37        .then(result => post_result(e.source, result));
     38    } else if (e.data.event === "focus-window") {
     39      wait_focus_event(window, e.data.timeout, true /* focus_target */)
     40        .then(result => post_result(e.source, result));
     41    } else if (e.data.event === "focus-input") {
     42      const input_element = document.querySelector("input");
     43      wait_focus_event(input_element, e.data.timeout, true /* focus_target */)
     44        .then(result => post_result(e.source, result));
     45    }
     46 });
     47 </script>