File: bullet_xhr.js

package info (click to toggle)
ruby-bullet 7.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 836 kB
  • sloc: ruby: 6,133; javascript: 57; sh: 27; makefile: 4
file content (64 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function () {
  var oldOpen = window.XMLHttpRequest.prototype.open;
  var oldSend = window.XMLHttpRequest.prototype.send;

  /**
   * Return early if we've already extended prototype. This prevents
   * "maximum call stack exceeded" errors when used with Turbolinks.
   * See https://github.com/flyerhzm/bullet/issues/454
   */
  if (isBulletInitiated()) return;

  function isBulletInitiated() {
    return oldOpen.name == "bulletXHROpen" && oldSend.name == "bulletXHRSend";
  }
  function bulletXHROpen(_, url) {
    this._storedUrl = url;
    return Reflect.apply(oldOpen, this, arguments);
  }
  function bulletXHRSend() {
    if (this.onload) {
      this._storedOnload = this.onload;
    }
    this.onload = null;
    this.addEventListener("load", bulletXHROnload);
    return Reflect.apply(oldSend, this, arguments);
  }
  function bulletXHROnload() {
    if (
      this._storedUrl.startsWith(window.location.protocol + "//" + window.location.host) ||
      !this._storedUrl.startsWith("http") // For relative paths
    ) {
      var bulletFooterText = this.getResponseHeader("X-bullet-footer-text");
      if (bulletFooterText) {
        setTimeout(function () {
          var oldHtml = document.querySelector("#bullet-footer").innerHTML.split("<br>");
          var header = oldHtml[0];
          oldHtml = oldHtml.slice(1, oldHtml.length);
          var newHtml = oldHtml.concat(JSON.parse(bulletFooterText));
          newHtml = newHtml.slice(newHtml.length - 10, newHtml.length); // rotate through 10 most recent
          document.querySelector("#bullet-footer").innerHTML = `${header}<br>${newHtml.join("<br>")}`;
        }, 0);
      }
      var bulletConsoleText = this.getResponseHeader("X-bullet-console-text");
      if (bulletConsoleText && typeof console !== "undefined" && console.log) {
        setTimeout(function () {
          JSON.parse(bulletConsoleText).forEach((message) => {
            if (console.groupCollapsed && console.groupEnd) {
              console.groupCollapsed("Uniform Notifier");
              console.log(message);
              console.groupEnd();
            } else {
              console.log(message);
            }
          });
        }, 0);
      }
    }
    if (this._storedOnload) {
      return Reflect.apply(this._storedOnload, this, arguments);
    }
  }
  window.XMLHttpRequest.prototype.open = bulletXHROpen;
  window.XMLHttpRequest.prototype.send = bulletXHRSend;
})();