PageSourceSearch

https://amarkets.life/wp-content/plugins/amarkets-com/js/auth_link_tracker/auth_link_tracker.js?ver=403.253

js amarkets.life collected 2026-10-08 00:53:33 UTC 7,594 bytes, 244 lines download raw bytes

1(function () {
2    const LINK_SELECTOR = '.auth-link, .auth-link > a';
3
4    const url_params = get_params_from_url();
5    const url_params_applied = new WeakSet();
6    const url_param_keys = Object.keys(url_params);
7
8    let url_observer = null;
9    let cookie_observer = null;
10
11    init_url_observe();
12    init_cookie_observe();
13
14    document.addEventListener('DOMContentLoaded', function () {
15        stop_url_observer();
16        setTimeout(() => stop_cookie_observer(), 5000);
17    });
18
19    function get_params_from_url() {
20        const utm_names = [
21            'utm_source',
22            'utm_campaign',
23            'utm_medium',
24            'utm_content'
25        ];
26        const params = new URLSearchParams(window.location.search);
27        const result = {};
28
29        utm_names.forEach(field => {
30            const val = params.get(field);
31            if (val) result[field] = val;
32        });
33
34        const promo_code = get_promo_code_url();
35        if (promo_code) {
36            result.promo_code = promo_code;
37        }
38
39        return result;
40    }
41
42    function get_promo_code_url() {
43        const query_params = new URLSearchParams(window.location.search);
44        const from_query = query_params.get('g');
45        if (from_query) return from_query;
46
47        const hash = window.location.hash.substring(1);
48        const hash_params = new URLSearchParams(hash);
49        const from_hash = hash_params.get('g');
50        if (from_hash) return from_hash;
51
52        return null;
53    }
54
55    function init_url_observe() {
56        if (Object.keys(url_params).length > 0) {
57            const links = document.querySelectorAll(LINK_SELECTOR);
58
59            if (links.length > 0) {
60                links.forEach((link) => apply_url_params_to_link(link));
61            }
62
63            url_observer = init_node_elements_observer();
64        }
65    }
66
67    function init_cookie_observe() {
68        cookie_observer = setInterval(apply_cookie_params, 500);
69    }
70
71    function init_node_elements_observer() {
72        const observer = new MutationObserver(function (mutations) {
73            for (const mutation of mutations) {
74                for (const node of mutation.addedNodes) {
75                    if (node.nodeType !== Node.ELEMENT_NODE) continue;
76
77                    // Проверяем добавленный элемент
78                    if (node.matches && node.matches(LINK_SELECTOR)) {
79                        apply_url_params_to_link(node);
80                    }
81                }
82            }
83        });
84
85        observer.observe(document.documentElement, {
86            childList: true,
87            subtree: true
88        });
89
90        return observer;
91    }
92
93    function apply_url_params_to_link(link) {
94        if (url_params_applied.has(link)) return;
95        url_params_applied.add(link);
96        update_link_href(link, url_params);
97    }
98
99    function apply_cookie_params() {
100        const cookie_params = get_params_from_cookies();
101        if (Object.keys(cookie_params).length === 0) return;
102
103        const cookie_params_keys = Object.keys(cookie_params);
104        const all_expected_present = cookie_params_keys.every(key =>
105            cookie_params.hasOwnProperty(key) && cookie_params[key]
106        );
107
108        if (all_expected_present) stop_cookie_observer();
109
110        const links = document.querySelectorAll(LINK_SELECTOR);
111        if (links.length === 0) return;
112
113        links.forEach((link) => {
114            // Если на ссылку уже навесили URL-параметры — исключаем эти ключи из кук, чтобы не перезаписать
115            if (url_params_applied.has(link) && url_param_keys.length > 0) {
116                const filtered_cookie_params = {};
117
118                for (const [key, value] of Object.entries(cookie_params)) {
119                    if (!url_param_keys.includes(key)) {
120                        filtered_cookie_params[key] = value;
121                    }
122                }
123
124                if (Object.keys(filtered_cookie_params).length > 0) {
125                    update_link_href(link, filtered_cookie_params);
126                }
127            } else {
128                update_link_href(link, cookie_params);
129            }
130        });
131    }
132
133    function update_link_href(link, params) {
134        const href = link.getAttribute('href');
135        if (!href || href === '#' || /^javascript:/i.test(href)) return;
136
137        try {
138            const url = new URL(href, window.location.origin);
139            let changed = false;
140
141            for (const [key, value] of Object.entries(params)) {
142                if (value && (!url.searchParams.has(key) || url.searchParams.get(key) !== value)) {
143                    url.searchParams.set(key, value);
144                    changed = true;
145                }
146            }
147
148            if (changed) link.setAttribute('href', url.toString());
149        } catch (e) {
150            console.error('auth_link_tracker: Invalid auth URL');
151        }
152    }
153
154    function get_params_from_cookies() {
155        const promo_code = get_cookie('promo_code');
156        const utmzz = get_cookie('__utmzz');
157        const utmz = get_cookie('__utmz');
158
159        let utm_params = {};
160        if (utmzz) {
161            utm_params = parse_utm_cookie(utmzz);
162        } else if (utmz) {
163            utm_params = parse_utm_cookie(utmz);
164        }
165
166        const analytics = {
167            _ym_uid: get_cookie('_ym_uid'),
168            _bge_bbg: get_cookie('_bge_bbg'),
169            _bge_ci: get_cookie('_bge_ci'),
170            _ga: get_cookie('_ga'),
171            _fbp: get_cookie('_fbp'),
172            _fbc: get_cookie('_fbc'),
173            msclkid: get_cookie('msclkid')
174        };
175
176        return {
177            ...(promo_code ? { promo_code } : {}),
178            ...utm_params,
179            ...analytics
180        };
181    }
182
183    function parse_utm_cookie(cookie_value) {
184        if (!cookie_value) return {};
185
186        // Отрезаем технический префикс 51880761.1778676189.9.5.
187        const utm_start = cookie_value.indexOf('utmcsr=');
188        if (utm_start !== -1) {
189            cookie_value = cookie_value.substring(utm_start);
190        }
191
192        const utm_map = {
193            utmcsr: 'utm_source',
194            utmccn: 'utm_campaign',
195            utmcmd: 'utm_medium',
196            utmcct: 'utm_content'
197        };
198        const result = {};
199
200        cookie_value.split('|').forEach(part => {
201            const eq_index = part.indexOf('=');
202            if (eq_index === -1) return;
203
204            const key = part.slice(0, eq_index);
205            const raw_value = part.slice(eq_index + 1);
206
207            if (utm_map[key] && raw_value) {
208                try {
209                    result[utm_map[key]] = decodeURIComponent(raw_value);
210                } catch (e) {
211                    result[utm_map[key]] = raw_value;
212                }
213            }
214        });
215
216        return result;
217    }
218
219    function get_cookie(name) {
220        const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
221        if (!match) return null;
222
223        try {
224            return decodeURIComponent(match[2]);
225        } catch (e) {
226            console.error('Ошибка декодирования куки:', e);
227            return match[2];
228        }
229    }
230
231    function stop_url_observer() {
232        if (url_observer) {
233            url_observer.disconnect();
234            url_observer = null;
235        }
236    }
237
238    function stop_cookie_observer() {
239        if (cookie_observer) {
240            clearInterval(cookie_observer);
241            cookie_observer = null;
242        }
243    }
244})();

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.