Skip to content

Commit 03e50f0

Browse files
committed
- remove complex HTML rewriting in favor of targeted fetch interception
- refactor `timeTravel()` to preserve original page scripts - add targeted JSON interceptor for `data/games.json` and `data/tools.json` - fix issues with embedded scripts not executing properly - improve code formatting with consistent indentation
1 parent f96f682 commit 03e50f0

File tree

1 file changed

+64
-91
lines changed

1 file changed

+64
-91
lines changed

timeline.js

Lines changed: 64 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -252,106 +252,79 @@ function findNearestStep(position) {
252252
}, null);
253253
}
254254

255-
function rewriteAssetUrls(html, hash, currentPath) {
256-
const parser = new DOMParser();
257-
const doc = parser.parseFromString(html, 'text/html');
258-
const basePath = currentPath.split('/').slice(0, -1).join('/');
259-
const baseUrl = `https://raw.githubusercontent.com/TobiasMue91/tobiasmue91.github.io/${hash}/`;
260-
const baseTag = document.createElement('base');
261-
baseTag.href = basePath ? `${baseUrl}${basePath}/` : baseUrl;
262-
doc.head.prepend(baseTag);
255+
function timeTravel(hash, path = 'index.html') {
256+
if (hash === currentHash && path === 'index.html') return;
257+
showLoading();
263258

264-
// Remove problematic scripts that shouldn't be loaded in time travel mode
265-
const scriptsToRemove = [
266-
'timeline.js',
267-
'sw.js',
268-
'chatbot.js',
269-
'gc.zgo.at/count.js'
270-
];
259+
const githubUrl = `https://raw.githubusercontent.com/TobiasMue91/tobiasmue91.github.io/${hash}/${path}`;
260+
fetch(githubUrl)
261+
.then(response => {
262+
if (!response.ok) {
263+
throw new Error(`Failed to fetch content: ${response.status}`);
264+
}
265+
return response.text();
266+
})
267+
.then(html => {
268+
// Skip HTML rewriting entirely and just inject the fetch interceptor directly
269+
currentHash = hash;
271270

272-
// Remove script tags with matching src attributes
273-
doc.querySelectorAll('script').forEach(script => {
274-
const src = script.getAttribute('src');
275-
if (src && scriptsToRemove.some(badScript => src.includes(badScript))) {
276-
script.remove();
277-
}
278-
});
271+
if (contentFrame) {
272+
contentFrame.remove();
273+
}
279274

280-
// Also remove inline scripts related to service worker registration
281-
doc.querySelectorAll('script:not([src])').forEach(script => {
282-
if (script.textContent.includes('serviceWorker.register') ||
283-
script.textContent.includes('goatcounter')) {
284-
script.remove();
285-
}
286-
});
275+
contentFrame = document.createElement('iframe');
276+
contentFrame.id = 'historical-content';
277+
contentFrame.style.position = 'fixed';
278+
contentFrame.style.top = '0';
279+
contentFrame.style.left = '0';
280+
contentFrame.style.width = '100%';
281+
contentFrame.style.height = '100%';
282+
contentFrame.style.border = 'none';
283+
contentFrame.style.zIndex = '9998';
284+
contentFrame.style.background = 'white';
285+
document.body.appendChild(contentFrame);
287286

288-
// Continue with the existing URL rewriting logic
289-
doc.querySelectorAll('link[rel="stylesheet"]').forEach(link => {
290-
const href = link.getAttribute('href');
291-
if (href && !href.startsWith('http') && !href.startsWith('//')) {
292-
link.setAttribute('href', new URL(href, baseUrl).href);
293-
}
294-
});
287+
contentFrame.style.display = 'block';
288+
document.querySelector('#timeline-exit').style.display = 'block';
295289

296-
doc.querySelectorAll('img').forEach(img => {
297-
const src = img.getAttribute('src');
298-
if (src && !src.startsWith('http') && !src.startsWith('//')) {
299-
img.setAttribute('src', new URL(src, baseUrl).href);
300-
}
301-
});
290+
const frameDoc = contentFrame.contentDocument || contentFrame.contentWindow.document;
291+
frameDoc.open();
292+
frameDoc.write(html);
302293

303-
// Only rewrite URLs for scripts we're keeping
304-
doc.querySelectorAll('script').forEach(script => {
305-
const src = script.getAttribute('src');
306-
if (src && !src.startsWith('http') && !src.startsWith('//')) {
307-
script.setAttribute('src', new URL(src, baseUrl).href);
308-
}
309-
});
294+
// Inject the fetch interceptor script without modifying anything else
295+
const script = frameDoc.createElement('script');
296+
script.textContent = `
297+
// Intercept fetch for JSON files only
298+
const originalFetch = window.fetch;
299+
window.fetch = function(url, options) {
300+
if (typeof url === 'string') {
301+
if (url.includes('data/games.json') || url.includes('data/tools.json')) {
302+
const fileName = url.split('/').pop();
303+
const absoluteUrl = "https://raw.githubusercontent.com/TobiasMue91/tobiasmue91.github.io/${hash}/data/" + fileName;
304+
console.log("Rewriting fetch URL:", url, "to", absoluteUrl);
305+
return originalFetch(absoluteUrl, options);
306+
}
307+
}
308+
return originalFetch(url, options);
309+
};
310+
console.log("Fetch interceptor installed for JSON files");
311+
`;
312+
frameDoc.head.appendChild(script);
310313

311-
return new XMLSerializer().serializeToString(doc);
312-
}
314+
// Close the document after adding the script
315+
frameDoc.close();
313316

314-
function timeTravel(hash, path = 'index.html') {
315-
if (hash === currentHash && path === 'index.html') return;
316-
showLoading();
317+
// Set up link interception
318+
setTimeout(() => interceptLinks(frameDoc, hash), 300);
317319

318-
const githubUrl = `https://raw.githubusercontent.com/TobiasMue91/tobiasmue91.github.io/${hash}/${path}`;
319-
fetch(githubUrl).then(response => {
320-
if (!response.ok) {
321-
throw new Error(`Failed to fetch content: ${response.status}`);
322-
}
323-
return response.text();
324-
}).then(html => {
325-
html = rewriteAssetUrls(html, hash, path);
326-
currentHash = hash;
327-
if (contentFrame) {
328-
contentFrame.remove();
329-
}
330-
contentFrame = document.createElement('iframe');
331-
contentFrame.id = 'historical-content';
332-
contentFrame.style.position = 'fixed';
333-
contentFrame.style.top = '0';
334-
contentFrame.style.left = '0';
335-
contentFrame.style.width = '100%';
336-
contentFrame.style.height = '100%';
337-
contentFrame.style.border = 'none';
338-
contentFrame.style.zIndex = '9998';
339-
contentFrame.style.background = 'white';
340-
document.body.appendChild(contentFrame);
341-
contentFrame.style.display = 'block';
342-
document.querySelector('#timeline-exit').style.display = 'block';
343-
const frameDoc = contentFrame.contentDocument || contentFrame.contentWindow.document;
344-
frameDoc.open();
345-
frameDoc.write(html);
346-
frameDoc.close();
347-
setTimeout(() => interceptLinks(frameDoc, hash), 300);
348-
document.querySelector('#timeline-container').scrollLeft = timelineScrollPosition;
349-
hideLoading();
350-
}).catch(error => {
351-
console.error("Time travel failed:", error);
352-
showError(`Failed to load content: ${error.message}`);
353-
hideLoading();
354-
});
320+
document.querySelector('#timeline-container').scrollLeft = timelineScrollPosition;
321+
hideLoading();
322+
})
323+
.catch(error => {
324+
console.error("Time travel failed:", error);
325+
showError(`Failed to load content: ${error.message}`);
326+
hideLoading();
327+
});
355328
}
356329

357330
function interceptLinks(doc, hash) {

0 commit comments

Comments
 (0)