Skip to content

Commit a097327

Browse files
hellpanderrrclaude
andcommitted
feat: context-aware lua_init (web + extension) + extension sync script
- scripts/lua_init.js detects the extension (chrome.runtime.getURL) and loads Lua modules from chrome-extension:// URLs; otherwise keeps the web app's ../wiktionary_pron/ HTTP path. Removes debug print noise. - utils/sync_extension.cjs copies languages.js/lexicon.js/utils.js/ lua_init.js into utils/ext_tmp/scripts (npm run build:ext) so the extension can be rebuilt from the app without drift. The extension itself lives in utils/ext_tmp/ and will move to its own repo. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent bea9bf1 commit a097327

3 files changed

Lines changed: 68 additions & 26 deletions

File tree

wiktionary_pron/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"test:unit": "mocha scripts/tests/unit/*.test.js --require scripts/tests/setup.cjs --timeout 20000",
99
"test:e2e": "playwright test --grep-invert macronizer",
1010
"test:e2e:macronizer": "playwright test e2e/macronizer.spec.js",
11-
"serve": "http-server .. -p 8993 -s"
11+
"serve": "http-server .. -p 8993 -s",
12+
"build:ext": "node utils/sync_extension.cjs"
1213
},
1314
"devDependencies": {
1415
"@playwright/test": "^1.49.0",

wiktionary_pron/scripts/lua_init.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,70 @@ import {
77
const factory = await lb.factory;
88
const lua = await factory.createEngine();
99

10-
// Set a JS function to be a global lua function
11-
12-
lua.global.set("fetch", (url) => fetchWithCache(url));
13-
lua.global.set("fetchMultiple", (url) => fetchWithCacheMultiple(url));
10+
// Detect extension context: chrome.runtime.getURL exists only in extensions.
11+
const isExtension = typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.getURL;
12+
const LUA_BASE = isExtension
13+
? chrome.runtime.getURL("lua_modules/")
14+
: "../wiktionary_pron/lua_modules/";
1415

16+
// Mount initial Lua files from the right place
1517
async function mountFile(file_path, lua_path) {
1618
const content = await fetch(file_path).then((data) => data.text());
1719
await factory.mountFile(lua_path, content);
1820
}
1921

22+
// Set a JS function to be a global lua function.
23+
// In the web app we use the HTTP-caching fetch; in the extension we use a
24+
// direct fetch that resolves chrome-extension:// URLs.
25+
lua.global.set("fetch", isExtension
26+
? (url) => fetch(url)
27+
: (url) => fetchWithCache(url),
28+
);
29+
lua.global.set("fetchMultiple", isExtension
30+
? (url) => fetch(url)
31+
: (url) => fetchWithCacheMultiple(url),
32+
);
33+
2034
lua.global.set("updateLoadingText", (file_path, extension) =>
2135
updateLoadingText(file_path, extension),
2236
);
2337

24-
await mountFile("../wiktionary_pron/lua_modules/memoize.lua", "memoize.lua");
25-
//await mountFile("lua_modules/memoize.lua", "memoize.lua");
38+
await mountFile(isExtension
39+
? chrome.runtime.getURL("lua_modules/memoize.lua")
40+
: "../wiktionary_pron/lua_modules/memoize.lua",
41+
"memoize.lua");
2642

43+
// The require shim Lua code is identical in both contexts; only the URL it
44+
// passes to JS-side fetch() changes via LUA_BASE.
2745
await lua.doString(`
2846
memoize = require('memoize')
2947
t = {}
3048
function require(path, extension)
3149
extension = extension or 'lua'
32-
print(' required '..path,'from:', debug.getinfo(2).name)
3350
table.insert(t, 'lua_modules'..string.char(92)..path)
3451
if select(2,string.gsub(path, "%.", "")) > 0 then
3552
new_path = string.gsub(path,"%.", "/",1)
36-
print('replacing ', path,'->', new_path)
3753
path = new_path
3854
end
39-
40-
4155
updateLoadingText(path, extension)
42-
43-
resp = fetch(string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension)):await()
44-
45-
46-
56+
resp = fetch('${LUA_BASE}' .. string.format('%s.%s',path,extension)):await()
4757
updateLoadingText("", "")
48-
4958
local text = resp:text():await()
5059
local module = load(text)()
51-
print(' loaded '..path)
5260
return module
5361
end
54-
55-
56-
5762
require = memoize(require)
5863
require('debug/track')
5964
require('ustring/charsets')
6065
require('ustring/lower')
6166
require('mw-title')
6267
mw = require('mw')
6368
`);
64-
console.log(lua);
6569

6670
async function loadLanguage(code) {
67-
console.log(lua);
68-
6971
await lua.doString(`t = {}
7072
${code} = require("${code}-pron_wasm")`);
71-
// Get a global lua function as a JS function
7273
window[code + "_ipa"] = lua.global.get(code);
73-
// Set a JS function to be a global lua function
7474
}
7575

7676
export { loadLanguage, updateLoadingText };
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* One-way sync: copies shared scripts from the main app into the extension's
3+
* scripts/ directory. Run this after any change to the shared files so the
4+
* extension doesn't drift.
5+
*
6+
* Usage: node utils/sync_extension.cjs
7+
* Add to package.json as `npm run build:ext`.
8+
*/
9+
10+
const fs = require("node:fs");
11+
const path = require("node:path");
12+
13+
const ROOT = path.resolve(__dirname, "..");
14+
const EXT = path.join(ROOT, "utils", "ext_tmp");
15+
16+
// Only files that are identical between the main app and the extension.
17+
// tts_engine.js is extension-specific (not scripts/tts.js) — skip it.
18+
const FILES = [
19+
"scripts/languages.js",
20+
"scripts/lexicon.js",
21+
"scripts/utils.js",
22+
"scripts/lua_init.js",
23+
// Classic script — must stay extension-side, but rebuilt alongside the sync
24+
"utils/ext_tmp/options_lexicon_shim.js",
25+
];
26+
27+
let copied = 0;
28+
for (const rel of FILES) {
29+
const src = path.join(ROOT, rel);
30+
const dest = path.join(EXT, rel);
31+
if (!fs.existsSync(src)) {
32+
console.warn(` SKIP ${rel} — source not found`);
33+
continue;
34+
}
35+
const content = fs.readFileSync(src, "utf8");
36+
fs.mkdirSync(path.dirname(dest), { recursive: true });
37+
fs.writeFileSync(dest, content, "utf8");
38+
console.log(` ${rel} -> utils/ext_tmp/${rel}`);
39+
copied++;
40+
}
41+
console.log(`Synced ${copied} file(s).`);

0 commit comments

Comments
 (0)