-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
168 lines (156 loc) · 5.05 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
layout: page
title: ZFX的学习笔记
description: "向着目标前进,加油"
header-img: "/img/home-bg.jpg"
---
<!-- 网站主页 -->
{% for post in paginator.posts %}
<div class="post-preview">
<a href="{{ post.url | prepend: site.baseurl }}">
<h2 class="post-title">
{{ post.title }}
</h2>
{% if post.subtitle %}
<h3 class="post-subtitle">
{{ post.subtitle }}
</h3>
{% endif %}
{% if post.description %}
<div class="post-content-preview">
{% if post.description %}
{{ post.description }}
{% endif %}
</div>
{% endif %}
</a>
<p class="post-meta">
发表时间:{{ post.date | date: "%Y-%m-%d" }} 浏览量:<span id="{{ post.url }}" class="leancloud_visitors" data-flag-title="{{ post.title }}"> - </span>次. 字数:{{ post.content | number_of_words }}
</p>
</div>
<hr>
{% endfor %}
<script src="//unpkg.com/[email protected]/dist/av-min.js"></script>
<script>
AV.init({
appId: "b6xbX21qO1uIQJAVuRHfOXQe-gzGzoHsz",
appKey: "Dx4XP9hYgFhPhBsPEjf50Is0",
serverURL: "https://leancloud.zfxkj.top"
});
var name='Counter';
function createRecord(Counter){
// 设置 ACL
var acl = new AV.ACL();
acl.setPublicReadAccess(true);
acl.setPublicWriteAccess(true);
// 获得span的所有元素
var elements=document.getElementsByClassName('leancloud_visitors');
// 一次创建多条记录
var allcounter=[];
for (var i = 0; i < elements.length ; i++) {
// 若某span的内容不包括 '-' ,则不必创建记录
if(elements[i].textContent.indexOf('-') == -1){
continue;
}
var title = elements[i].getAttribute('data-flag-title');
var url = elements[i].id;
var newcounter = new Counter();
newcounter.setACL(acl);
newcounter.set("title", title);
newcounter.set("url", url);
newcounter.set("time", 0);
allcounter.push(newcounter);
// 顺便更新显示span为默认值0
elements[i].textContent=0;
}
// 判断是否需要新建记录
if(allcounter.length == 0){
return;
}
AV.Object.saveAll(allcounter).then(function (todo) {
// 成功保存记录之后
console.log('创建记录成功!');
}, function (error) {
// 异常错误
console.error('创建记录失败: ' + error.message);
});
}
function showCount(Counter){
// 是否需要创建新纪录的标志(添加一篇新文章)
var flag=false;
var query = new AV.Query(name);
query.greaterThanOrEqualTo('time', 0);
query.find().then(function (results) {
// 当获取到的记录为0时置默认值
if(results.length==0){
$('.leancloud_visitors').text('-');
flag=true;
console.log('返回查询记录为空');
// 如果获取到空记录就创建新记录
createRecord(Counter);
return;
}
// 将获取到的数据设置为text
for (var i = 0; i < results.length; i++) {
var item = results[i];
var url = item.get('url');
var time = item.get('time');
try {
var element = document.getElementById(url);
// 删除原字符串中的空格
if(element.textContent.replace(/\s+| /ig,'') == '-' || parseInt(element.textContent) < time){
element.textContent = time;
}
} catch (error) {
// console.log("获取"+url+"的网页元素出错:"+error);
}
}
// 当某个span含有默认值时说明需要创建记录
if($('.leancloud_visitors').text().indexOf("-") != -1){
flag=true;
}
// 当获取的记录数与span个数不吻合时
if(results.length != $('.leancloud_visitors').length){
flag=true;
}
if(flag){
createRecord(Counter);
}
}, function (error) {
console.log('query error:'+error.message);
});
}
$(function() {
var Counter = AV.Object.extend(name);
showCount(Counter);
});
</script>
<!-- Pager -->
<ul class="pager">
{% if paginator.previous_page %}
<li class="home previous">
{% if paginator.previous_page == 1 %}
<a href="{{ site.BASE_PATH }}/">← 上一页</a>
{% else %}
<a href="{{ site.BASE_PATH }}/{{ site.paginate_path | replace: ':num', paginator.previous_page }}">← 上一页</a>
{% endif %}
</li>
{% else %}
<li class="home previous disabled">
<a>← 上一页</a>
</li>
{% endif %}
<li>
<span class="page_number">页面: {{ paginator.page }} of {{ paginator.total_pages }}</span>
</li>
{% if paginator.next_page %}
<li class="home next">
<a href="{{ site.BASE_PATH }}/{{ site.paginate_path | replace: ':num', paginator.next_page }}">下一页 →</a>
</li>
{% else %}
<li class="home next disabled">
<a>下一页 →</a>
</li>
{% endif %}
</ul>
<br>