-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
108 lines (92 loc) · 2.51 KB
/
util.js
File metadata and controls
108 lines (92 loc) · 2.51 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
96
97
98
99
100
101
102
103
104
105
106
107
108
import { JSDOM } from "jsdom";
import axios from "axios";
export function removeFirstElementIfExistent(element, div) {
let elem = div.querySelector(element);
if (elem) {
elem.remove();
}
}
export function removeAllElementsIfExistent(element, div) {
let elements = div.querySelectorAll(element);
if (elements) {
for (let i = 0; i < elements.length; i++) {
elements[i].remove();
}
}
}
export function removeAllElementsWithText(element, text, div) {
let elems = div.querySelectorAll(element);
Array.from(elems).filter((elem) =>
elem.textContent.includes(text) ? elem.remove() : elem,
);
}
export function addCommas(element, div) {
const elements = div.querySelectorAll(element);
if (elements) {
for (let i = 0; i < elements.length; i++) {
if (i !== elements.length - 1) {
elements[i].innerHTML += ", ";
}
}
}
}
export function changeTagToParagraph(element, div) {
let currentElement = div.querySelector(element);
if (currentElement) {
let newElement = div.createElement("p");
newElement.innerHTML = currentElement.innerHTML;
currentElement.parentNode.replaceChild(newElement, currentElement);
}
}
export function createDivWithContent(content) {
const { window } = new JSDOM("");
const document = window.document;
const div = document.createElement("div");
div.innerHTML = content;
return div;
}
export function deleteTimeouts() {
let id = window.setTimeout(function () {}, 0);
while (id--) {
window.clearTimeout(id);
}
}
export function deleteIntervals() {
var id = window.setInterval(function () {}, 0);
while (id--) {
window.clearInterval(id);
}
}
export async function getSourceAsDOM(url) {
const response = await fetch(url);
const html = await response.text();
// const parser = new DOMParser();
//const clean = DOMPurify.sanitize(html);
const dom = new JSDOM(html);
return dom.window.document;
// return parser.parseFromString(html, "text/html");
}
export async function getHTMLContent(url) {
axios
.get(url)
.then(function (response) {
console.log(response);
return response;
})
.catch(function (error) {
console.log(error);
});
}
export function getEmptyDocument() {
const { window } = new JSDOM("");
return window.document;
}
export function createEmptyDiv() {
const { window } = new JSDOM("");
const document = window.document;
return document.createElement("div");
}
export function text2DOM(text) {
const { window } = new JSDOM(text);
return window.document;
}