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
|
{{ if eq .Kind "page" }}
<div id="like-container">
<img id="like-icon" src="/icons/thumb-up.svg" alt="Like" />
<span id="like-count">0</span>
</div>
<style>
#like-container {
text-align: center;
margin: -2.5rem 0 -5rem 0rem;
user-select: none;
color: white;
}
#like-icon {
width: 33px;
height: 36px;
cursor: pointer;
transition: transform 0.15s ease;
filter: brightness(0) invert(1);
}
#like-icon.clicked {
animation: bounce 0.4s ease;
}
@keyframes bounce {
0% { transform: scale(1); }
50% { transform: scale(1.4); }
100% { transform: scale(1); }
}
#like-count {
display: inline-block;
margin-left: 0px;
font-size: 1.5rem;
}
</style>
<script>
(function () {
const appId = 'QMq4HFQshcHbTkGqUoX10lDK-gzGzoHsz';
const appKey = 'MgjYDq8Uwr9g8x8HEJ24tyEB';
const serverURL = 'https://qmz4hfqs.lc-cn-n1-shared.com';
const pagePath = window.location.pathname;
const localKey = 'liked_' + pagePath;
const maxLikes = 50;
const headers = {
'X-LC-Id': appId,
'X-LC-Key': appKey,
'Content-Type': 'application/json'
};
async function fetchLike() {
const res = await fetch(`${serverURL}/1.1/classes/Like?where=` + encodeURIComponent(JSON.stringify({ path: pagePath })), { headers });
const data = await res.json();
if (data.results.length > 0) {
return data.results[0];
} else {
const resNew = await fetch(`${serverURL}/1.1/classes/Like`, {
method: 'POST',
headers,
body: JSON.stringify({
path: pagePath,
count: 0,
ACL: {
"*": {
"read": true,
"write": true
}
}
})
});
return await resNew.json();
}
}
async function increaseLike(objectId) {
await fetch(`${serverURL}/1.1/classes/Like/${objectId}`, {
method: 'PUT',
headers,
body: JSON.stringify({ count: { __op: 'Increment', amount: 1 } })
});
}
document.addEventListener('DOMContentLoaded', async () => {
const icon = document.getElementById('like-icon');
const count = document.getElementById('like-count');
const likedCount = parseInt(localStorage.getItem(localKey) || '0');
const data = await fetchLike();
count.textContent = data.count || 0;
icon.addEventListener('click', async () => {
let current = parseInt(localStorage.getItem(localKey) || '0');
if (current >= maxLikes) return;
icon.classList.add('clicked');
setTimeout(() => icon.classList.remove('clicked'), 400);
count.textContent = parseInt(count.textContent) + 1;
localStorage.setItem(localKey, current + 1);
await increaseLike(data.objectId);
});
});
})();
</script>
{{ end }}
|