-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (145 loc) · 5.47 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
window.onload = function () {
function updateSiteSelector(siteOne, siteOneName, siteTwo, siteTwoName) {
document.getElementById("siteSelector").innerHTML = `
<option value="https://${siteOne}">${siteOneName}</option>
<option value="https://${siteTwo}">${siteTwoName}</option>
`;
}
function loadStoredLinks() {
const storedTestSite =
localStorage.getItem("testSite") || "odi-test.opensourcelearning.co.uk";
const storedLiveSite =
localStorage.getItem("liveSite") || "moodle.learndata.info";
const storedTestSiteName =
localStorage.getItem("testSiteName") || "Test Site";
const storedLiveSiteName =
localStorage.getItem("liveSiteName") || "Live Site";
updateSiteSelector(
storedTestSite,
storedTestSiteName,
storedLiveSite,
storedLiveSiteName
);
// Load the stored values into the input fields
document.getElementById("testSite").value = storedTestSite;
document.getElementById("liveSite").value = storedLiveSite;
document.getElementById("testSiteName").value = storedTestSiteName;
document.getElementById("liveSiteName").value = storedLiveSiteName;
}
document.getElementById("submit").addEventListener("click", function () {
const siteOne = document.getElementById("testSite").value;
const siteTwo = document.getElementById("liveSite").value;
const siteOneName =
document.getElementById("testSiteName").value || "Test Site";
const siteTwoName =
document.getElementById("liveSiteName").value || "Live Site";
if (siteOne && siteTwo) {
localStorage.setItem("testSite", siteOne);
localStorage.setItem("liveSite", siteTwo);
localStorage.setItem("testSiteName", siteOneName);
localStorage.setItem("liveSiteName", siteTwoName);
updateSiteSelector(siteOne, siteOneName, siteTwo, siteTwoName);
} else {
alert("Please enter valid URLs and names.");
}
});
loadStoredLinks();
function clearLocalStorage() {
localStorage.clear();
// Reset input fields and dropdowns if needed
document.getElementById("testSite").value = "";
document.getElementById("liveSite").value = "";
document.getElementById("testSiteName").value = "";
document.getElementById("liveSiteName").value = "";
updateSiteSelector(
"odi-test.opensourcelearning.co.uk",
"Test Site",
"moodle.learndata.info",
"Live Site"
);
}
document
.getElementById("clearStorage")
.addEventListener("click", function () {
if (confirm("Are you sure you want to clear all stored data?")) {
clearLocalStorage();
}
});
const originalHrefs = {};
function updateLinks() {
const code = document.getElementById("userInput").value;
const site = document.getElementById("siteSelector").value;
const links = document.querySelectorAll(".link-list a");
links.forEach((link, index) => {
if (!originalHrefs[index]) {
originalHrefs[index] = link.getAttribute("href");
}
let href = originalHrefs[index];
if (href.includes("[coursecode]")) {
href = href.replace("[coursecode]", code);
}
link.setAttribute("href", site + href);
});
}
document.getElementById("userInput").addEventListener("change", updateLinks);
document
.getElementById("siteSelector")
.addEventListener("change", updateLinks);
};
document.querySelectorAll(".addLinkButton").forEach((button) => {
button.addEventListener("click", function () {
const section = this.getAttribute("data-section");
const inputContainer = document.getElementById(section + "Inputs");
if (inputContainer.children.length === 0) {
createInputFields(inputContainer, section);
} else {
inputContainer.style.display = "block";
}
});
});
function createInputFields(container, section) {
const linkNameInput = createInput("text", "newLinkName", "Enter link name");
const linkURLInput = createInput("text", "newLinkURL", "Enter link URL");
const submitButton = document.createElement("button");
submitButton.textContent = "Submit";
submitButton.addEventListener("click", function () {
addLinkToList(section);
});
container.appendChild(linkNameInput);
container.appendChild(linkURLInput);
container.appendChild(submitButton);
container.style.display = "block";
}
function createInput(type, id, placeholder) {
const input = document.createElement("input");
input.setAttribute("type", type);
input.setAttribute("id", id);
input.setAttribute("placeholder", placeholder);
return input;
}
function addLinkToList(section) {
const linkName = document.getElementById("newLinkName").value;
const linkURL = document.getElementById("newLinkURL").value;
if (linkName && linkURL) {
const newListItem = document.createElement("li");
const newLink = document.createElement("a");
newLink.setAttribute("href", linkURL);
newLink.setAttribute("target", "_blank");
newLink.textContent = linkName;
const targetList = document.getElementById(section);
if (targetList) {
targetList.appendChild(newListItem);
newListItem.appendChild(newLink);
// Clear and hide the input fields
const inputContainer = document.getElementById(section + "Inputs");
if (inputContainer) {
inputContainer.style.display = "none";
inputContainer.innerHTML = "";
}
} else {
console.error("Target list not found for section:", section);
}
} else {
alert("Please enter both a name and a URL for the link.");
}
}