PageSourceSearch

https://brandmate.ai/js/env-rewrite.js

js brandmate.ai collected 2026-10-05 11:52:33 UTC 2,194 bytes, 61 lines download raw bytes

1/*
2 * env-rewrite.js — staging URL rewriter.
3 * On staging.* hosts only, rewrites hardcoded production URLs to their staging
4 * equivalents so links, forms, and fetch() calls stay inside the staging env.
5 * No-op on production. Loaded synchronously in <head> before any other script.
6 */
7(function () {
8  if (location.hostname.indexOf("staging") !== 0) return; // prod = no-op
9
10  function map(u) {
11    if (typeof u !== "string" || u.indexOf("brandmate.ai") === -1) return u;
12    return u
13      .replace(/\/\/app\.brandmate\.ai/g, "//staging-app.brandmate.ai")
14      .replace(/\/\/www\.brandmate\.ai/g, "//staging.brandmate.ai")
15      .replace(/\/\/brandmate\.ai/g, "//staging.brandmate.ai");
16  }
17
18  // 1) Patch fetch (covers free-audit + pricing API calls hardcoded to app.brandmate.ai)
19  var _fetch = window.fetch;
20  if (_fetch) {
21    window.fetch = function (input, init) {
22      try {
23        if (typeof input === "string") input = map(input);
24        else if (input && input.url) input = new Request(map(input.url), input);
25      } catch (e) {}
26      return _fetch.call(this, input, init);
27    };
28  }
29
30  // 2) Rewrite <a href> and <form action> — initial + dynamically injected
31  function fixEl(el) {
32    if (!el || !el.getAttribute) return;
33    var href = el.getAttribute("href");
34    if (href && href.indexOf("brandmate.ai") > -1) el.setAttribute("href", map(href));
35    var action = el.getAttribute("action");
36    if (action && action.indexOf("brandmate.ai") > -1) el.setAttribute("action", map(action));
37  }
38  function scan(root) {
39    if (root && root.querySelectorAll) {
40      root.querySelectorAll("a[href], form[action]").forEach(fixEl);
41    }
42  }
43  function run() {
44    scan(document);
45    try {
46      new MutationObserver(function (muts) {
47        muts.forEach(function (m) {
48          if (!m.addedNodes) return;
49          m.addedNodes.forEach(function (n) {
50            if (n.nodeType === 1) { fixEl(n); scan(n); }
51          });
52        });
53      }).observe(document.documentElement, { childList: true, subtree: true });
54    } catch (e) {}
55  }
56  if (document.readyState === "loading") {
57    document.addEventListener("DOMContentLoaded", run);
58  } else {
59    run();
60  }
61})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.