-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (80 loc) · 3.52 KB
/
app.js
File metadata and controls
95 lines (80 loc) · 3.52 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
85
86
87
88
89
90
91
92
93
94
95
function app() {
// Waits for DOM load
window.addEventListener('DOMContentLoaded', () => {
// Listens for click event
document.addEventListener('click', (event) => {
function scrape() {
console.log('scrape working')
const axios = require('axios').default;
const cheerio = require("cheerio");
// List to store array of links
const links = [];
// Fetches the given link
const fetchLinks = async () => {
console.log('fetch working')
console.log(site, 'get url success');
try {
const response = await
// Looks for website
axios.get(site);
const html = response.data;
// Stores html from page
const $ = cheerio.load(html);
// Looks for anchor tags
$('a').each((_idx, value) => {
// Pulls href value
let url = $(value).attr('href');
// Pushes value to 'links' list
links.push(url);
});
return links;
}
// Throws error if unsuccessful
catch (error) {
throw error;
}
};
fetchLinks().then((links) => {
// Displays links to user
const listContainer = document.getElementById('list_container');
// Reveals container
listContainer.style.display = 'flex';
console.log('linklist', links);
// Loops through links list and adds a anchor list item to each site
for (var i in links) {
console.log('i', links[i]);
document.getElementById('link_list').innerHTML += `
<li>
<a href="${links[i]}">
${links[i]}
<a>
</li>
`;
}
// Converts links list to JSON
let json_arr = {};
json_arr[site] = links;
let json_string = JSON.stringify(json_arr);
// Prints JSON
let json_parse = JSON.parse(json_string);
console.log('parse', json_parse);
});
}
// Grabs the value from input
let site = document.getElementById('url').value;
// When above click event triggers, the elemnent is stored in a list
let element = []
element.push(event.target);
//if the element clicked was the button, the scrape function is triggered
if (element.includes(document.querySelectorAll('#scrape')[0])) {
console.log('button working');
return scrape();
}
// If the click event is triggered by anything not the button, return
else {
return
}
});
});
}
return app();