tor-browser

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

bug1939248-rosasthai.com-load-event-helper.js (1451B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /**
      8 * Bug 1939248 - Load event listener issue on rosasthai.com
      9 * WebCompat issue #145642 - https://webcompat.com/issues/145642
     10 *
     11 * The site assumes that its code will run before the window load event, and
     12 * so their buttons will not function properly if that is untrue. We can call
     13 * those event listeners immediately as they try to add them, if the document
     14 * readyState is already "complete", which fixes this bug.
     15 */
     16 
     17 /* globals exportFunction */
     18 
     19 console.info(
     20  "running late window load listeners for compatibility reasons. See https://bugzilla.mozilla.org/show_bug.cgi?id=1939248 for details."
     21 );
     22 
     23 (function runLateWindowLoadEventListenersImmediately() {
     24  const { prototype } = window.wrappedJSObject.EventTarget;
     25  const { addEventListener } = prototype;
     26  prototype.addEventListener = exportFunction(function (type, b, c, d) {
     27    if (
     28      this !== window ||
     29      document?.readyState !== "complete" ||
     30      type?.toLowerCase() !== "load"
     31    ) {
     32      return addEventListener.call(this, type, b, c, d);
     33    }
     34    console.log("window.addEventListener(load) called too late, so calling", b);
     35    try {
     36      b?.call();
     37    } catch (e) {
     38      console.error(e);
     39    }
     40    return undefined;
     41  }, window);
     42 })();