PageSourceSearch

https://ancor.co.th/local/templates/.default/js/Ads.js

js ancor.co.th collected 2026-10-05 03:42:24 UTC 3,940 bytes, 129 lines download raw bytes

1function AncorAds(options) {
2    const cookieName = 'cookie-accept';
3    this.options = options;
4    this.selector = '.i-adsDialog';
5    this.nextShow = true;
6    this.cookieName = 'ancor_ads';
7
8    if (this.checkShow(cookieName)) {
9        this.init();
10    }
11}
12
13AncorAds.prototype.checkShow = function(cookieName) {
14    let isDesktop = !window.matchMedia('(max-width: 1023.98px)').matches;
15    let hasCookie = this.hasCookie(cookieName);
16
17    return isDesktop || (!isDesktop && hasCookie) ;
18};
19AncorAds.prototype.init = function() {
20    let base = this.selector;
21    let self = this;
22    $.addInit(function(context){
23        $(base, context).each(function() {
24            let component = $(this);
25            let close = component.find(base + '-close');
26            self.queueNew(component, base);
27            close.on('click', function(e) {
28                component.removeClass('m-active');
29                self.read(component.data('id'), function () {
30                    if (self.nextShow) {
31                        self.queueNew(component, base);
32                    }
33                });
34            });
35        });
36    });
37    $.addInit(function(context) {
38        $('.b-form', context).each(function() {
39            let form = $(this).find('form')
40            if (!form.length) {
41                form = $(this)
42            }
43            
44            form.on('submit', function() {
45                if (!this.checkValidity()) {
46                    return;
47                }
48                let formId = $(this).find('[name=WEB_FORM_ID]').val()
49                if (formId) {
50                    self.query('/ajax/ads/forms/send/', {
51                        formId: formId,
52                        page: window.location.pathname
53                    }, function (result) {})
54                }
55            })
56            form.find('input,select').on('input, change', function () {
57                let formId = form.find('[name=WEB_FORM_ID]').val()
58                if (formId) {
59                    self.query('/ajax/ads/forms/start/', {
60                        formId: formId,
61                        page: window.location.pathname
62                    }, function (result) {})
63                }
64            })
65        });
66    });
67};
68
69AncorAds.prototype.read = function(id, callback) {
70    let cookie = this.getCookie(this.cookieName);
71
72    if (cookie.length) {
73        cookie = cookie.replaceAll('%7C', '|');
74        cookie += '|';
75    }
76    cookie += id;
77
78    this.setCookie(this.cookieName, cookie, 365);
79    callback();
80}
81
82AncorAds.prototype.queueNew = function(component, base) {
83    let self = this;
84    setTimeout(function() {
85        self.query('/ajax/ads/', {}, function (result) {
86            if (result.length) {
87                component.find(base + '-content').html(result);
88                component.data('id', component.find('.i-news-id').data('id'));
89                component.addClass('m-active');
90            } else {
91                self.nextShow = false;
92            }
93        });
94    }, 5000);
95};
96AncorAds.prototype.query = function(url, data, callback) {
97
98    let options = {
99        url: url,
100        data: data,
101        type: 'GET',
102    };
103
104    if (typeof callback !== 'undefined') {
105        options['success'] = callback;
106    }
107
108    $.ajax(options);
109};
110
111AncorAds.prototype.hasCookie = function (cookie) {
112    return new RegExp('(^|; )' + cookie + '=').test(document.cookie);
113}
114
115AncorAds.prototype.setCookie = function (cookie, value, days) {
116    let cookieExpires = new Date();
117    cookieExpires.setDate(cookieExpires.getDate() + days);
118    document.cookie = encodeURIComponent(cookie) + '=' + encodeURIComponent(value) + ';path=/;expires=' + cookieExpires.toUTCString();
119}
120
121AncorAds.prototype.getCookie = function (name) {
122    const value = `; ${document.cookie}`;
123    const parts = value.split(`; ${name}=`);
124    if (parts.length === 2) return parts.pop().split(';').shift();
125
126    return '';
127}
128
129new AncorAds();

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.