const BOARD_URL https vk com board37950548 function getDom url return

 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
const BOARD_URL = "https://vk.com/board37950548";
function getDom(url) {
return Promise
.race([
new Promise((res, rej) => setTimeout(rej, 5000)),
fetch(url)
])
.then(response =>
response.arrayBuffer()
.then(responseArrayBuffer => {
const
dataView = new DataView(responseArrayBuffer),
decoder = new TextDecoder('windows-1251');
return decoder.decode(dataView)
})
)
.then(textDom => new DOMParser().parseFromString(textDom, 'text/html'));
}
function defineStartValue(url) {
return new Promise((res, rej) => {
function request() {
return getDom(url)
.then(DOM => res(DOM.getElementById('blst_cont').childNodes.length))
}
request().catch(() => request());
})
}
function loop(total) {
let currentTotal,
answer;
const response = getDom(BOARD_URL)
.then((DOM) => {
currentTotal = DOM.getElementById('blst_cont').childNodes.length;
const
text = DOM.querySelector('.blst_title').innerText,
link = DOM.querySelector('.blst_title').getAttribute('href');
answer = total < currentTotal && confirm(`Доступно новое задание:\n ${text} \nПерейти к обсуждению?`);
if (answer) {
window.open(link);
}
});
Promise.all([response, new Promise(res => setTimeout(res, 5000))])
.then(() => loop(currentTotal))
.catch((e) => {console.log(e); loop(currentTotal)});
}
defineStartValue(BOARD_URL)
.then(loop);