-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.js
More file actions
103 lines (89 loc) · 2.64 KB
/
global.js
File metadata and controls
103 lines (89 loc) · 2.64 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
console.log("IT’S ALIVE!");
function $$(selector, context = document) {
return Array.from(context.querySelectorAll(selector));
}
let nav = document.createElement("nav");
document.body.prepend(nav);
let pages = [
{ url: "", title: "Home" },
{ url: "projects/", title: "Projects" },
{ url: "contact/", title: "Contact" },
{ url: "https://github.com/seashello", title: "Profile" },
{ url: "resume/", title: "Resume" },
{ url: "meta/", title: "Meta" },
];
const ARE_WE_HOME = document.documentElement.classList.contains("home");
for (let p of pages) {
let url = p.url;
if (!ARE_WE_HOME && !url.startsWith("http")) {
url = "../" + url;
}
let title = p.title;
let a = document.createElement("a");
a.href = url;
a.textContent = title;
if (a.host === location.host && a.pathname === location.pathname) {
a.classList.add("current");
}
if (a.host !== location.host) {
a.target = "_blank";
}
nav.append(a);
}
document.body.insertAdjacentHTML(
"afterbegin",
`
<label class="color-scheme">
Theme:
<select>
<option value="light dark">Automatic</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>`
);
let select = document.querySelector("select");
select.addEventListener("input", function (event) {
console.log("color scheme changed to", event.target.value);
document.documentElement.style.setProperty(
"color-scheme",
event.target.value
);
localStorage.colorScheme = event.target.value;
});
if ("colorScheme" in localStorage) {
document.documentElement.style.setProperty(
"color-scheme",
localStorage.colorScheme
);
select.value = localStorage.colorScheme;
}
export async function fetchJSON(url) {
try {
// Fetch the JSON file from the given URL
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch projects: ${response.statusText}`);
}
console.log(response);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching or parsing JSON data:", error);
}
}
export function renderProjects(project, containerElement, headingLevel = "h2") {
containerElement.innerHTML = "";
project.forEach((project) => {
const article = document.createElement("article");
article.innerHTML = `
<${headingLevel}>${project.title}</${headingLevel}>
<img src="${project.image}" alt="${project.title}">
<p>${project.year}: ${project.description}</p>
`;
containerElement.appendChild(article);
});
}
export async function fetchGitHubData(username) {
return fetchJSON(`https://api.github.com/users/${username}`);
}