summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/pageloader/chrome/lh_hero.js
blob: 55f28e2333837b4af250c53690e330592cfa4b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env mozilla/frame-script */

function _contentHeroHandler(isload) {
  var obs = null;
  var el = content.window.document.querySelector("[elementtiming]");
  if (el) {
    function callback(entries) {
      entries.forEach(() => {
        sendAsyncMessage("PageLoader:LoadEvent", {
          time: content.window.performance.now(),
          name: "tphero",
        });
        obs.disconnect();
      });
    }
    // we want the element 100% visible on the viewport
    var options = { root: null, rootMargin: "0px", threshold: [1] };
    try {
      obs = new content.window.IntersectionObserver(callback, options);
      obs.observe(el);
    } catch (err) {
      sendAsyncMessage("PageLoader:Error", { msg: err.message });
    }
  } else if (isload) {
    // If the hero element is added from a settimeout handler, it might not run before 'load'
    content.setTimeout(function () {
      _contentHeroHandler(false);
    }, 5000);
  } else {
    var err = "Could not find a tag with an elmenttiming attr on the page";
    sendAsyncMessage("PageLoader:Error", { msg: err });
  }
  return obs;
}

function _contentHeroLoadHandler() {
  _contentHeroHandler(true);
}

addEventListener(
  "load",
  // eslint-disable-next-line no-undef
  contentLoadHandlerCallback(_contentHeroLoadHandler),
  true
);