PageSourceSearch

https://www.airthings.com/static/version1790585893/frontend/Airthi…/Aheadworks_Affiliate/js/promo_link.js

js airthings.com collected 2026-10-02 03:34:46 UTC 3,298 bytes, 118 lines download raw bytes

1function getUrlParamVal(param) {
2    var vars = {};
3    window.location.href.replace(location.hash, '').replace(
4      /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
5      function (m, key, value) { // callback
6        vars[key] = value !== undefined ? value : '';
7      }
8    );
9
10    if (param) {
11      return vars[param] ? vars[param] : null;
12    }
13    return vars;
14  }
15
16  function getCookieByName(name) {
17    var value = '; ' + document.cookie,
18        parts = value.split('; ' + name + '=');
19
20    if (parts.length === 2) {
21      return parts.pop().split(';').shift();
22    }
23
24    return '';
25  }
26
27  /**
28   * Resolve the store base URL robustly, without depending on a single global
29   * that may not be defined yet (or at all) depending on theme / load order.
30   *
31   * Order of preference:
32   *   1. Hyva / Magento inline global `window.BASE_URL`
33   *   2. RequireJS baseUrl (Luma), normalized back to the store root
34   *   3. Current origin as a last resort
35   */
36  function resolveBaseUrl() {
37    if (window.BASE_URL) {
38      return window.BASE_URL;
39    }
40
41    try {
42      if (window.require && require.s && require.s.contexts && require.s.contexts._) {
43        var cfg = require.s.contexts._.config;
44
45        if (cfg && cfg.baseUrl) {
46          // require baseUrl usually points at ".../pub/static/<area>/..." – strip
47          // everything from the static path onwards to get back to the store root.
48          return cfg.baseUrl.replace(/\/(pub\/)?static\/.*$/, '/');
49        }
50      }
51    } catch (e) {
52      // ignore and fall through to origin fallback
53    }
54
55    return window.location.origin + '/';
56  }
57
58  /**
59   * Resolve the form key robustly. Prefer Hyva's canonical helper when present,
60   * otherwise fall back to reading the cookie directly.
61   */
62  function resolveFormKey() {
63    if (window.hyva && typeof window.hyva.getFormKey === 'function') {
64      var key = window.hyva.getFormKey();
65
66      if (key) {
67        return key;
68      }
69    }
70
71    return getCookieByName('form_key');
72  }
73
74  function sendCheckLinkRequest(affiliateVal) {
75    var baseUrl = resolveBaseUrl();
76
77    if (!baseUrl) {
78      return;
79    }
80
81    var path = 'aw_affiliate/promo/checkLink',
82        requestUrl = new URL(baseUrl.replace(/\/$/, '') + '/' + path.replace(/^\//, ''), window.location.href),
83        formKey = resolveFormKey(),
84        body = new URLSearchParams();
85
86    body.append('aw_affiliate', affiliateVal);
87    body.append('form_key', formKey);
88
89    fetch(requestUrl.toString(), {
90      method: 'POST',
91      headers: {
92        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
93        'X-Requested-With': 'XMLHttpRequest',
94        'form_key': formKey
95      },
96      body: body.toString(),
97      cache: 'no-cache'
98    });
99  }
100
101  function onDocumentReady() {
102    var affiliateVal = getUrlParamVal('aw_affiliate');
103
104    if (!affiliateVal) {
105      return;
106    }
107
108    sendCheckLinkRequest(affiliateVal);
109  }
110
111  // The script is injected in the <head>, so defer execution until the DOM is
112  // ready. This guarantees the theme globals (window.BASE_URL, form_key cookie,
113  // hyva helpers) have been emitted before we try to use them.
114  if (document.readyState === 'loading') {
115    document.addEventListener('DOMContentLoaded', onDocumentReady);
116  } else {
117    onDocumentReady();
118  }

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.