github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/static/js/main.js (about)

     1  function syntaxHighlight(json) {
     2    json = JSON.stringify(json, undefined, 2);
     3    json = json
     4      .replace(/&/g, "&")
     5      .replace(/</g, "&lt;")
     6      .replace(/>/g, "&gt;");
     7    if (json.length > 1000000) {
     8      return `<span class="key">${json}</span>`;
     9    }
    10    return json.replace(
    11      /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
    12      (match) => {
    13        let cls = "number";
    14        if (/^"/.test(match)) {
    15          if (/:$/.test(match)) {
    16            cls = "key";
    17          } else {
    18            cls = "string";
    19          }
    20        } else if (/true|false/.test(match)) {
    21          cls = "boolean";
    22        } else if (/null/.test(match)) {
    23          cls = "null";
    24        }
    25        return `<span class="${cls}">${match}</span>`;
    26      }
    27    );
    28  }
    29  
    30  function getCoinCookie() {
    31    if(hasSecondary) return document.cookie
    32    .split("; ")
    33    .find((row) => row.startsWith("secondary_coin="))
    34    ?.split("=");
    35  }
    36  
    37  function changeCSSStyle(selector, cssProp, cssVal) {
    38    const mIndex = 1;
    39    const cssRules = document.all ? "rules" : "cssRules";
    40    for (
    41      i = 0, len = document.styleSheets[mIndex][cssRules].length;
    42      i < len;
    43      i++
    44    ) {
    45      if (document.styleSheets[mIndex][cssRules][i].selectorText === selector) {
    46        document.styleSheets[mIndex][cssRules][i].style[cssProp] = cssVal;
    47        return;
    48      }
    49    }
    50  }
    51  
    52  function amountTooltip() {
    53    const prim = this.querySelector(".prim-amt");
    54    const sec = this.querySelector(".sec-amt");
    55    const csec = this.querySelector(".csec-amt");
    56    const base = this.querySelector(".base-amt");
    57    const cbase = this.querySelector(".cbase-amt");
    58    let s = `${prim.outerHTML}<br>`;
    59    if (base) {
    60      let t = base.getAttribute("tm");
    61      if (!t) {
    62        t = "now";
    63      }
    64      s += `<span class="amt-time">${t}</span>${base.outerHTML}<br>`;
    65    }
    66    if (cbase) {
    67      s += `<span class="amt-time">now</span>${cbase.outerHTML}<br>`;
    68    }
    69    if (sec) {
    70      let t = sec.getAttribute("tm");
    71      if (!t) {
    72        t = "now";
    73      }
    74      s += `<span class="amt-time">${t}</span>${sec.outerHTML}<br>`;
    75    }
    76    if (csec) {
    77      s += `<span class="amt-time">now</span>${csec.outerHTML}<br>`;
    78    }
    79    return `<span class="l-tooltip">${s}</span>`;
    80  }
    81  
    82  function addressAliasTooltip() {
    83    const type = this.getAttribute("alias-type");
    84    const address = this.getAttribute("cc");
    85    return `<span class="l-tooltip">${type}<br>${address}</span>`;
    86  }
    87  
    88  window.addEventListener("DOMContentLoaded", () => {
    89    const a = getCoinCookie();
    90    if (a?.length === 3) {
    91      if (a[2] === "true") {
    92        changeCSSStyle(".prim-amt", "display", "none");
    93        changeCSSStyle(".sec-amt", "display", "initial");
    94      }
    95      document
    96        .querySelectorAll(".amt")
    97        .forEach(
    98          (e) => new bootstrap.Tooltip(e, { title: amountTooltip, html: true })
    99        );
   100    }
   101  
   102    document
   103      .querySelectorAll("[alias-type]")
   104      .forEach(
   105        (e) =>
   106          new bootstrap.Tooltip(e, { title: addressAliasTooltip, html: true })
   107      );
   108  
   109    document
   110      .querySelectorAll("[tt]")
   111      .forEach((e) => new bootstrap.Tooltip(e, { title: e.getAttribute("tt") }));
   112  
   113    document.querySelectorAll("#header .bb-group>.btn-check").forEach((e) =>
   114      e.addEventListener("click", (e) => {
   115        const a = getCoinCookie();
   116        const sc = e.target.id === "secondary-coin";
   117        if (a?.length === 3 && (a[2] === "true") !== sc) {
   118          document.cookie = `${a[0]}=${a[1]}=${sc}; Path=/`;
   119          changeCSSStyle(".prim-amt", "display", sc ? "none" : "initial");
   120          changeCSSStyle(".sec-amt", "display", sc ? "initial" : "none");
   121        }
   122      })
   123    );
   124  
   125    document.querySelectorAll(".copyable").forEach((e) =>
   126      e.addEventListener("click", (e) => {
   127        if (e.clientX < e.target.getBoundingClientRect().x) {
   128          let t = e.target.getAttribute("cc");
   129          if (!t) t = e.target.innerText;
   130          navigator.clipboard.writeText(t);
   131          e.target.className = e.target.className.replace("copyable", "copied");
   132          setTimeout(
   133            () =>
   134              (e.target.className = e.target.className.replace(
   135                "copied",
   136                "copyable"
   137              )),
   138            1000
   139          );
   140          e.preventDefault();
   141        }
   142      })
   143    );
   144  });