92 lines
4.9 KiB
JavaScript
92 lines
4.9 KiB
JavaScript
import { randomBytes } from 'node:crypto'
|
|
import { darkReaderPath } from './preview-assets.js'
|
|
|
|
// A separate document keeps the main application's strict image/script policy intact.
|
|
// The sandbox has no same-origin access. Only the nonce-protected integration and
|
|
// the locally bundled Dark Reader code can execute; scripts from mail are removed.
|
|
export function htmlPreviewDocument({ dark = false, remoteImages = false } = {}) {
|
|
const nonce = randomBytes(24).toString('base64')
|
|
const background = dark ? '#222225' : '#ffffff',
|
|
foreground = dark ? '#eeeef0' : '#252329',
|
|
accent = dark ? '#b49aef' : '#6d3bd1'
|
|
const csp = `default-src 'none'; img-src data:${remoteImages ? ' https:' : ''}; style-src 'unsafe-inline'; script-src 'nonce-${nonce}'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'; sandbox allow-scripts`
|
|
const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="color-scheme" content="${dark ? 'dark' : 'light'}"><style>
|
|
html,body{margin:0;background:${background};color:${foreground};color-scheme:${dark ? 'dark' : 'light'}}
|
|
body{font:14px Arial,sans-serif;overflow-wrap:break-word}
|
|
#mail-viewport{width:100%;overflow-x:auto;overflow-y:hidden}
|
|
#mail-content{display:flow-root;padding:16px;box-sizing:border-box;min-width:0}
|
|
img{max-width:100%}pre{white-space:pre-wrap}a{color:${accent}}
|
|
.syncova-image-unavailable{background:#8882;outline:1px solid #8885;object-fit:contain}
|
|
</style>${dark ? `<script nonce="${nonce}" src="${darkReaderPath}"></script>` : ''}</head><body><div id="mail-viewport"><div id="mail-content"></div></div><script nonce="${nonce}">
|
|
(() => {
|
|
const channel = location.hash.slice(1), root = document.getElementById('mail-content');
|
|
let scheduled = false, lastHeight = 0, lastFailed = -1, failedImages = 0, width = 0;
|
|
function fit() {
|
|
width = document.documentElement.clientWidth;
|
|
root.style.zoom = '1'; root.style.width = 'auto';
|
|
const natural = Math.min(20000, Math.max(width, root.scrollWidth));
|
|
if (natural > width + 2) {
|
|
root.style.width = natural + 'px';
|
|
root.style.zoom = String(Math.max(0.5, width / natural));
|
|
}
|
|
}
|
|
function darken() {
|
|
if (!${dark ? 'true' : 'false'} || !window.DarkReader) return;
|
|
// No CSS/image proxy fetches: remote-content policy belongs exclusively to CSP.
|
|
DarkReader.setFetchMethod(() => Promise.reject(new Error('Preview fetch disabled')));
|
|
const create = document.createElement;
|
|
// The trusted library synchronously installs its own fixed stylesheet observer
|
|
// script. Give only that synchronous call the preview nonce; mail scripts remain removed.
|
|
document.createElement = function(name, options) {
|
|
const element = create.call(this, name, options);
|
|
if (String(name).toLowerCase() === 'script') element.nonce = '${nonce}';
|
|
return element;
|
|
};
|
|
try {
|
|
DarkReader.enable({brightness:100,contrast:100,sepia:0,darkSchemeBackgroundColor:'#222225',darkSchemeTextColor:'#eeeef0'},
|
|
{ignoreImageAnalysis:['*'],disableStyleSheetsProxy:true,disableCustomElementRegistryProxy:true});
|
|
} finally { document.createElement = create; }
|
|
}
|
|
function report() {
|
|
if (scheduled) return;
|
|
scheduled = true;
|
|
requestAnimationFrame(() => {
|
|
scheduled = false;
|
|
const height = Math.max(1, Math.ceil(Math.max(root.getBoundingClientRect().height, root.scrollHeight * (Number(root.style.zoom) || 1))));
|
|
if (height !== lastHeight || failedImages !== lastFailed) {
|
|
lastHeight = height; lastFailed = failedImages;
|
|
parent.postMessage({ type: 'syncova-preview-size', channel, height, failedImages }, '*');
|
|
}
|
|
});
|
|
}
|
|
addEventListener('message', (event) => {
|
|
const data = event.data;
|
|
if (event.source !== parent || data?.type !== 'syncova-preview-content' ||
|
|
data.channel !== channel || typeof data.html !== 'string') return;
|
|
failedImages = 0;
|
|
if (window.DarkReader?.isEnabled()) DarkReader.disable();
|
|
root.innerHTML = data.html;
|
|
root.querySelectorAll('img').forEach(image => {
|
|
image.referrerPolicy = 'no-referrer';
|
|
image.addEventListener('load', () => { fit(); report(); });
|
|
const unavailable = () => {
|
|
if (image.classList.contains('syncova-image-unavailable')) return;
|
|
if (image.hasAttribute('width') && image.hasAttribute('height') && image.width <= 2 && image.height <= 2 && !image.alt) {image.style.display='none';return;}
|
|
failedImages++;
|
|
image.classList.add('syncova-image-unavailable');
|
|
image.alt ||= 'Bild nicht verfügbar';
|
|
report();
|
|
};
|
|
image.addEventListener('error', unavailable);
|
|
if (!image.getAttribute('src') && !image.getAttribute('srcset')) unavailable();
|
|
else if (image.complete && !image.naturalWidth) unavailable();
|
|
});
|
|
darken(); fit(); report();
|
|
});
|
|
new ResizeObserver(report).observe(root);
|
|
addEventListener('resize', () => {if (width !== document.documentElement.clientWidth) fit(); report();});
|
|
})();
|
|
</script></body></html>`
|
|
return { html, csp }
|
|
}
|