Files
nextcloud/php/public/toggle-dark-mode.js
copilot-swe-agent[bot] 3d322e2fe2 Move security headers from Caddyfiles to PHP middleware
- Add SecurityHeadersMiddleware that sets Content-Security-Policy,
  X-Content-Type-Options, X-Frame-Options, X-Permitted-Cross-Domain-Policies,
  X-DNS-Prefetch-Control, Referrer-Policy, and X-Robots-Tag on all responses
- Register SecurityHeadersMiddleware in index.php
- Add click-handlers.js for CSP-compliant event handling (data-confirm,
  data-stop-event-propagation)
- Update toggle-dark-mode.js to attach click handler via addEventListener
- Remove inline onclick from theme toggle button in layout.twig
- Replace all inline onclick with data-confirm in containers.twig,
  community-containers.twig, and optional-containers.twig

Co-authored-by: szaimen <42591237+szaimen@users.noreply.github.com>
Agent-Logs-Url: https://github.com/nextcloud/all-in-one/sessions/d87889ba-d2ad-4d76-b257-2afd725dac28
2026-03-25 11:39:40 +00:00

39 lines
1.4 KiB
JavaScript

// Function to toggle theme
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = (currentTheme === 'dark') ? '' : 'dark'; // Toggle between no theme and dark theme
setThemeToDOM(newTheme);
localStorage.setItem('theme', newTheme);
// Change the icon based on the current theme
setThemeIcon(newTheme);
}
function setThemeToDOM(value) {
// Set the theme to the root document and all possible iframe documents (so they can adapt their styling, too).
const documents = [document, Array.from(document.querySelectorAll('iframe')).map((iframe) => iframe.contentDocument)].flat()
documents.forEach((doc) => doc.documentElement.setAttribute('data-theme', value));
}
function getSavedTheme() {
return localStorage.getItem('theme') ?? '';
}
// Function to apply theme-icon update
function setThemeIcon(theme) {
if (theme === 'dark') {
document.getElementById('theme-icon').textContent = '☀️'; // Sun icon for dark mode
} else {
document.getElementById('theme-icon').textContent = '🌙'; // Moon icon for light mode
}
}
// Immediately apply the saved theme to avoid flickering
setThemeToDOM(getSavedTheme());
// Apply theme when the page loads
document.addEventListener('DOMContentLoaded', () => {
setThemeIcon(getSavedTheme())
document.querySelector('button#theme-toggle')?.addEventListener('click', toggleTheme);
});