-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
185 lines (163 loc) · 5.91 KB
/
script.js
File metadata and controls
185 lines (163 loc) · 5.91 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
184
185
// Run non-theme initialization after DOM is ready to avoid errors when elements are missing.
document.addEventListener('DOMContentLoaded', () => {
debugLog('DOMContentLoaded fired')
// Set up fallback src for images that declare a data-fallback attribute.
document.querySelectorAll('img[data-fallback]').forEach((img) => {
img.addEventListener('error', function () {
if (this.dataset.fallback && this.src !== this.dataset.fallback) {
this.src = this.dataset.fallback
}
}, { once: true })
})
// Render links from links.json
debugLog('Calling renderLinks')
renderLinks('#linksContainer', 'links.json')
// Render footer from includes/footer.html
debugLog('Calling renderFooter')
renderFooter('#siteFooter', 'includes/footer.html')
})
/**
* Fetches a JSON file containing an array of links and renders them into the page.
* Each link object may have: { title, url, subtitle, target }
*/
function renderLinks (containerSelector, jsonPath) {
const container = document.querySelector(containerSelector)
if (!container) return
// Show a visible loading message so it's obvious the script ran
container.innerHTML = '<p class="links-loading">Loading links</p>'
console.log('renderLinks: fetching', jsonPath)
debugLog(`renderLinks: fetching ${jsonPath}`)
fetch(jsonPath)
.then((res) => {
if (!res.ok) throw new Error('Failed to load links.json')
return res.json()
})
.then((links) => {
// Clear container
container.innerHTML = ''
console.log('renderLinks: got', links.length, 'links')
debugLog(`renderLinks: got ${links.length} links`)
links.forEach((link) => {
const anchor = document.createElement('a')
anchor.className = 'link-button'
anchor.href = link.url || '#'
if (link.target) anchor.target = link.target
const span = document.createElement('span')
span.className = 'link-title'
span.textContent = link.title || link.url || 'Link'
anchor.appendChild(span)
// Optional subtitle
if (link.subtitle) {
const subtitle = document.createElement('div')
subtitle.className = 'link-subtitle'
subtitle.textContent = link.subtitle
anchor.appendChild(subtitle)
}
container.appendChild(anchor)
})
})
.catch((err) => {
console.error('Error rendering links:', err)
// Fallback: show an error message in the UI so it's visible
const msg = err?.message ?? 'unknown error'
container.innerHTML = `<p class="links-error">Could not load links: ${msg}</p>`
debugLog(`renderLinks error: ${msg}`)
})
}
/**
* Fetches an HTML file and injects its content into the target element.
* Falls back to the original static footer markup already in the page if the fetch fails.
*/
function renderFooter (containerSelector, htmlPath) {
const container = document.querySelector(containerSelector)
if (!container) return
// Capture whatever static footer HTML is already present so we can restore it on failure
const originalHTML = container.innerHTML
debugLog(`renderFooter: fetching ${htmlPath}`)
fetch(htmlPath)
.then((res) => {
if (!res.ok) throw new Error(`Failed to load ${htmlPath}`)
return res.text()
})
.then((html) => {
container.innerHTML = html
debugLog('renderFooter: footer loaded')
})
.catch((err) => {
const msg = err?.message ?? 'unknown error'
console.error('Error rendering footer:', err)
debugLog(`renderFooter error: ${msg}`)
// Fallback: restore the original static footer markup if available
if (originalHTML && originalHTML.trim() !== '') {
container.innerHTML = originalHTML
} else {
// Last-resort minimal attribution if no original markup existed
container.textContent = '© 2026 SillyLittleTech.'
}
})
}
let _debugEnabled = false
let _debugBuffer = []
/** Returns the debug overlay box, creating it if it doesn't exist yet. */
function getOrCreateDebugBox () {
let box = document.getElementById('scriptDebugBox')
if (!box) {
box = document.createElement('div')
box.id = 'scriptDebugBox'
box.style.position = 'fixed'
box.style.left = '12px'
box.style.bottom = '12px'
box.style.maxWidth = '420px'
box.style.background = 'rgba(0,0,0,0.6)'
box.style.color = 'white'
box.style.fontSize = '12px'
box.style.padding = '8px'
box.style.borderRadius = '8px'
box.style.zIndex = 99999
box.style.whiteSpace = 'pre-wrap'
box.style.pointerEvents = 'none'
document.body.appendChild(box)
}
return box
}
function debugLog (message) {
// always log to console for developers
console.log(message)
const time = new Date().toLocaleTimeString()
const entry = `${time} — ${message}`
if (!_debugEnabled) {
// buffer until unlocked
_debugBuffer.push(entry)
// keep buffer reasonably small
if (_debugBuffer.length > 200) _debugBuffer.shift()
return
}
// when enabled, ensure box exists and prepend
const box = getOrCreateDebugBox()
box.textContent = `${entry}\n${box.textContent}`
}
// Unlock debug mode by clicking the avatar 5 times quickly
function setupAvatarDebugUnlock () {
const avatar = document.querySelector('.avatar')
if (!avatar) return
let clicks = 0
let timer = null
avatar.addEventListener('click', () => {
clicks += 1
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
clicks = 0
}, 3000) // 3s window to complete sequence
if (clicks >= 5) {
clicks = 0
_debugEnabled = true
// flush buffer into the visible box
const box = getOrCreateDebugBox()
box.textContent = `${_debugBuffer.toReversed().join('\n')}\n${box.textContent}`
_debugBuffer = []
debugLog('Debug mode unlocked (avatar clicked 5x)')
}
})
}
// initialize avatar unlock after DOM is ready
document.addEventListener('DOMContentLoaded', () => setupAvatarDebugUnlock())