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
|
window.addEventListener('DOMContentLoaded', () => {
// This script insert a love button at the end of an article page
const articles = document.querySelectorAll('article');
function getLikeEndPoint() {
let currentArticleName = window.location.pathname.split('/')[2];
if (currentArticleName.indexOf('.') > 0) {
currentArticleName = currentArticleName.slice(0, currentArticleName.lastIndexOf('.'))
}
return `${window.location.origin}/like/${currentArticleName}`;
}
// The front page have multiple articles
if (articles.length === 1) {
const article = articles[0];
const likes = article.querySelector('.likes');
const nbLikes = article.querySelector('.nbLikes');
const footnotes = article.querySelector('.footnotes');
likes.style="display: 'block';";
const updateNbLikes = () => {
fetch(getLikeEndPoint(), {
method: 'GET',
headers: {
'i-love-what-you-do': '<3',
},
}).then((res) => {
if (res.ok) {
res.text().then((text) => nbLikes.textContent = text);
} else {
nbLikes.textContent = "";
}
});
};
updateNbLikes();
if (footnotes) {
// We want: article.insertBefore(likes, footnotes);
footnotes.before(likes);
}
const icon = likes.querySelector('.icon');
const messagesLike = likes.querySelector('.likesNotes');
icon.addEventListener('mouseover', () => {
for (const c of icon.children) {
const path = c.querySelector('path');
path?.classList.add('anim');
}
});
icon.addEventListener('mouseout', () => {
for (const c of icon.children) {
const path = c.querySelector('path');
path?.classList.remove('anim');
}
});
let timeOut;
let messageText;
icon.addEventListener('click', () => {
const c = icon.children[1];
{
const path = c.querySelector('path');
console.log(path)
path.classList.add('anim-click');
path.getAnimations().forEach(anim => {
anim.cancel();
anim.play();
});
fetch(getLikeEndPoint(), {
method: 'POST',
headers: {
'i-love-what-you-do': '<3',
},
}).then(response => {
const currentText = messagesLike.textContent;
const t = response.text().then(text => {
messagesLike.textContent = text;
return text;
});
if (response.ok) {
messagesLike.classList.remove('err');
t.then((t) => messageText = t);
updateNbLikes();
} else {
if (!messageText) {
messageText = currentText;
}
messagesLike.classList.add('err');
setTimeout(() => {
messagesLike.classList.remove('err');
messagesLike.textContent = messageText;
}, 3000);
}
}).catch(() => {
messagesLike.classList.add('err');
messagesLike.textContent = 'Désolé, le service est indisponible';
messageText = '';
});
}
});
}
});
|