Files
Glimmer/internal/server/static/js/base.js
T
lucioandClaude Fable 5 a9da249760 Remove unsafe-inline from script-src CSP
Move every inline <script> block and on*= attribute out of the templates
into per-page files under static/js/, then tighten the CSP to
script-src 'self' so injected inline scripts are actually blocked.

- static/js/base.js: QR modal, form[data-confirm] submit confirmation,
  and a shared glimmerCopy clipboard helper (loaded by base.html)
- One script per template: index, bin_view, bin_token, admin,
  admin_edit, admin_bin, admin_bin_edit, admin_uploads
- Template data now flows through data-* attributes (data-slug,
  data-filename, data-max-mb, ...) instead of being interpolated
  into JS; confirm() prompts use data-confirm
- style-src keeps unsafe-inline for the style="" attributes
- Update CLAUDE.md / AGENTS.md with the no-inline-JS rule

Verified in a live browser session: shorten flow, login, QR modal,
paste editor (token radios, name preview), paste cards (preview
dialog, expand/close), public paste view, token prompt redirect, and
uploads page - no console errors or CSP violations.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-06-09 20:14:30 -07:00

57 lines
2.2 KiB
JavaScript

// Shared chrome behaviour for every layout page: QR modal, confirm-on-submit
// forms, and a clipboard helper. The CSP blocks inline scripts, so all page
// behaviour lives in files under /static/js/.
(function() {
'use strict';
// Copy text to the clipboard and flash the button label. Used by the
// per-page scripts.
window.glimmerCopy = function(text, btn) {
var orig = btn.textContent;
navigator.clipboard.writeText(text).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = orig; }, 1500);
});
};
window.openQRModal = function(slug, name) {
var overlay = document.getElementById('qr-modal-overlay');
document.getElementById('qr-modal-image').src = '/admin/qr/' + slug;
document.getElementById('qr-modal-link').textContent = '/admin/qr/' + slug;
var dl = document.getElementById('qr-modal-download');
dl.href = '/admin/qr/' + slug;
dl.download = (name || slug) + '_QR.svg';
overlay.classList.add('show');
};
window.closeQRModal = function() {
document.getElementById('qr-modal-overlay').classList.remove('show');
document.getElementById('qr-modal-image').src = '';
};
var overlay = document.getElementById('qr-modal-overlay');
if (overlay) {
overlay.addEventListener('click', function(e) {
if (e.target === overlay) window.closeQRModal();
});
}
var closeBtn = document.getElementById('qr-modal-close');
if (closeBtn) {
closeBtn.addEventListener('click', function() { window.closeQRModal(); });
}
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') window.closeQRModal();
});
// Forms with data-confirm ask before submitting (replaces inline
// onsubmit="return confirm(...)"). Delegated so it works for any form.
document.addEventListener('submit', function(e) {
var form = e.target;
if (form && form.getAttribute && form.getAttribute('data-confirm')) {
if (!window.confirm(form.getAttribute('data-confirm'))) {
e.preventDefault();
}
}
});
})();