-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.html
More file actions
46 lines (39 loc) · 1.57 KB
/
Copy pathrandom.html
File metadata and controls
46 lines (39 loc) · 1.57 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
<!DOCTYPE html>
<html>
<head>
<title>Random Page Redirect</title>
</head>
<body>
<p>Picking a random page for you...</p>
<script>
async function redirectToRandom() {
try {
// Fetch your sitemap file (using the relative path)
const response = await fetch('sitemap.xml');
const text = await response.text();
// Parse the XML
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
// Get all <loc> tags (these contain your page URLs)
const locations = xmlDoc.getElementsByTagName("loc");
const urls = [];
for (let i = 0; i < locations.length; i++) {
urls.push(locations[i].textContent);
}
if (urls.length > 0) {
// Pick a random URL from the list
const randomURL = urls[Math.floor(Math.random() * urls.length)];
// Redirect the entire browser window
window.top.location.href = randomURL;
} else {
document.body.innerHTML = "No URLs found in sitemap.";
}
} catch (error) {
console.error("Error fetching sitemap:", error);
document.body.innerHTML = "Error loading pages. Please check the sitemap path.";
}
}
redirectToRandom();
</script>
</body>
</html>