fix(html-patcher): escape $ in inline script replacement to prevent String.replace() special pattern corruption — root cause of observer SyntaxError #task-619

This commit is contained in:
Variet Worker
2026-04-13 12:22:02 +09:00
parent a214ab029f
commit d6ed8764b8
2 changed files with 6 additions and 3 deletions

View File

@@ -293,14 +293,17 @@ function _patchHtmlFiles(scriptDir: string, combinedScript: string, logToFile: (
logToFile(`[OBSERVER] ${spec.name} removed old inline script block`);
}
// Insert BEFORE </body> (not </html>) to ensure Electron executes it
// CRITICAL: Escape $ in inlineBlock to prevent String.replace() special patterns
// ($' = text after match, $& = matched text, etc.) which corrupt the JS code.
const safeInlineBlock = inlineBlock.replace(/\$/g, '$$$$');
if (html.includes('</body>')) {
html = html.replace('</body>',
`\n${inlineBlock}\n</body>`);
`\n${safeInlineBlock}\n</body>`);
logToFile(`[OBSERVER] ${spec.name} inline script INSERTED before </body>`);
} else {
// Fallback: insert before </html>
html = html.replace('</html>',
`\n${inlineBlock}\n</html>`);
`\n${safeInlineBlock}\n</html>`);
logToFile(`[OBSERVER] ${spec.name} inline script INSERTED before </html> (fallback)`);
}
// SAFETY: Final validation before write