PageSourceSearch

https://msi-russia.ru/catalog/view/javascript/cookie_notice.js?v=1777753340&ajax_check=1&cache_bypass=1

js msi-russia.ru collected 2026-09-28 23:16:48 UTC 7,459 bytes, 253 lines download raw bytes

1/**
2 * Cookie Notice Standalone
3 * Работает независимо от кеширования
4 */
5(function($) {
6    'use strict';
7
8    if (typeof $ === 'undefined') {
9        return;
10    }
11
12    var CookieNotice = {
13        config: {
14            cookieName: 'cookie_notice_accepted',
15            checkUrl: 'index.php?route=extension/module/cookie_notice/checkStatus',
16            renderUrl: 'index.php?route=extension/module/cookie_notice/getNotice',
17            acceptUrl: 'index.php?route=extension/module/cookie_notice/acceptCookie',
18            lazyDelay: 3500,
19            hideDelay: 250,
20            ajaxCheck: true,
21            cacheBypass: true,
22            styleId: 'cookie-notice-dynamic-style'
23        },
24        isLoading: false,
25        hasLoaded: false,
26        eventsBound: false,
27
28        init: function() {
29            this.readScriptConfig();
30
31            if (this.getCookie(this.config.cookieName) || $('#cn-wrapper').length) {
32                return;
33            }
34
35            this.scheduleLazyLoad();
36        },
37
38        readScriptConfig: function() {
39            var script = document.querySelector('script[src*="catalog/view/javascript/cookie_notice.js"]');
40            var url;
41
42            if (!script || !script.src || typeof URL === 'undefined') {
43                return;
44            }
45
46            url = new URL(script.src, window.location.origin);
47            this.config.ajaxCheck = url.searchParams.get('ajax_check') !== '0';
48            this.config.cacheBypass = url.searchParams.get('cache_bypass') !== '0';
49        },
50
51        scheduleLazyLoad: function() {
52            var self = this;
53            var hasStarted = false;
54            var interactionEvents = ['scroll', 'click', 'keydown', 'touchstart'];
55            var timerId = null;
56            var cleanup = function() {
57                var i;
58
59                for (i = 0; i < interactionEvents.length; i++) {
60                    window.removeEventListener(interactionEvents[i], onInteraction);
61                }
62            };
63            var startLoading = function(delay) {
64                if (hasStarted) {
65                    return;
66                }
67
68                hasStarted = true;
69
70                if (timerId) {
71                    window.clearTimeout(timerId);
72                }
73
74                cleanup();
75
76                timerId = window.setTimeout(function() {
77                    self.loadNotice();
78                }, delay);
79            };
80            var onInteraction = function() {
81                startLoading(150);
82            };
83            var i;
84
85            for (i = 0; i < interactionEvents.length; i++) {
86                window.addEventListener(interactionEvents[i], onInteraction, { passive: true });
87            }
88
89            timerId = window.setTimeout(function() {
90                startLoading(0);
91            }, self.config.lazyDelay);
92        },
93
94        loadNotice: function() {
95            var self = this;
96            var requestData = {};
97
98            if (self.isLoading || self.hasLoaded || self.getCookie(self.config.cookieName)) {
99                return;
100            }
101
102            self.isLoading = true;
103
104            if (self.config.cacheBypass) {
105                requestData.cn_cache_buster = Date.now();
106            }
107
108            $.ajax({
109                url: self.config.ajaxCheck ? self.config.checkUrl : self.config.renderUrl,
110                type: 'GET',
111                dataType: 'json',
112                data: requestData,
113                cache: !self.config.cacheBypass,
114                success: function(response) {
115                    self.isLoading = false;
116                    self.hasLoaded = true;
117
118                    if (response.status === 'success' && response.show_notice && response.html) {
119                        self.showNotice(response.html, response.css);
120                    }
121                },
122                error: function() {
123                    self.isLoading = false;
124                }
125            });
126        },
127
128        showNotice: function(html, css) {
129            this.injectCss(css);
130
131            if (!$('#cn-wrapper').length) {
132                $('body').append(html);
133            }
134
135            this.bindEvents();
136
137            window.setTimeout(function() {
138                $('#cn-wrapper').addClass('cn-blok-visible');
139            }, 30);
140        },
141
142        injectCss: function(css) {
143            var cssText = '';
144            var i;
145
146            if (!css || !css.length) {
147                return;
148            }
149
150            for (i = 0; i < css.length; i++) {
151                cssText += css[i];
152            }
153
154            $('#' + this.config.styleId).remove();
155            $('head').append('<style id="' + this.config.styleId + '">' + cssText + '</style>');
156        },
157
158        bindEvents: function() {
159            var self = this;
160
161            if (this.eventsBound) {
162                return;
163            }
164
165            this.eventsBound = true;
166
167            $('body').off('click.cookieNotice', '#cookie_notice_btn').on('click.cookieNotice', '#cookie_notice_btn', function(e) {
168                e.preventDefault();
169                self.acceptCookie();
170            });
171        },
172
173        acceptCookie: function() {
174            var self = this;
175
176            this.setCookie(this.config.cookieName, '1', 365);
177
178            $.ajax({
179                url: self.config.acceptUrl,
180                type: 'POST',
181                dataType: 'json',
182                cache: !self.config.cacheBypass,
183                success: function() {
184                    self.hideNotice();
185                },
186                error: function() {
187                    self.hideNotice();
188                }
189            });
190        },
191
192        hideNotice: function() {
193            window.setTimeout(function() {
194                var $wrapper = $('#cn-wrapper');
195
196                if (!$wrapper.length) {
197                    return;
198                }
199
200                $wrapper.removeClass('cn-blok-visible');
201
202                window.setTimeout(function() {
203                    $wrapper.remove();
204                    $('#' + CookieNotice.config.styleId).remove();
205                }, 350);
206            }, this.config.hideDelay);
207        },
208
209        setCookie: function(name, value, days) {
210            var expires = '';
211            var secure = window.location.protocol === 'https:' ? '; secure' : '';
212            var sameSite = '; samesite=lax';
213
214            if (days) {
215                var date = new Date();
216                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
217                expires = '; expires=' + date.toUTCString();
218            }
219
220            document.cookie = name + '=' + (value || '') + expires + '; path=/' + sameSite + secure;
221        },
222
223        getCookie: function(name) {
224            var nameEQ = name + '=';
225            var ca = document.cookie.split(';');
226            var i;
227            var c;
228
229            for (i = 0; i < ca.length; i++) {
230                c = ca[i];
231
232                while (c.charAt(0) === ' ') {
233                    c = c.substring(1, c.length);
234                }
235
236                if (c.indexOf(nameEQ) === 0) {
237                    return c.substring(nameEQ.length, c.length);
238                }
239            }
240
241            return null;
242        }
243    };
244
245    if (document.readyState === 'complete') {
246        CookieNotice.init();
247    } else {
248        $(window).on('load', function() {
249            CookieNotice.init();
250        });
251    }
252    
253})(jQuery);

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.