Files
yianxue-assistant/build-userscript.mjs
T

34 lines
2.0 KiB
JavaScript

import { readFileSync, writeFileSync } from 'node:fs';
const metadata = `// ==UserScript==
// @name 谊安学助手 2.0
// @namespace yianxue-question-bank
// @version 2.1.0
// @description 谊安学助手:统一题库、六栏面板、视频任务与考试全文核验。
// @match http://10.119.67.7/clientcn/*
// @run-at document-idle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_setClipboard
// @grant GM_openInTab
// @noframes
// ==/UserScript==
`;
const zip = readFileSync(new URL('./vendor/jszip.min.js', import.meta.url), 'utf8');
const zipLicense = readFileSync(new URL('./vendor/LICENSE-JSZip.md', import.meta.url), 'utf8').split(/\r?\nGPL version 3/)[0];
const pakoLicense = readFileSync(new URL('./vendor/LICENSE-pako.txt', import.meta.url), 'utf8');
const notices = '/* Third-party notices: JSZip and pako distributed under their MIT licenses.\n'
+ zipLicense + '\n' + pakoLicense + '\n*/\n';
// 用局部 CommonJS 环境加载内置 ZIP 库,避免被网页的 AMD / require 接管。
const zipWrapper = '(function () { const module = { exports: {} }; const exports = module.exports;\n'
+ zip + '\nwindow.JSZip = module.exports;\n})();\n';
const modules = ['question-bank-core.js', 'bank-store.js', 'answer-matcher.js',
'copy-controller.js', 'assistant-panel.js', 'answer-runner.js', 'learning-engine.js', 'quiz-adapter.js', 'exam-controller.js', 'practice-exporter.js'];
const source = modules.map(name => readFileSync(new URL('./' + name, import.meta.url), 'utf8')).join('\n');
const scope = readFileSync(new URL('./site-scope.js', import.meta.url), 'utf8');
// Also enforce the boundary when a manager injects manually or ignores metadata.
const bundle = '(function () {\n' + scope + '\nif (!YianSiteScope.isAllowedURL(location.href)) return;\n'
+ zipWrapper + source + '\n})();\n';
writeFileSync(new URL('./谊安学助手 2.0.user.js', import.meta.url), metadata + '\n' + notices + bundle, 'utf8');
console.log('已生成 谊安学助手 2.0.user.js');