dwd/public/path-fix.js

46 lines
1.6 KiB
JavaScript

/**
* Path fix script for static exports
* This script helps to fix absolute path references in the static HTML
* when served from a file system or simple static server
*/
(function() {
// Only run this script when the window has loaded
window.addEventListener('DOMContentLoaded', function() {
// Fix for stylesheet links
document.querySelectorAll('link[rel="stylesheet"]').forEach(function(link) {
if (link.href.startsWith('http')) return; // Skip absolute URLs
if (link.href.startsWith('/')) {
link.href = './' + link.href.slice(1);
}
});
// Fix for script tags
document.querySelectorAll('script[src]').forEach(function(script) {
if (script.src.startsWith('http')) return; // Skip absolute URLs
if (script.src.startsWith('/')) {
script.src = './' + script.src.slice(1);
}
});
// Fix for images
document.querySelectorAll('img[src]').forEach(function(img) {
if (img.src.startsWith('http')) return; // Skip absolute URLs
if (img.src.startsWith('/')) {
img.src = './' + img.src.slice(1);
}
});
// Initialize the mobile menu toggle
const toggleButton = document.querySelector('[aria-haspopup="dialog"]');
const sheet = document.querySelector('[data-state="closed"]');
if (toggleButton && sheet) {
toggleButton.addEventListener('click', function() {
const isOpen = sheet.getAttribute('data-state') === 'open';
sheet.setAttribute('data-state', isOpen ? 'closed' : 'open');
toggleButton.setAttribute('aria-expanded', !isOpen);
});
}
});
})();