-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (42 loc) · 1.44 KB
/
script.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
52
// const HTML_REGEXP = /<(.*?)>/g;
const TAG_REGEXP = /<(\w*?)[ >]/g;
const CLASS_REGEXP = /class="(.*?)"/g;
const ID_REGEXP = /id="(.*?)"/g;
const EXCLUDED_TAGS = ["base", "head", "link", "meta", "style", "title", "canvas", "script", "noscript"];
let btn = document.querySelector("#extractor");
btn.addEventListener("click", update)
function parse(rawHtml, options) {
const result = [];
let stringOutput = "";
function extract(regex, o) {
[...rawHtml.matchAll(regex)].forEach(([, match]) => {
if (o?.class === true) match = `.${match}`;
if (o?.id === true) match = `#${match}`;
if (!EXCLUDED_TAGS.includes(match) && !result.includes(match)) {
result.push(match);
}
});
}
if (options.includes("tags")) extract(TAG_REGEXP);
if (options.includes("classes")) extract(CLASS_REGEXP, { class: true });
if (options.includes("ids")) extract(ID_REGEXP, { id: true });
result.forEach((elem) => {
stringOutput += `${elem} {
}
`;
});
return stringOutput;
}
function update() {
let outputBox = document.querySelector("#output");
let inputBox = document.querySelector("#input");
let options = [];
Array.from(
document.querySelectorAll('input[type="checkbox"]:checked')
).forEach((item) => {
options = [...options, item.getAttribute("id")];
});
if (options.length === 0) return alert("You must select an option!");
outputBox.value = "";
outputBox.value = parse(inputBox.value, options);
}