Files
yianxue-assistant/build-userscript.mjs
T

33 lines
1.9 KiB
JavaScript

import { readFileSync, writeFileSync } from 'node:fs';
const metadata = `// ==UserScript==
// @name 谊安学助手 2.0
// @namespace yianxue-question-bank
// @version 2.0.3
// @description 谊安学站点助手:可拖动面板、岗位搜索、答案全文核对及自动选择。
// @match http://10.119.67.7/clientcn/*
// @run-at document-idle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_setClipboard
// @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', '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');