forked from TheDressedMoleRat/webring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
91 lines (73 loc) · 3.25 KB
/
script.js
File metadata and controls
91 lines (73 loc) · 3.25 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
let members_names = [];
let members_links = [];
let page_loaded = false;
const offsets = { "next": 1, "previous": -1, "skipnext": 2, "skipprevious": -2 }
function fix_url(url) {
if (url.startsWith('http://') || url.startsWith('https://')) {
return url;
} else {
return 'https://' + url;
}
}
function display_members(fetched_text) {
let list_element = document.getElementById("members_list");
let list_html = "";
let member_count = 0;
for (const line of fetched_text.split("\n")) {
if (line.trim() == "" || line.startsWith("//")) {
continue;
}
const colon_index = line.indexOf(":");
const name = line.slice(0, colon_index).trim();
const website = line.slice(colon_index + 1).trim();
list_html += `<li><a href=${fix_url(website)}>${name}</a></li>`;
members_names.push(name);
members_links.push(fix_url(website));
member_count++;
}
list_element.innerHTML = list_html;
let count_element = document.getElementById("members_count");
count_element.textContent = member_count;
}
function copy(text_to_copy) {
if (text_to_copy == "nav") { text_to_copy = `<p id="soweliring" style="display: flex; justify-content: space-between; width: 16em;"><a href="https://thedressedmolerat.github.io/webring?name=NAME&to=skipprevious">⇇</a> <a href="https://thedressedmolerat.github.io/webring?name=NAME&to=previous"><</a> <a href="https://thedressedmolerat.github.io/webring?to=random">⇅</a> <a href="https://thedressedmolerat.github.io/webring">soweli ring</a> <a href="https://thedressedmolerat.github.io/webring?name=NAME&to=next">></a> <a href="https://thedressedmolerat.github.io/webring?name=NAME&to=skipnext">⇉</a></p>`; }
let name_value = document.getElementById("user_name").value;
let updated = text_to_copy.replaceAll("NAME", encodeURIComponent(name_value));
navigator.clipboard.writeText(updated);
}
// ########################################################################## //
// wow promise chaining is wild
fetch('EVERYONE.txt')
.then(fetch_response => fetch_response.text())
.then(display_members)
.then(() => {
// redirect
const url_parameters = new URLSearchParams(window.location.search);
let url_name = url_parameters.get("name");
let url_to = url_parameters.get("to");
let target_url = "";
if (url_to == "random") {
target_url = members_links[Math.floor(Math.random()*members_links.length)];
} else if (url_to in offsets) {
let current_index = 0;
// neither members_names or url_name are url encoded
if (members_names.includes(url_name)) {
current_index = members_names.indexOf(url_name);
let target_index = current_index + offsets[url_to];
target_index = (target_index + members_names.length) % members_names.length;
target_url = members_links[target_index];
} else {
(url_name == "NAME" ) ? alert(`Replace "NAME" in the URL with your name in the ring!`)
: alert(`"${url_name}" does not exist in the members list!`);
}
} else if (url_to != null) {
alert(`${url_to} isn't a valid to-value. Valid to-values are: ${Object.keys(offsets).join(", ")}`);
}
if (target_url) {
window.location.replace(target_url);
} else {
document.getElementById("container").style.display = "block";
document.getElementById("redirecting").style.display = "none";
document.body.style.backgroundColor = "#32286f";
}
})