0%

## java Category

2019
09-23
AbstractQueuedSynchronizer
{ 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 { 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`.'); } }) .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 } = {"enable":true,"app_id":"ysza182Vghlqjdt7QiwGLLJy-gzGzoHsz","app_key":"s9GDqbn7gnGGkusf66YRVccw","server_url":"https://leancloud.cn","security":true}; 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); }); } })();