-
Notifications
You must be signed in to change notification settings - Fork 0
/
Google Sitemap Test.js
96 lines (79 loc) · 3.23 KB
/
Google Sitemap Test.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
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
console.time("Function #1");
let resultsArr = [["addys"]];
const sitemapUrls = await fetchSitemapUrls();
for (let i = 0; i < sitemapUrls.length; i++) {
// for (let i = 0; i < 100; i++) {
const arr = await getAnchorElementsWithoutIds(sitemapUrls[i]);
if (arr.length > 1) {
resultsArr.push(arr);
}
}
console.log("COMPLETE!");
console.timeEnd("Function #1");
// console.log(resultsArr);
async function fetchSitemapUrls() {
try {
const siteMapUrl = "https://developers.google.com/maps/sitemap.xml";
const response = await fetch(siteMapUrl);
const textResponse = await response.text();
const doc = new DOMParser().parseFromString(textResponse, "text/html");
let arr = [];
const locs = doc.getElementsByTagName("loc");
for (let i = 0, max = locs.length; i < max; i++) {
// console.log(locs[i].textContent);
arr.push(locs[i].textContent);
}
return arr;
} catch (error) {
console.log(error);
return error;
}
}
// const urlText = "https://developers.google.com/maps/faq";
// const result = await getAnchorElementsWithoutIds(urlText);
async function getAnchorElementsWithoutIds(url) {
try {
const response = await fetch(url);
const textResponse = await response.text();
const doc = new DOMParser().parseFromString(textResponse, "text/html");
const anchors = doc.getElementsByTagName("a");
let arr = [];
arr.push(url);
for (let i = 0, max = anchors.length; i < max; i++) {
const href = anchors[i].getAttribute("href");
// Check if href
if (href) {
// Check if href is over 1 character
if (href.length > 1) {
// Check if href link is for same page reference
if (href.charAt(0) === "#") {
const str = href.substring(1);
// Check if href hasn't already been checked
if (!arr.includes(str)) {
// Check missing references to href
// Check for reference to Id
if (!doc.getElementById(str)) {
// Check for missing anchor tag name reference
if (doc.querySelector(`[name="${str}"]`)) {
if (
doc.querySelector(`[name="${str}"]`)
.tagName !== "A"
) {
// arr.push(str);
console.log(`${url},${str},"Missing_Name"`)
}
} else {
console.log(`${url},${str},"Missing_ID"`)
}
}
}
}
}
}
}
return arr;
} catch (error) {
console.log(error);
return error;
}
}