根據此篇博客(點擊查看) 配置出本身的博客閱讀量,裏面介紹瞭如何配置開通 leancloud
應用javascript
固然介紹我如何配置 yilia
顯示本身的瀏覽量的.css
yilia
主題下修改 _config.yml
添加以下配置信息# 添加瀏覽量 leancloud_visitors: enable: true app_id: ************** app_key: ************ #添加一下js插件的 CDN地址 js_cdn: jquery: https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js av : //cdn1.lncld.net/static/js/2.5.0/av-min.js
layout\_parial\post
找到 title.ejs
文件,添加顯示閱讀量的html代碼:<% if (post.link){ %> <h1 itemprop="name"> <a class="<%= class_name %>" href="<%- url_for(post.link) %>" target="_blank" itemprop="url"><%= post.title %></a> </h1> <% } else if (post.title){ %> <% if (index){ %> <h1 itemprop="name"> <a class="<%= class_name %>" href="<%- url_for(post.path) %>"><%= post.title %></a> </h1> <% } else { %> <h1 class="<%= class_name %>" itemprop="name"> <%= post.title %> </h1> <% } %> <% } %> #上面是 yilia 主題的標題 下面是我本身添加的閱讀量 html 代碼 # span 給的 id 是文章的 url 做爲惟一的 key 值 <% if (theme.leancloud_visitors.enable){ %> <a href="javascript:;" class="archive-article-date"> <i class="icon-smile icon"></i> 閱讀數:<span id="<%- url_for(post.path) %>" class="pageViews">0</span>次 </a> <% } %>
3.添加 js
代碼, 在layout\_parial
找到 after.footer.ejs
添加以下代碼:html
<script> var yiliaConfig = { mathjax: <%=theme.mathjax%>, isHome: <%=is_home()%>, isPost: <%=is_post()%>, isArchive: <%=is_archive()%>, isTag: <%=is_tag()%>, isCategory: <%=is_category()%>, open_in_new: <%=theme.open_in_new%>, toc_hide_index: <%=theme.toc_hide_index%>, root: "<%=config.root%>", innerArchive: <%=theme.smart_menu.innerArchive ? true : false%>, showTags: <%=(theme.slider && theme.slider.showTags) ? true : false%> } </script> #上面是 yilia 主題配置自帶 下面是我本身添加的js代碼 <script src="<%- theme.js_cdn.jquery %>"></script> <% if (theme.leancloud_visitors.enable){ %> //這是自定一個js文件,放在 layout\_parial\post 目錄 leancloud.ejs中 <%- partial('post/leancloud') %> <% } %> #下面是 yilia 主題配置自帶 <%- partial('script') %> <% if (theme.mathjax){ %> <%- partial('mathjax') %> <% } %>
leancloud.ejs
內容:java
<script src="<%- theme.js_cdn.av %>"></script> <script type="text/javascript"> if(<%- theme.leancloud_visitors.enable %>){ var leancloud_app_id = '<%- theme.leancloud_visitors.app_id %>'; var leancloud_app_key = '<%- theme.leancloud_visitors.app_key %>'; AV.init({ appId: leancloud_app_id, appKey: leancloud_app_key }); var pageViewsLength = $(".pageViews").length; var isIndex = $(".pageViews").length > 1 ?true:false; function showTime() { var Counter = AV.Object.extend("Counter"); if(isIndex){ $(".pageViews").each(function(){ showPageViewsNum($(this),Counter); addPageViewsNum($(this)); }); }else{ showPageViewsNum($(".pageViews"),Counter); addPageViewsNum($(".pageViews")); } } //顯示閱讀量 function showPageViewsNum(ele,Counter){ var query = new AV.Query("Counter"); var url = ele.attr('id').trim(); query.equalTo("words", url); query.count().then(function (number) { $(document.getElementById(url)).text(number? number : '0'); }, function (error) { $(document.getElementById(url)).text('0'); }); } //追加閱讀量 function addPageViewsNum(ele){ var url = ele.attr('id').trim(); var Counter = AV.Object.extend("Counter"); var query = new Counter; query.save({ words: url }).then(function (object) {}); } if(pageViewsLength){ //此處判斷等於 1 在執行 可去除循環 showTime(); } } </script>
而後就是熟悉的 hexo g
hexo s
查看,記得要在 leancloud
[安全中心] [Web 安全域名] 設置2個安全域名,一個是正式環境的域名,一個是調試的域名.否則會報錯.
預覽查看: blog.easydo.workjquery