Add page data delivery and publication guards

This commit is contained in:
john
2026-07-08 21:10:47 +08:00
parent 8d629e7a4e
commit eea14c1855
66 changed files with 3035 additions and 413 deletions
+30
View File
@@ -0,0 +1,30 @@
/**
* Insert markup before the document's closing </body> or </head>, ignoring
* occurrences inside inline scripts, styles, or string literals.
*/
function maskHtmlLiteralRegions(html) {
return String(html ?? '').replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, (match) =>
' '.repeat(match.length),
).replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, (match) => ' '.repeat(match.length));
}
export function injectBeforeDocumentClosingBody(html, markup) {
const source = String(html ?? '');
const injection = String(markup ?? '');
if (!injection) return source;
const masked = maskHtmlLiteralRegions(source).toLowerCase();
const lastIndex = masked.lastIndexOf('</body>');
if (lastIndex < 0) return `${source}${injection}`;
return `${source.slice(0, lastIndex)}${injection}${source.slice(lastIndex)}`;
}
export function injectBeforeDocumentClosingHead(html, markup) {
const source = String(html ?? '');
const injection = String(markup ?? '');
if (!injection) return source;
const masked = maskHtmlLiteralRegions(source).toLowerCase();
const index = masked.indexOf('</head>');
if (index < 0) return `${injection}${source}`;
return `${source.slice(0, index)}${injection}${source.slice(index)}`;
}