Files
ignis/scripts/patch-obsidian.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2026-03-10 21:07:19 +01:00
#!/usr/bin/env node
// Patches the extracted Obsidian asar for browser use:
// 1. Removes Content-Security-Policy meta tag
// 2. Injects shim-loader.js script (non-deferred, before all other scripts)
// 3. Injects favicon link
2026-03-10 21:07:19 +01:00
const fs = require("fs");
const path = require("path");
2026-03-22 18:50:23 +01:00
const { getVersion } = require("../server/version");
2026-03-10 21:07:19 +01:00
const asarDir = process.argv[2];
if (!asarDir) {
console.error("Usage: node patch-obsidian.js <extracted-asar-dir>");
process.exit(1);
}
2026-03-22 18:50:23 +01:00
function patchHtml(filePath, version) {
const backupPath = filePath + ".orig";
if (!fs.existsSync(filePath) && !fs.existsSync(backupPath)) {
2026-03-10 22:31:01 +01:00
console.warn(`[patch] Skipping (not found): ${filePath}`);
return;
}
2026-03-10 21:07:19 +01:00
// Create backup of the original on first patch; restore from it on subsequent runs
if (!fs.existsSync(backupPath)) {
fs.copyFileSync(filePath, backupPath);
console.log(`[patch] Backed up original: ${backupPath}`);
} else {
fs.copyFileSync(backupPath, filePath);
}
2026-03-10 22:31:01 +01:00
let html = fs.readFileSync(filePath, "utf-8");
2026-03-10 21:07:19 +01:00
2026-03-10 22:31:01 +01:00
// Remove CSP meta tag
html = html.replace(
/\s*<meta\s+http-equiv="Content-Security-Policy"[^>]*>\s*/g,
"\n",
);
2026-03-10 21:07:19 +01:00
// Inject favicon into <head>
html = html.replace(
"</head>",
' <link rel="icon" type="image/png" href="favicon.png">\n</head>',
);
// Inject ignis scripts before the first <script> tag
2026-03-10 22:31:01 +01:00
html = html.replace(
2026-03-10 21:07:19 +01:00
'<script type="text/javascript"',
2026-03-22 18:50:23 +01:00
`<script type="text/javascript" src="ignis-ui.js?v=${version}"></script>\n` +
`<script type="text/javascript" src="shim-loader.js?v=${version}"></script>\n` +
'<script type="text/javascript"',
2026-03-10 22:31:01 +01:00
);
fs.writeFileSync(filePath, html);
console.log(`[patch] Patched ${filePath}`);
}
2026-03-10 21:07:19 +01:00
2026-03-22 18:50:23 +01:00
const version = getVersion();
patchHtml(path.join(asarDir, "index.html"), version);
console.log(`[patch] Injected version: ${version}`);