-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (152 loc) · 6.51 KB
/
script.js
File metadata and controls
183 lines (152 loc) · 6.51 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
document.addEventListener("DOMContentLoaded", () => {
const sections = document.querySelectorAll("section");
function showSection(hash) {
sections.forEach(sec => {
if (hash === "#" + sec.id || (hash === "" && sec.id === "home")) {
sec.style.display = "block";
sec.style.opacity = "0";
setTimeout(() => {
sec.style.transition = "opacity 0.3s ease";
sec.style.opacity = "1";
}, 10);
} else {
sec.style.display = "none";
}
});
updateActiveNav(hash);
}
showSection(window.location.hash);
window.addEventListener("hashchange", () => {
showSection(window.location.hash);
});
const projectTiles = document.querySelectorAll('.project-tile');
projectTiles.forEach(tile => {
tile.addEventListener('click', () => {
tile.style.transform = 'scale(0.95)';
setTimeout(() => {
tile.style.transform = 'translateY(-3px)';
}, 100);
});
});
initSpotifyWidget();
initWritingProgress();
});
function updateActiveNav(hash) {
document.querySelectorAll('.navbar a').forEach(link => {
link.classList.remove('active');
});
const currentHash = hash || '#home';
const activeLink = document.querySelector(`.navbar a[href="${currentHash}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
}
function toggleProject(projectId) {
const content = document.getElementById(projectId + '-content');
content.classList.toggle('active');
}
function showTab(event, projectId, tabName) {
const tabContents = document.querySelectorAll(`#${projectId}-content .tab-content`);
tabContents.forEach(content => content.classList.remove('active'));
const tabButtons = document.querySelectorAll(`#${projectId}-content .tab-button`);
tabButtons.forEach(button => button.classList.remove('active'));
document.getElementById(`${projectId}-${tabName}`).classList.add('active');
event.target.classList.add('active');
}
function initSpotifyWidget() {
const setDefaultState = () => {
const albumImg = document.getElementById("spotify-album");
const trackText = document.getElementById("spotify-track");
const artistText = document.getElementById("spotify-artist");
const progressBar = document.getElementById("spotify-progress");
if (trackText) trackText.textContent = "Nothing Playing";
if (artistText) artistText.textContent = "";
if (albumImg) albumImg.src = "unnamed.jpg";
if (progressBar) progressBar.style.width = "0%";
};
setDefaultState();
async function updateSpotify() {
try {
const res = await fetch("https://api.joelalexander.dev/spotify/current");
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
const data = await res.json();
const albumImg = document.getElementById("spotify-album");
const trackText = document.getElementById("spotify-track");
const artistText = document.getElementById("spotify-artist");
const progressBar = document.getElementById("spotify-progress");
if (data && data.track) {
if (albumImg) {
albumImg.src = data.albumArt || "unnamed.jpg";
albumImg.alt = `${data.album} by ${data.artist}`;
}
if (trackText) {
trackText.textContent = data.track;
}
if (artistText) {
artistText.textContent = data.artist;
}
if (progressBar) {
const progressPercent = (data.progress_ms / data.duration_ms) * 100;
progressBar.style.width = progressPercent + "%";
}
} else {
setDefaultState();
}
} catch (err) {
setDefaultState();
}
}
updateSpotify();
setInterval(updateSpotify, 10000);
}
function initWritingProgress() {
async function updateWritingProgress() {
try {
const res = await fetch("https://api.joelalexander.dev/docs/info");
const data = await res.json();
if (data) {
const wordCountElement = document.querySelector('.word-count .count');
if (wordCountElement) {
wordCountElement.textContent = data.wordCount.toLocaleString();
}
const projectStatsWordCount = document.querySelector('.project-stats span');
if (projectStatsWordCount) {
projectStatsWordCount.innerHTML = `📖 ${data.wordCount.toLocaleString()} words`;
}
const projectTitleWordCount = document.querySelector('.project-header h2');
if (projectTitleWordCount && projectTitleWordCount.textContent.includes('30,463')) {
projectTitleWordCount.innerHTML = projectTitleWordCount.innerHTML.replace('30,463', data.wordCount.toLocaleString());
}
const overviewWordCount = document.querySelector('#bloodstained-honor-overview p:nth-of-type(2)');
if (overviewWordCount && overviewWordCount.innerHTML.includes('30,463')) {
const progressPercent = Math.round((data.wordCount / 100000) * 100);
overviewWordCount.innerHTML = `<strong>Progress:</strong> ${progressPercent}% complete (${data.wordCount.toLocaleString()} / 100,000 words)`;
}
const allOverviewParagraphs = document.querySelectorAll('#bloodstained-honor-overview p');
allOverviewParagraphs.forEach(p => {
if (p.innerHTML.includes('Progress:')) {
const progressPercent = Math.round((data.wordCount / 100000) * 100);
p.innerHTML = `<strong>Progress:</strong> ${progressPercent}% complete (${data.wordCount.toLocaleString()} / 100,000 words)`;
}
if (p.innerHTML.includes('Current Chapter:')) {
p.innerHTML = `<strong>Current Chapter:</strong> Chapter ${data.chapterNum} - "${data.title}"`;
}
if (p.innerHTML.includes('Recent Milestone:')) {
p.innerHTML = `<strong>Recent Milestone:</strong> Completed Chapter ${data.chapterNum - 1}, working on Chapter ${data.chapterWord} - "${data.title}".`;
}
});
const progressBar = document.querySelector('#bloodstained-honor-overview .progress-fill');
if (progressBar) {
const progressPercent = Math.round((data.wordCount / 100000) * 100);
progressBar.style.width = progressPercent + "%";
}
}
} catch (err) {
console.error("Writing progress fetch error:", err);
}
}
updateWritingProgress();
setInterval(updateWritingProgress, 30000);
}