-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (62 loc) · 2.33 KB
/
script.js
File metadata and controls
69 lines (62 loc) · 2.33 KB
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
const url = new URL(window.location.href);
const prefixUrl = "https://c1ce-2a01-cb05-9121-e400-edbe-f9cc-3d6d-ac76.ngrok-free.app/recommendations/"
const visitorId= url.searchParams.get('q');
const style = "top:40px;right:5px;position:fixed;background-color:white;box-shadow: 2px 2px 2px gray;border-radius: 3px;padding: 2px;border: 1px solid gray;"
if (visitorId) {
getRecommendations(visitorId)
.then(recos => {
addRecoDiv(recos)
})
}
async function getRecommendations(visitorId) {
try {
const reco_url = prefixUrl + visitorId
const reponse = await fetch(reco_url);
const recommendations = await reponse.json();
reco_array = JSON.parse(recommendations)
console.log(reco_array)
return reco_array
}catch(e) {
console.error(e)
}
}
async function addRecoDiv(recos) {
if (!recos || recos.length == 0) {
return
}
const div = document.createElement("div")
const title = document.createElement("p")
title.setAttribute("style", "font-size:15px;font-weight:bold;cursor:pointer;")
title.innerHTML = "Recommandations"
div.appendChild(title)
const recoDiv = document.createElement("div")
recoDiv.setAttribute("style", "")
div.appendChild(recoDiv)
div.setAttribute("style", style)
const ul = document.createElement("ul")
//ul.setAttribute("style", "list-style:none;padding:0px;margin:0px;font-size:10px;animation-duration:0.5s;")// display: flex;flex - direction: column;
recoDiv.setAttribute("style", "max-height: 0;transition: max-height 0.5s ease-out;overflow:hidden;")
recoDiv.appendChild(ul)
const maxLength = 10
recos.forEach(reco => {
const li = document.createElement("li")
const a = document.createElement("a")
a.href = reco[1]+"#page="+reco[2]
a.target = "_blank"
const name = reco[0].length > maxLength ? (reco[0].substring(0, maxLength) + "...") : reco[0]
a.innerHTML = name
a.setAttribute("title",reco[0])
li.appendChild(a)
ul.appendChild(li)
})
div.opened=false
title.addEventListener("click", () => {
if (!div.opened) {
recoDiv.style.maxHeight=""
} else {
recoDiv.style.maxHeight = "0"
}
div.opened = !div.opened
})
document.body.appendChild(div)
}