-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts_tv.js
More file actions
84 lines (76 loc) · 2.62 KB
/
scripts_tv.js
File metadata and controls
84 lines (76 loc) · 2.62 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
async function runToAspire(url, part) {
try {
const response = await fetch(url)
if (!response.ok) {
throw new Error('Network response was not ok')
}
const
html = await response.text(),
parser = new DOMParser(),
newHtml = parser.parseFromString(html, 'text/html'),
section = newHtml.querySelector(part)
if (section) {
return section
}
else {
console.log('élément nont trouvé')
return null
}
}
catch (error) {
console.error(error)
return null
}
}
function createElement(element, data, parent) {
var element = document.createElement(element)
element.textContent = data
return parent.appendChild(element)
}
function runHtml(data) {
[data].forEach(function(items) {
items.forEach(function(item) {
var a = document.createElement('a')
a.href = item.link.replace(local, website)
a.target = "_blank"
a.classList.add('card')
createElement('h2', item.title, a)
createElement('span', item.hour, a)
createElement('p', item.description, a)
var img = document.createElement('img')
img.src = item.img.replace(local, website)
a.appendChild(img)
article.appendChild(a)
})
})
}
const
//local = "http://localhost",
local = "https://carassin.lintermediaire.be",
website = "https://www.mon-programme-tv.be",
article = document.querySelector('.js_fetdata'),
url = 'fetchWebsite.php',
part = '.thisevening'
runToAspire(url, part).then(content => {
if (content) {
const newContent = content.outerHTML.split('<span class="head">Films de la soirée</span>'),
newHtml = newContent[1].split('<span class="head">Séries de la soirée</span>')[0],
tempDiv = document.createElement('section')
tempDiv.innerHTML = newHtml
const newHtmlElement = tempDiv,
itemElements = newHtmlElement.querySelectorAll('.item'),
items = []
itemElements.forEach(itemElement => {
const item = {
title: itemElement.querySelector('.details a').textContent,
hour: itemElement.querySelector('.hour').textContent,
description: itemElement.querySelector('.details span').textContent,
link: itemElement.querySelector('.details a').href,
img: itemElement.querySelector('.evchaine img').src,
}
items.push(item)
})
console.log( items )
runHtml(items)
}
})