80 lines
2.7 KiB
JavaScript
80 lines
2.7 KiB
JavaScript
/**
|
|
* وبلاگ — نوار پیشرفت مطالعه، کپی لینک، ثبت بازدید (AJAX)
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var progress = document.getElementById('single-reading-progress-bar');
|
|
var article = document.querySelector('.single-article');
|
|
if (progress && article) {
|
|
var onScroll = function () {
|
|
var rect = article.getBoundingClientRect();
|
|
var total = article.offsetHeight - window.innerHeight;
|
|
if (total <= 0) {
|
|
progress.style.width = '0%';
|
|
return;
|
|
}
|
|
var scrolled = Math.min(Math.max(-rect.top, 0), total);
|
|
progress.style.width = (scrolled / total) * 100 + '%';
|
|
};
|
|
window.addEventListener('scroll', onScroll, { passive: true });
|
|
onScroll();
|
|
}
|
|
|
|
document.querySelectorAll('[data-copy-url]').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
var url = btn.getAttribute('data-copy-url');
|
|
if (!url || !navigator.clipboard) {
|
|
return;
|
|
}
|
|
navigator.clipboard.writeText(url).then(function () {
|
|
var label = btn.querySelector('[data-copy-label]');
|
|
var done = btn.getAttribute('data-copied-text') || 'کپی شد!';
|
|
if (label) {
|
|
var orig = label.textContent;
|
|
label.textContent = done;
|
|
btn.classList.add('is-copied');
|
|
setTimeout(function () {
|
|
label.textContent = orig;
|
|
btn.classList.remove('is-copied');
|
|
}, 2000);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
if (typeof hesabix2Blog === 'undefined' || !hesabix2Blog.postId) {
|
|
return;
|
|
}
|
|
|
|
var postId = parseInt(hesabix2Blog.postId, 10);
|
|
if (!postId) {
|
|
return;
|
|
}
|
|
|
|
var body = new FormData();
|
|
body.append('action', hesabix2Blog.trackAction);
|
|
body.append('post_id', String(postId));
|
|
body.append('nonce', hesabix2Blog.trackNonce);
|
|
|
|
fetch(hesabix2Blog.ajaxUrl, {
|
|
method: 'POST',
|
|
body: body,
|
|
credentials: 'same-origin',
|
|
})
|
|
.then(function (res) {
|
|
return res.json();
|
|
})
|
|
.then(function (data) {
|
|
if (!data || !data.success || !data.data || !data.data.views) {
|
|
return;
|
|
}
|
|
var el = document.getElementById('single-post-views-count');
|
|
if (el) {
|
|
el.textContent = data.data.views;
|
|
}
|
|
})
|
|
.catch(function () {
|
|
/* نادیده — بازدید سمت سرور در صورت عدم کش ثبت شده */
|
|
});
|
|
})();
|