2025-2-26-fixed
This commit is contained in:
31
themes/next/layout/_third-party/statistics/busuanzi-counter.swig
vendored
Normal file
31
themes/next/layout/_third-party/statistics/busuanzi-counter.swig
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
{%- if theme.busuanzi_count.enable %}
|
||||
<div class="busuanzi-count">
|
||||
<script{{ pjax }} async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
|
||||
|
||||
{%- if theme.busuanzi_count.total_visitors %}
|
||||
<span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
|
||||
<span class="post-meta-item-icon">
|
||||
<i class="{{ theme.busuanzi_count.total_visitors_icon }}"></i>
|
||||
</span>
|
||||
<span class="site-uv" title="{{ __('footer.total_visitors') }}">
|
||||
<span id="busuanzi_value_site_uv"></span>
|
||||
</span>
|
||||
</span>
|
||||
{%- endif %}
|
||||
|
||||
{%- if theme.busuanzi_count.total_visitors and theme.busuanzi_count.total_views %}
|
||||
<span class="post-meta-divider">|</span>
|
||||
{%- endif %}
|
||||
|
||||
{%- if theme.busuanzi_count.total_views %}
|
||||
<span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
|
||||
<span class="post-meta-item-icon">
|
||||
<i class="{{ theme.busuanzi_count.total_views_icon }}"></i>
|
||||
</span>
|
||||
<span class="site-pv" title="{{ __('footer.total_views') }}">
|
||||
<span id="busuanzi_value_site_pv"></span>
|
||||
</span>
|
||||
</span>
|
||||
{%- endif %}
|
||||
</div>
|
||||
{%- endif %}
|
||||
5
themes/next/layout/_third-party/statistics/cnzz-analytics.swig
vendored
Normal file
5
themes/next/layout/_third-party/statistics/cnzz-analytics.swig
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{%- if theme.cnzz_siteid %}
|
||||
<div style="display: none;">
|
||||
<script{{ pjax }} src="//s95.cnzz.com/z_stat.php?id={{ theme.cnzz_siteid }}&web_id={{ theme.cnzz_siteid }}"></script>
|
||||
</div>
|
||||
{%- endif %}
|
||||
73
themes/next/layout/_third-party/statistics/firestore.swig
vendored
Normal file
73
themes/next/layout/_third-party/statistics/firestore.swig
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
{%- if theme.firestore.enable %}
|
||||
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>
|
||||
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-firestore.js"></script>
|
||||
<script>
|
||||
firebase.initializeApp({
|
||||
apiKey : '{{ theme.firestore.apiKey }}',
|
||||
projectId: '{{ theme.firestore.projectId }}'
|
||||
});
|
||||
|
||||
function getCount(doc, increaseCount) {
|
||||
// IncreaseCount will be false when not in article page
|
||||
return doc.get().then(d => {
|
||||
var count = 0;
|
||||
if (!d.exists) { // Has no data, initialize count
|
||||
if (increaseCount) {
|
||||
doc.set({
|
||||
count: 1
|
||||
});
|
||||
count = 1;
|
||||
}
|
||||
} else { // Has data
|
||||
count = d.data().count;
|
||||
if (increaseCount) {
|
||||
// If first view this article
|
||||
doc.set({ // Increase count
|
||||
count: count + 1
|
||||
});
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
});
|
||||
}
|
||||
|
||||
function appendCountTo(el) {
|
||||
return count => {
|
||||
el.innerText = count;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script{{ pjax }}>
|
||||
(function() {
|
||||
var db = firebase.firestore();
|
||||
var articles = db.collection('{{ theme.firestore.collection }}');
|
||||
|
||||
if (CONFIG.page.isPost) { // Is article page
|
||||
var title = document.querySelector('.post-title').innerText.trim();
|
||||
var doc = articles.doc(title);
|
||||
var increaseCount = CONFIG.hostname === location.hostname;
|
||||
if (localStorage.getItem(title)) {
|
||||
increaseCount = false;
|
||||
} else {
|
||||
// Mark as visited
|
||||
localStorage.setItem(title, true);
|
||||
}
|
||||
getCount(doc, increaseCount).then(appendCountTo(document.querySelector('.firestore-visitors-count')));
|
||||
} else if (CONFIG.page.isHome) { // Is index page
|
||||
var promises = [...document.querySelectorAll('.post-title')].map(element => {
|
||||
var title = element.innerText.trim();
|
||||
var doc = articles.doc(title);
|
||||
return getCount(doc);
|
||||
});
|
||||
Promise.all(promises).then(counts => {
|
||||
var metas = document.querySelectorAll('.firestore-visitors-count');
|
||||
counts.forEach((val, idx) => {
|
||||
appendCountTo(metas[idx])(val);
|
||||
});
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
{%- endif %}
|
||||
4
themes/next/layout/_third-party/statistics/index.swig
vendored
Normal file
4
themes/next/layout/_third-party/statistics/index.swig
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{% include 'busuanzi-counter.swig' %}
|
||||
{% include 'cnzz-analytics.swig' %}
|
||||
{% include 'firestore.swig' %}
|
||||
{% include 'lean-analytics.swig' %}
|
||||
99
themes/next/layout/_third-party/statistics/lean-analytics.swig
vendored
Normal file
99
themes/next/layout/_third-party/statistics/lean-analytics.swig
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
{%- if theme.leancloud_visitors.enable and not theme.valine.visitor %}
|
||||
<script{{ pjax }}>
|
||||
(function() {
|
||||
function leancloudSelector(url) {
|
||||
url = encodeURI(url);
|
||||
return document.getElementById(url).querySelector('.leancloud-visitors-count');
|
||||
}
|
||||
|
||||
function addCount(Counter) {
|
||||
var visitors = document.querySelector('.leancloud_visitors');
|
||||
var url = decodeURI(visitors.id);
|
||||
var title = visitors.dataset.flagTitle;
|
||||
|
||||
Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
|
||||
.then(response => response.json())
|
||||
.then(({ results }) => {
|
||||
if (results.length > 0) {
|
||||
var counter = results[0];
|
||||
leancloudSelector(url).innerText = counter.time + 1;
|
||||
Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
|
||||
.catch(error => {
|
||||
console.error('Failed to save visitor count', error);
|
||||
});
|
||||
} else {
|
||||
{%- if theme.leancloud_visitors.security %}
|
||||
leancloudSelector(url).innerText = 'Counter not initialized! More info at console err msg.';
|
||||
console.error('ATTENTION! LeanCloud counter has security bug, see how to solve it here: https://github.com/theme-next/hexo-leancloud-counter-security. \n However, you can still use LeanCloud without security, by setting `security` option to `false`.');
|
||||
{% else %}
|
||||
Counter('post', '/classes/Counter', { title, url, time: 1 })
|
||||
.then(response => response.json())
|
||||
.then(() => {
|
||||
leancloudSelector(url).innerText = 1;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to create', error);
|
||||
});
|
||||
{%- endif %}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('LeanCloud Counter Error', error);
|
||||
});
|
||||
}
|
||||
|
||||
function showTime(Counter) {
|
||||
var visitors = document.querySelectorAll('.leancloud_visitors');
|
||||
var entries = [...visitors].map(element => {
|
||||
return decodeURI(element.id);
|
||||
});
|
||||
|
||||
Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
|
||||
.then(response => response.json())
|
||||
.then(({ results }) => {
|
||||
for (let url of entries) {
|
||||
let target = results.find(item => item.url === url);
|
||||
leancloudSelector(url).innerText = target ? target.time : 0;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('LeanCloud Counter Error', error);
|
||||
});
|
||||
}
|
||||
|
||||
let { app_id, app_key, server_url } = {{ theme.leancloud_visitors | json }};
|
||||
function fetchData(api_server) {
|
||||
var Counter = (method, url, data) => {
|
||||
return fetch(`${api_server}/1.1${url}`, {
|
||||
method,
|
||||
headers: {
|
||||
'X-LC-Id' : app_id,
|
||||
'X-LC-Key' : app_key,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
};
|
||||
if (CONFIG.page.isPost) {
|
||||
if (CONFIG.hostname !== location.hostname) return;
|
||||
addCount(Counter);
|
||||
} else if (document.querySelectorAll('.post-title-link').length >= 1) {
|
||||
showTime(Counter);
|
||||
}
|
||||
}
|
||||
|
||||
let api_server = app_id.slice(-9) !== '-MdYXbMMI' ? server_url : `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;
|
||||
|
||||
if (api_server) {
|
||||
fetchData(api_server);
|
||||
} else {
|
||||
fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
|
||||
.then(response => response.json())
|
||||
.then(({ api_server }) => {
|
||||
fetchData('https://' + api_server);
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
{%- endif %}
|
||||
Reference in New Issue
Block a user