Files
katelyatv/patches/patch-announcement.js
T

21 lines
1.3 KiB
JavaScript

// 把根布局的免责声明公告改为动态读取授权系统(CardAuth)公告
// 幂等:含标记则跳过。失败开放(fail-open):授权系统异常时保留原公告。
const fs = require("fs");
const target = "/app/.next/server/chunks/8583.js";
const MARK = "/*ANN-CARD*/";
if (!fs.existsSync(target)) { console.log("8583.js not found"); process.exit(0); }
let s = fs.readFileSync(target, "utf8");
if (s.includes(MARK)) { console.log("layout-announcement: skip (already)"); process.exit(0); }
const anchor = '}let u={STORAGE_TYPE:process.env.NEXT_PUBLIC_STORAGE_TYPE||"localstorage",ENABLE_REGISTER:n,IMAGE_PROXY:o,DOUBAN_PROXY:p};return';
const idx = s.indexOf(anchor);
if (idx < 0) { console.error("anchor not found"); process.exit(1); }
const di = s.indexOf("async function d({children");
if (di < 0 || idx < di) { console.error("anchor not inside layout d()"); process.exit(1); }
const inject = ';' + MARK +
'try{const __rc=await fetch("http://127.0.0.1:3000/api/license-config",{cache:"no-store",signal:AbortSignal.timeout(1500)}).then(x=>x.json());' +
'if(__rc&&200===__rc.code&&__rc.data&&__rc.data.announcement&&__rc.data.announcement.content){s=String(__rc.data.announcement.content)}}catch(__e){}' +
anchor;
s = s.slice(0, idx) + inject + s.slice(idx + anchor.length);
fs.writeFileSync(target, s);
console.log("layout-announcement: injected");