maboloshi/github-chinese · error · Error
[GitHub 中文化插件] 词库文件 locals.js 未加载
Error message
[GitHub 中文化插件] 词库文件 locals.js 未加载
What it means
Same checkI18NLoaded() guard as the other variants, but this build (main.user.js) sources its vocabulary from GitHub's raw.githubusercontent.com gh-pages branch via // @require .../locals.js?v1.9.4.4-2026-08-04. The throw fires when that require did not define the global I18N before the IIFE runs, so the plugin halts rather than translating with an empty dictionary.
Solutions
- Visit the exact @require URL in a browser; a 404 means the gh-pages version mismatch — update the @require URL to the currently published locals.js path/version.
- If raw.githubusercontent.com is blocked, switch to the NJU mirror build (main(nju.edu).user.js) whose @require points at mirror.nju.edu.cn.
- Re-enable 'download @require resources' and force the manager to re-fetch (reinstall or edit+save the metadata).
- As a last resort, download locals.js locally and switch the @require to a file:/// URL with local-file access enabled.
Example fix
// before // @require https://raw.githubusercontent.com/mabolashi/github-chinese/gh-pages/locals.js?v1.9.4.4-2026-08-04 // after — fix a 404 by aligning the version with the actually published gh-pages file // @require https://raw.githubusercontent.com/mabolashi/github-chinese/gh-pages/locals.js?v1.9.4.4
Defensive patterns
Strategy: validation
Validate before calling
// After @require should have run, before init():
if (typeof I18N === 'undefined' || typeof I18N.t !== 'function' && typeof I18N !== 'object') {
console.error('[github-chinese] gh-pages locals.js missing — skipping');
return;
} Type guard
function isI18NPresent(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null;
} Try / catch
try {
checkI18NLoaded();
init();
} catch (e) {
console.warn('[github-chinese] skipped:', e instanceof Error ? e.message : e);
} Prevention
- Verify the @require URL resolves to a JS file before installing.
- Keep the @require version string in sync with the published gh-pages artifact.
- Have a fallback mirror URL ready for networks that block raw.githubusercontent.com.
- Ensure the manager fetches @require resources on install/upgrade.
When it happens
Trigger: The @require of https://raw.githubusercontent.com/mabolashi/github-chinese/gh-pages/locals.js?v... returns a 404 (the gh-pages file was removed/renamed or the version tag in the URL no longer matches a published file), or raw.githubusercontent.com is blocked/unreachable from the user's network, or the manager did not fetch @require resources — leaving I18N undefined at line 165.
Common situations: raw.githubusercontent.com blocked behind the GFW or a corporate proxy; a stale @require version string pointing at a not-yet-published or renamed gh-pages artifact; gh-pages branch HEAD moved but the cache-busting ?v=... is pinned to a missing path; manager network/@require disabled.
Related errors
AI-assisted analysis of maboloshi/github-chinese@1db777260a (2026-08-13).
Data as JSON: /api/errors/d12ffc8ec4b1b931.
Report an issue: GitHub.
Appendix: source
Thrown at main.user.js:143
// 当前运行时状态
pageConfig: null, // 当前页面配置(null 表示无有效页面)
currentURL: window.location.href, // 当前页面URL
transEngine: 'iflyrec', // 当前翻译引擎
mutationObserver: null, // DOM变化观察器
urlChangeHandler: null, // 存储URL变化处理器
dynamicMenus: {}, // 动态菜单ID记录
initDone: false,
};
/* =========================== 安全检查 =========================== */
/**
* 检查词库文件是否加载 — 未加载则抛出错误阻止继续执行
*/
function checkI18NLoaded() {
if (typeof I18N === 'undefined') {
alert('GitHub 汉化插件:词库文件 locals.js 未加载,脚本无法运行!');
throw new Error('[GitHub 中文化插件] 词库文件 locals.js 未加载');
}
}
/**
* 错误边界 — 包装函数,捕获异常避免阻断页面正常使用
* @param {Function} fn - 要执行的函数
* @param {string} label - 错误标签
* @returns {Function} 包装后的函数
*/
function safe(fn, label) {
return function (...args) {
try {
return fn.apply(this, args);
} catch (e) {
console.error(`[GitHub 中文化插件] ${label} 出错:`, e);
}
};
}View on GitHub (pinned to 1db777260a)