-
Notifications
You must be signed in to change notification settings - Fork 8
/
scraper.js
52 lines (42 loc) · 1.14 KB
/
scraper.js
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
function scrape_from_html(query, func) {
data = []
let scrape_els = document.querySelectorAll(query);
if (scrape_els.length == 0) {
return data;
}
for (let i = 0; i < scrape_els.length; i++) {
func(scrape_els[i], data);
}
return data;
}
function is_valid_data(data) {
return !(data == undefined || data.length == 0 || data.length == undefined)
}
function scrape_data_if_needed(data, query, func, done_func) {
if (is_valid_data(data)) {
done_func(data)
window.localStorage.setItem(name, JSON.stringify(data));
return true
}
data = scrape_from_html(query, func);
if (is_valid_data(data)) {
done_func(data)
window.localStorage.setItem(name, JSON.stringify(data));
return true
}
return false
}
function scrape(name, query, func, done_func, interval_time = 0) {
let data = JSON.parse(window.localStorage.getItem(name));
if (scrape_data_if_needed(data, query, func, done_func)) {
return
}
if (interval_time == 0) {
return
}
let interval = setInterval(() => {
if (scrape_data_if_needed(data, query, func, done_func)) {
clearInterval(interval)
}
}, interval_time);
}