PageSourceSearch

https://bit24.cash/blog/wp-content/themes/bit24-blog/assets/client/js/ajax.js?ver=2.1.95

js bit24.cash collected 2026-10-07 17:45:49 UTC 23,031 bytes, 649 lines download raw bytes

1jQuery(document).ready(function($) {
2    let searchDelay;
3    $.countPage = 1
4    $.stylePage = 'grid'
5    $.searchLimit = {}
6
7    $.makeAjaxCall = async function(action, data = {}) {
8        try {
9            const response = await $.ajax({
10                type: 'POST',
11                url: ajax_data.ajax_url,
12                data: {
13                    action,
14                    _wpnonce: ajax_data._wpnonce,
15                    ...data
16                }
17            });
18            return response;
19        } catch (error) {
20            console.error(`Error in ${action}:`, error);
21            throw error;
22        }
23    };
24
25    const makeAjaxCall = async (action, data = {}) => {
26        try {
27            const response = await $.ajax({
28                type: 'POST',
29                url: ajax_data.ajax_url,
30                data: {
31                    action,
32                    _wpnonce: ajax_data._wpnonce,
33                    ...data
34                }
35            });
36            return response;
37        } catch (error) {
38            console.error(`Error in ${action}:`, error);
39            throw error;
40        }
41    };
42
43    function goToSearchResults(key) {
44        key = key ? String(key).trim() : '';
45        if (key.length <= 2) return;
46        var base = (ajax_data.site_url || '').replace(/\/$/, '');
47        if (base) window.location.href = base + '/search/' + encodeURIComponent(key);
48    }
49
50    $('#searchBarInput').on('keydown', function(e) {
51        if (e.which === 13 || e.keyCode === 13) {
52            e.preventDefault();
53            goToSearchResults($(this).val());
54            return false;
55        }
56    });
57
58    $('#searchBarInput').keyup(async function() {
59        const key = $(this).val();
60        if (key.length === 0) {
61            await $.desktopSearchDefaultData();
62            return;
63        }
64        if (key.length > 2) {
65            clearTimeout(searchDelay);
66            searchDelay = setTimeout(async () => {
67                try {
68                    const data = await makeAjaxCall('search_post', { key });
69                    if (data._r) {
70                        $('#desktop-search-response-box').html(data.data);
71                    }
72                } catch (error) {
73                    console.error('Search error:', error);
74                }
75            }, 1000);
76        }
77    });
78
79    $('#logout').click(async function() {
80        try {
81            const response = await makeAjaxCall('remove_token');
82            if (response.success) {
83                window.history.replaceState(null, null, window.location.href);
84                location.reload();
85            }
86        } catch (error) {
87            console.error('Logout error:', error);
88        }
89    });
90
91    $('#mobile-menu-search-input').keyup(async function() {
92        const key = $(this).val();
93        if (key.length > 2) {
94            clearTimeout(searchDelay);
95            searchDelay = setTimeout(async () => {
96                try {
97                    const data = await makeAjaxCall('search_post', { key });
98                    if (data._r) {
99                        $('#mobile-search-response-box').html(data.data);
100                    }
101                } catch (error) {
102                    console.error('Mobile search error:', error);
103                }
104            }, 1000);
105        }
106    });
107
108    $(document).on('click', '.author-des-data', async function(event) {
109        const status = $(this).attr('_i')
110        try {
111            const data = await makeAjaxCall('author_des_handler', { status });
112            const resultsBox = $('#author-des-box');
113            const moreIcon = $('.more-icon');
114            status == 'more' ? $(moreIcon).css('transform', 'rotate(90deg)') : $(moreIcon).css('transform', 'rotate(0deg)')
115            $(resultsBox).html(data.html);
116        } catch (error) {
117            console.error('Author description error:', error);
118        }
119    });
120
121    $(document).on('click', '.remove-search-history', async function(event) {
122        event.preventDefault()
123        const parent = $(this).parent();
124        const index = $(parent).attr('_i');
125        try {
126            const data = await makeAjaxCall('remove_search_history', { index });
127            let resultsBox;
128            const width = $.getScreenWidth();
129            if (width > 991) {
130                resultsBox = $('#desktop-search-response-box');
131            } else {
132                resultsBox = $('#mobile-search-response-box');
133            }
134            $(resultsBox).html(data.data);
135        } catch (error) {
136            console.error('Remove search history error:', error);
137        }
138    });
139
140    $.mobileSearchDefaultData = async function() {
141        const searchBox = $('#mobile-search-response-box');
142        $(cover).fadeIn(300);
143        $(searchBox).addClass('opacity-100 translate-y-1 visible').removeClass('translate-y-7 invisible opacity-0');
144        try {
145            const data = await makeAjaxCall('search_default_data');
146            $('#mobile-search-response-box').html(data.data);
147        } catch (error) {
148            console.error('Mobile search default data error:', error);
149        }
150    };
151
152    $.desktopSearchDefaultData = async function() {
153        const searchBox = $('#desktop-search-response-box');
154        $(searchBox).addClass('opacity-100 translate-y-1 visible').removeClass('translate-y-7 invisible opacity-0');
155        try {
156            const data = await makeAjaxCall('search_default_data');
157            $('#desktop-search-response-box').html(data.data);
158        } catch (error) {
159            console.error('Desktop search default data error:', error);
160        }
161    };
162
163    $.postChangeStyle = async function() {
164        let termId = $('#post-body').attr('_mi');
165        let pageType = $('#post-body').attr('_t');
166        try {
167            const data = await makeAjaxCall('get_posts_data', {
168                term_id: termId,
169                count: $.countPage,
170                page: 0,
171                page_type: pageType,
172                style: $.stylePage
173            });
174            const resultsBox = $('#post-list');
175            $(resultsBox).html(data.html);
176            const pathname = window.location.pathname;
177            const newPath = pathname.replace(/\/page\/\d+\/?$/, '/');
178            if (newPath !== pathname) {
179                const url = new URL(window.location);
180                window.history.replaceState({}, document.title, newPath + url.search);
181                $('.paged-breadcrumb').remove()
182                $('.tag-page').remove()
183                $('.tag-description').removeClass('hidden')
184            }
185        } catch (error) {
186            console.error('Post change style error:', error);
187        }
188    }
189
190    $.postMore = async function(id, type) {
191        let termId = '';
192        let pageType = '';
193        $('#post-body').attr('_mi') !== '' ? termId = $('#post-body').attr('_mi') : termId = id;
194        $('#post-body').attr('_t') !== '' ? pageType = $('#post-body').attr('_t') : pageType = type;
195        $.countPage = $.countPage + 1;
196        try {
197            const data = await makeAjaxCall('get_posts_data', {
198                term_id: termId,
199                count: $.countPage,
200                page: 0,
201                page_type: pageType,
202                style: $.stylePage
203            });
204            $('.loader-inside-box').addClass('invisible').removeClass('visible');
205            if (data.next && $.countPage > 1) {
206                $('.loader-more').addClass('flex').removeClass('hidden');
207            } else {
208                $('.loader-more').addClass('hidden').removeClass('flex');
209            }
210            $('#post-list').html(data.html);
211        } catch (error) {
212            console.error('Post more error:', error);
213        }
214    }
215
216    $.postMorePaginate = async function(page) {
217        let termId = $('#post-body').attr('_mi');
218        let pageType = $('#post-body').attr('_t');
219        try {
220            const data = await makeAjaxCall('get_posts_data', {
221                term_id: termId,
222                page: page,
223                count: 1,
224                page_type: pageType,
225                style: $.stylePage
226            });
227            $('.loader-inside-box').addClass('invisible').removeClass('visible');
228            $('.loader-more').addClass('hidden').removeClass('flex');
229            $('#post-list').html(data.html);
230        } catch (error) {
231            console.error('Post more paginate error:', error);
232        }
233    }
234
235    if ($('#singlePostPopularCoins').length) {
236        (async () => {
237            try {
238                const response = await makeAjaxCall('get_single_page_popular_coins');
239                if (response.success && response.data.html) {
240                    $('#singlePostPopularCoins').html(response.data.html);
241                } else {
242                    console.error('Error: No valid HTML received in response.', response);
243                }
244            } catch (error) {
245                console.error('Popular coins error:', error);
246            }
247        })();
248    }
249
250    if ($('#singlePostRecentListedCoins').length) {
251        (async () => {
252            try {
253                const response = await makeAjaxCall('get_single_page_recent_listed_coins');
254                if (response.success && response.data.html) {
255                    $('#singlePostRecentListedCoins').html(response.data.html);
256                } else {
257                    console.error('Error: No valid HTML received in response.', response);
258                }
259            } catch (error) {
260                console.error('Recent listed coins error:', error);
261            }
262        })();
263    }
264
265    $('.submitSearchBar').click(function() {
266        goToSearchResults($('#searchBarInput').val() || $('#search-page-input').val());
267    });
268
269    $(document).on('click', '.news-modal-item', async function(e) {
270        e.stopPropagation();
271        $('body').addClass('overflow-hidden');
272        const modalBox = $('#day-news-modal-box');
273        if (!modalBox.hasClass('open')) {
274            $('#background-cover').fadeIn(300);
275            modalBox.fadeIn(300).addClass('open');
276        }
277
278        $('.news-modal-item').removeClass('text-t-brand-primary').addClass('text-t-primary')
279        const id = $(this).attr('_i')
280        
281        try {
282            const response = await makeAjaxCall('get_single_day_news', { id: id });
283            if (response.success && response.html) {
284                $('#day-news-content-box').html(response.html);
285                const $container = $('#day-news-modal-itel-list');
286                const $item = $(`.news-modal-item[_i="${id}"]`, $container);
287                const containerTop = $container.offset().top;
288                const containerHeight = $container.height();
289                const itemTop = $item.offset().top;
290                const itemHeight = $item.outerHeight();
291
292                const scrollTop = $container.scrollTop();
293                const relativeTop = itemTop - containerTop;
294
295                const newScrollTop = scrollTop + relativeTop - (containerHeight / 2) + (itemHeight / 2);
296                $item.removeClass('text-t-primary').addClass('text-t-brand-primary')
297                $container.animate({ scrollTop: newScrollTop }, 0);
298            } else {
299                console.error('Error: No valid HTML received in response.', response);
300            }
301        } catch (error) {
302            $('body').removeClass('overflow-hidden');
303            $('#background-cover').fadeOut(300);
304            console.error('Day news modal error:', error);
305        }
306    });
307
308    $('.show-news-modal').click(async function(e) {
309        e.stopPropagation();
310        $('body').addClass('overflow-hidden')
311        const modalBox = $('#day-news-modal-box');
312        if (!modalBox.hasClass('open')) {
313            modalBox.addClass('open');
314            $('#background-cover').fadeIn(300);
315            modalBox.fadeIn(300);
316
317            try {
318                const response = await makeAjaxCall('get_all_day_news');
319                if (response.success && response.html) {
320                    $('#day-news-content-box').html(response.html);
321                } else {
322                    console.error('Error: No valid HTML received in response.', response);
323                }
324            } catch (error) {
325                console.error('Day news error:', error);
326            }
327        }
328    });
329
330    let pollingTimeout = null;
331    let isTabVisible = document.visibilityState === 'visible';
332    let pollingInterval = 60000;
333    let isPolling = false;
334
335    async function runAiNewsPolling() {
336        if (!isTabVisible || isPolling) return;
337
338        isPolling = true;
339
340        try {
341            const response = await makeAjaxCall('update_single_day_news');
342            if (response.success && response.html) {
343                $('#news-box').hasClass('hidden') && $('#news-box').removeClass('hidden').addClass('flex');
344                ticckerAddItem(response.html)
345            } else {
346                console.log('Waiting: No new content');
347            }
348        } catch (error) {
349            console.error('Polling error:', error);
350        } finally {
351            isPolling = false;
352            scheduleNextPolling();
353        }
354    }
355
356    const carouselSettings = {
357        speed: 3,
358        direction: "next",
359        delay: 30,
360        mode: "horizontal"
361    };
362
363    function ticckerAddItem(item) {
364
365        const $newSlide = item;
366
367        $('.carouselTicker__list').html($newSlide);
368
369        var pluginInstance = $('.carouselTicker-start').data('carouselTicker');
370
371        if (pluginInstance && pluginInstance.reloadCarouselTicker) {
372            pluginInstance.reloadCarouselTicker(carouselSettings);
373        } else {
374
375            if (pluginInstance && pluginInstance.stop) {
376                pluginInstance.stop();
377            }
378
379            $('.carouselTicker__clone').remove();
380
381            $('.carouselTicker__wrap').contents().unwrap();
382
383            $('.carouselTicker__wrap').remove();
384            $('.carouselTicker__loader').remove();
385
386            setTimeout(function() {
387                $(".carouselTicker-start").carouselTicker(carouselSettings);
388            }, 100);
389        }
390    }
391
392    function scheduleNextPolling() {
393        clearTimeout(pollingTimeout);
394
395        if (isTabVisible) {
396            pollingTimeout = setTimeout(() => {
397                runAiNewsPolling();
398            }, pollingInterval);
399        }
400    }
401
402    function handleVisibilityChange() {
403        isTabVisible = document.visibilityState === 'visible';
404
405        if (isTabVisible) {
406            clearTimeout(pollingTimeout);
407            runAiNewsPolling();
408        } else {
409            clearTimeout(pollingTimeout);
410        }
411    }
412
413    function startLongPolling() {
414        document.addEventListener('visibilitychange', () => handleVisibilityChange());
415        runAiNewsPolling();
416    }
417
418    startLongPolling()
419
420    $('#exit_login').click(function() {
421        (async () => {
422            try {
423                const response = await makeAjaxCall('removeToken');
424                if (response.success) {
425                    window.location.reload()
426                }
427            } catch (error) {
428                console.error('خروج از حساب:', error);
429            }
430        })();
431    })
432
433    let aiRequest = null;
434    let stopAiGeneration = false
435
436    $(document).on('click', '#generate-ai', function() {
437        webengage.onReady(function() {
438            webengage.track('blog_ai_clicked', {
439                "title": ajax_data.is_single ? ajax_data.title : "blog_main_page",
440                "url": window.location.href
441            });
442        });
443        const $this = $(this);
444        $this.fadeOut();
445        $('body').addClass('overflow-hidden');
446        $this.addClass('pointer-events-none');
447        let param = {};
448        if (ajax_data.is_single) {
449            param = { id: ajax_data.post_id };
450        }
451
452        (async () => {
453            try {
454                const response = await makeAjaxCall('initAiGeneration', param);
455                if (response.success) {
456                    $('#background-cover').fadeIn(0);
457                    $('body').append(response.html);
458
459                    adjustHeights();
460                    if (response.posts.found.length === 0) {
461                        $('#ai-modal-item-list').append(response.loader)
462                    }
463                    stopAiGeneration = false;
464                    getAiGeneratedData(response.posts.all);
465
466                }
467            } catch (error) {
468                console.error('AI generation error:', error);
469            }
470        })();
471    });
472
473    $(window).on('resize', adjustHeights);
474
475    function adjustHeights() {
476        if ($(window).width() < 789) {
477            const modalHeight = $(window).height() - 24;
478            $('#ai-post-list-modal').css('height', modalHeight + 'px');
479            const itemListHeight = modalHeight - 82;
480            $('#ai-modal-item-list').css('height', itemListHeight + 'px');
481        } else {
482            $('#ai-post-list-modal').css('height', '');
483            $('#ai-modal-item-list').css('height', '655px');
484        }
485    }
486
487    let aiPostCounter = 1;
488
489    function getAiGeneratedData(combinedList, postNum = 0) {
490        if (stopAiGeneration) {
491            return;
492        }
493
494        const checkSingle = ajax_data.is_single;
495
496        if (combinedList.length > 0 && postNum < combinedList.length) {
497            aiRequest = makeAjaxCall('getAiPost', {
498                post_id: combinedList[postNum],
499                is_single: ajax_data.is_single
500            });
501
502            aiRequest.then(function(response) {
503                if (stopAiGeneration) return;
504                if (response.success) {
505                    $('#ai-post-list-loader').remove();
506                    $('#ai-modal-item-list').append(response.html);
507                    $(`#generated-${combinedList[postNum]}`).attr('_cP', aiPostCounter);
508                    aiPostCounter++;
509                    setTimeout(function() {
510                        $.typeText(`#generated-${combinedList[postNum]} .ai-content`, response.posts.content, combinedList[postNum], false, checkSingle)
511                            .then(function() {
512                                getAiGeneratedData(combinedList, postNum + 1);
513                            });
514                    }, 1000);
515                }
516            }).catch(function(error) {
517                if (stopAiGeneration) return;
518                console.error('AI post error:', error);
519            });
520        }
521    }
522
523    $(document).on('click', '.close-ai-modal', async function(event) {
524        const $list = $('#ai-modal-item-list');
525        const scrollHeight = $list.prop('scrollHeight');
526        const clientHeight = $list.innerHeight();
527        if (scrollHeight > clientHeight) {
528            const scrollTop = $list.scrollTop();
529            const scrollPercent = Math.ceil((scrollTop / (scrollHeight - clientHeight)) * 10) * 10;
530
531            const trackData = {
532                "is_scrollable": true,
533                "percent_of_scroll": scrollPercent > 100 ? 100 : scrollPercent,  // محدود به 100
534                "url": window.location.href
535            };
536
537            console.log('Tracking blog_ai_closed:', trackData);
538
539            webengage.onReady(function() {
540                webengage.track('blog_ai_closed', trackData);
541            });
542        } else {
543            const trackData = {
544                "is_scrollable": false,
545                "percent_of_scroll": 0,
546                "url": window.location.href
547            };
548
549            console.log('Tracking blog_ai_closed:', trackData);
550
551            webengage.onReady(function() {
552                webengage.track('blog_ai_closed', trackData);
553            });
554        }
555
556        $('#background-cover').fadeOut(100);
557        $('#ai-post-list-modal').remove()
558        $('#generate-ai').fadeIn().removeClass('pointer-events-none')
559        $('body').removeClass('overflow-hidden')
560        $('#ai-modal-item-list').html('');
561        stopAiGeneration = true;
562        // توقف کامل typeText
563        $.stopTypeText();
564        if (aiRequest && typeof aiRequest.abort === 'function') {
565            aiRequest.abort(); // ← قطع کردن درخواست
566        }
567
568    })
569
570    $(document).on('click', '.ai-post-to-single', function(e) {
571        e.preventDefault();
572
573        const $list = $('#ai-modal-item-list');
574        const scrollHeight = $list.prop('scrollHeight');
575        const clientHeight = $list.innerHeight();
576        if (scrollHeight > clientHeight) {
577            const scrollTop = $list.scrollTop();
578            const scrollPercent = Math.ceil((scrollTop / (scrollHeight - clientHeight)) * 10) * 10;
579
580            const trackData = {
581                "is_scrollable": true,
582                "percent_of_scroll": scrollPercent > 100 ? 100 : scrollPercent,  // محدود به 100
583                "url": window.location.href
584            };
585
586            console.log('Tracking blog_ai_closed:', trackData);
587
588            webengage.onReady(function() {
589                webengage.track('blog_ai_closed', trackData);
590            });
591        } else {
592            const trackData = {
593                "is_scrollable": false,
594                "percent_of_scroll": 0,
595                "url": window.location.href
596            };
597
598            console.log('Tracking blog_ai_closed:', trackData);
599
600            webengage.onReady(function() {
601                webengage.track('blog_ai_closed', trackData);
602            });
603        }
604
605        const $this = $(this);
606        const postLink = $this.attr('_h');
607        let redirected = false;
608
609        const trackData = {
610            "title": ajax_data.is_single ? ajax_data.title : "blog_main_page",
611            "url": window.location.href,
612            "targeted_url": postLink,
613            "title_or_link": $this.hasClass('ai-title') ? "title" : "link",
614            "max_index": $this.closest('.generated-ai-box').attr('_ap'),
615            "index": $this.closest('.generated-ai-box').attr('_cp')
616        };
617
618        const fallback = setTimeout(function() {
619            if (!redirected) {
620                console.warn("Fallback redirect triggered", trackData);
621                redirected = true;
622                window.location.href = postLink;
623            }
624        }, 1000);
625
626        webengage.onReady(function() {
627            console.log("Tracking started:", trackData);
628
629            webengage.track(
630                'blog_ai_to_post',
631                trackData,
632                function() {
633                    console.log("Tracking success:", trackData);
634                    if (!redirected) {
635                        redirected = true;
636                        clearTimeout(fallback);
637                        window.location.href = postLink;
638                    }
639                }
640            );
641        });
642    });
643
644
645    if (ajax_data.theme_url) {
646        $('blockquote').prepend(`<img class="!absolute !m-0 !top-4 !right-5" src="${ajax_data.theme_url}/assets/client/images/public/quote.png" alt="icon" width="24" height="18">`);
647    }
648
649});

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.