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]>
100 lines
4.2 KiB
JavaScript
100 lines
4.2 KiB
JavaScript
// Admin pastes grid: preview dialog, copy-link, QR, and card click handling.
|
|
(function() {
|
|
'use strict';
|
|
|
|
function previewURL(name, token) {
|
|
var u = '/bin/' + encodeURIComponent(name);
|
|
if (token) u += '/' + encodeURIComponent(token);
|
|
return u;
|
|
}
|
|
|
|
function openPreview(name, token, title) {
|
|
var dlg = document.getElementById('preview-dialog');
|
|
var fullURL = previewURL(name, token);
|
|
// iframe uses the embed view so it strips the sidebar / header / toolbar —
|
|
// only the paste body is rendered inside the dialog.
|
|
var embedURL = fullURL + '?embed=1';
|
|
document.getElementById('preview-frame').src = embedURL;
|
|
// The "Open ↗" link goes to the full URL (with chrome) so a new tab shows the
|
|
// normal public page.
|
|
document.getElementById('preview-open').href = fullURL;
|
|
document.getElementById('preview-title').textContent = title || name;
|
|
dlg.classList.remove('expanded');
|
|
document.getElementById('preview-expand').innerHTML = '⤢';
|
|
document.getElementById('preview-expand').setAttribute('aria-label', 'Expand');
|
|
dlg.showModal();
|
|
}
|
|
|
|
function closePreview() {
|
|
document.getElementById('preview-dialog').close();
|
|
}
|
|
|
|
function toggleExpand() {
|
|
var dlg = document.getElementById('preview-dialog');
|
|
var expanded = dlg.classList.toggle('expanded');
|
|
var btn = document.getElementById('preview-expand');
|
|
btn.innerHTML = expanded ? '⤡' : '⤢';
|
|
btn.setAttribute('aria-label', expanded ? 'Shrink' : 'Expand');
|
|
}
|
|
|
|
function showBinQR(name, full) {
|
|
var url = location.origin + '/bin/' + encodeURIComponent(name);
|
|
var fetchUrl = '/admin/bin/qr/' + encodeURIComponent(name);
|
|
if (full) fetchUrl += '?full=1';
|
|
var overlay = document.getElementById('qr-modal-overlay');
|
|
document.getElementById('qr-modal-image').src = fetchUrl;
|
|
document.getElementById('qr-modal-link').textContent = url + (full ? ' (with token)' : '');
|
|
var dl = document.getElementById('qr-modal-download');
|
|
dl.href = fetchUrl;
|
|
dl.download = name + '_QR.svg';
|
|
overlay.classList.add('show');
|
|
}
|
|
|
|
// Per-card wiring: action buttons read name/token/title from the card's
|
|
// data attributes; clicking the card itself (not an action) opens the
|
|
// preview. Clicks on links/buttons/forms are ignored by the card handler,
|
|
// so the action buttons need no stopPropagation.
|
|
document.querySelectorAll('.js-preview').forEach(function(el) {
|
|
var copyBtn = el.querySelector('.js-copy-link');
|
|
if (copyBtn) {
|
|
copyBtn.addEventListener('click', function() {
|
|
var url = location.origin + previewURL(el.dataset.name, el.dataset.token);
|
|
window.glimmerCopy(url, copyBtn);
|
|
});
|
|
}
|
|
var qrBtn = el.querySelector('.js-bin-qr');
|
|
if (qrBtn) {
|
|
qrBtn.addEventListener('click', function() {
|
|
showBinQR(el.dataset.name, false);
|
|
});
|
|
}
|
|
|
|
el.addEventListener('click', function(e) {
|
|
if (e.target.closest('.paste-card-actions')) return;
|
|
if (e.target.closest('a, button, form, input')) return;
|
|
openPreview(el.dataset.name, el.dataset.token, el.dataset.title);
|
|
});
|
|
el.setAttribute('tabindex', '0');
|
|
el.setAttribute('role', 'button');
|
|
el.addEventListener('keydown', function(e) {
|
|
if ((e.key === 'Enter' || e.key === ' ') && !e.target.closest('.paste-card-actions')) {
|
|
e.preventDefault();
|
|
openPreview(el.dataset.name, el.dataset.token, el.dataset.title);
|
|
}
|
|
});
|
|
});
|
|
|
|
var expandBtn = document.getElementById('preview-expand');
|
|
if (expandBtn) expandBtn.addEventListener('click', toggleExpand);
|
|
var closeBtn = document.getElementById('preview-close');
|
|
if (closeBtn) closeBtn.addEventListener('click', closePreview);
|
|
|
|
// Clear iframe when dialog closes
|
|
var dlg = document.getElementById('preview-dialog');
|
|
if (dlg) {
|
|
dlg.addEventListener('close', function() {
|
|
document.getElementById('preview-frame').src = 'about:blank';
|
|
});
|
|
}
|
|
})();
|