ext-localize 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +538 -2
- package/assets/fill-core.js +338 -0
- package/dist/batch.js +25 -0
- package/dist/check.js +319 -0
- package/dist/cli.js +620 -0
- package/dist/config.js +110 -0
- package/dist/glossary.js +70 -0
- package/dist/listing.js +311 -0
- package/dist/locales.js +86 -0
- package/dist/messages.js +123 -0
- package/dist/placeholders.js +171 -0
- package/dist/providers/anthropic.js +60 -0
- package/dist/providers/deepl.js +269 -0
- package/dist/providers/index.js +40 -0
- package/dist/providers/ollama.js +48 -0
- package/dist/providers/openai.js +50 -0
- package/dist/providers/prompt.js +100 -0
- package/dist/providers/types.js +18 -0
- package/dist/retry.js +66 -0
- package/dist/stale.js +151 -0
- package/dist/state.js +201 -0
- package/dist/stores/chrome-auto.js +205 -0
- package/dist/stores/chrome.js +147 -0
- package/dist/stores/edge.js +43 -0
- package/dist/stores/firefox.js +235 -0
- package/dist/translate.js +308 -0
- package/package.json +54 -4
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* fill-core.js — fills the "Detailed description" of every language on the
|
|
3
|
+
* Chrome Web Store dashboard "Store listing" tab. Used by `ext-localize push chrome`
|
|
4
|
+
* (paste script) and `ext-localize push chrome --auto` (DevTools Protocol).
|
|
5
|
+
*
|
|
6
|
+
* Copied from spikes/chrome-listing-delivery/shared/fill-core.js (the fixed version:
|
|
7
|
+
* the language dropdown is matched by data-value, and the trigger text is checked with
|
|
8
|
+
* "contains", because the real dashboard adds icon text like "arrow_drop_down").
|
|
9
|
+
*
|
|
10
|
+
* Plain browser JavaScript, no build step. It sets `globalThis.ListingFill`.
|
|
11
|
+
*
|
|
12
|
+
* Rules it follows:
|
|
13
|
+
* - Find elements by role/label first, CSS selectors second.
|
|
14
|
+
* - Fail loudly when a lookup finds 0 or more than 1 element.
|
|
15
|
+
* - Read the language list from the live dropdown; report locales it cannot match.
|
|
16
|
+
* Only two code rewrites: "_" -> "-" and "he" -> "iw".
|
|
17
|
+
* - Wait for the page to settle after a language switch before writing.
|
|
18
|
+
* - After filling, walk all languages again and compare what the page holds to the file.
|
|
19
|
+
* - Never clicks "Submit for review" / "Publish". "Save draft" only if the caller asks.
|
|
20
|
+
*/
|
|
21
|
+
(function (root) {
|
|
22
|
+
'use strict';
|
|
23
|
+
|
|
24
|
+
const VERSION = '0.1.0';
|
|
25
|
+
const MAX_LEN = 16000; // dashboard limit for the detailed description (from LocalePack's selector; not in Google docs)
|
|
26
|
+
|
|
27
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
28
|
+
const norm = (s) => String(s == null ? '' : s).replace(/\r\n/g, '\n');
|
|
29
|
+
const clean = (s) => String(s || '').replace(/\s+/g, ' ').trim();
|
|
30
|
+
|
|
31
|
+
// Chrome _locales code -> dashboard code. pt_BR -> pt-BR, he -> iw.
|
|
32
|
+
function toDashboardCode(locale) {
|
|
33
|
+
let c = String(locale).replace(/_/g, '-');
|
|
34
|
+
if (c === 'he' || c.startsWith('he-')) c = 'iw' + c.slice(2);
|
|
35
|
+
return c;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isVisible(el) {
|
|
39
|
+
if (!el || !el.isConnected) return false;
|
|
40
|
+
const r = el.getBoundingClientRect();
|
|
41
|
+
if (r.width === 0 && r.height === 0) return false;
|
|
42
|
+
const cs = getComputedStyle(el);
|
|
43
|
+
return cs.visibility !== 'hidden' && cs.display !== 'none';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Small accessible-name helper (not the full spec; enough for labels/aria).
|
|
47
|
+
function accName(el) {
|
|
48
|
+
if (!el) return '';
|
|
49
|
+
const al = el.getAttribute('aria-label');
|
|
50
|
+
if (al) return clean(al);
|
|
51
|
+
const lb = el.getAttribute('aria-labelledby');
|
|
52
|
+
if (lb) {
|
|
53
|
+
const t = lb.split(/\s+/).map((id) => document.getElementById(id)).filter(Boolean).map((n) => n.textContent).join(' ');
|
|
54
|
+
if (clean(t)) return clean(t);
|
|
55
|
+
}
|
|
56
|
+
if (el.id) {
|
|
57
|
+
const lab = document.querySelector('label[for="' + CSS.escape(el.id) + '"]');
|
|
58
|
+
if (lab) return clean(lab.textContent);
|
|
59
|
+
}
|
|
60
|
+
const wrap = el.closest('label');
|
|
61
|
+
if (wrap) return clean(wrap.textContent);
|
|
62
|
+
return '';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Try strategies in order. The first strategy that finds anything wins.
|
|
66
|
+
// 0 matches overall, or >1 match in the winning strategy -> throw.
|
|
67
|
+
function findOne(what, strategies) {
|
|
68
|
+
for (const [how, fn] of strategies) {
|
|
69
|
+
let found;
|
|
70
|
+
try { found = Array.from(new Set(fn())).filter(isVisible); } catch (e) { found = []; }
|
|
71
|
+
if (found.length === 1) return { el: found[0], how };
|
|
72
|
+
if (found.length > 1) throw new Error('More than one match for ' + what + ' (strategy "' + how + '", ' + found.length + ' matches). Selectors need an update.');
|
|
73
|
+
}
|
|
74
|
+
throw new Error('Not found: ' + what + '. The dashboard markup may have changed.');
|
|
75
|
+
}
|
|
76
|
+
function tryFindOne(what, strategies) {
|
|
77
|
+
try { return findOne(what, strategies); } catch (e) { return null; }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const $$ = (sel, r) => Array.from((r || document).querySelectorAll(sel));
|
|
81
|
+
|
|
82
|
+
function findTrigger() {
|
|
83
|
+
return findOne('language dropdown ("Current editing language")', [
|
|
84
|
+
['role=combobox + label', () => $$('[role=combobox]').filter((e) => /editing language|^language$/i.test(accName(e)))],
|
|
85
|
+
['text label + button', () => {
|
|
86
|
+
const labels = $$('label, span, div, p').filter((e) => e.children.length === 0 && /^current editing language$/i.test(clean(e.textContent)));
|
|
87
|
+
const out = [];
|
|
88
|
+
for (const lab of labels) {
|
|
89
|
+
const box = lab.parentElement;
|
|
90
|
+
if (box) out.push(...$$('[role=button], [role=combobox], [aria-haspopup], button, select', box));
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}],
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function findList() {
|
|
98
|
+
return tryFindOne('language list', [
|
|
99
|
+
['role=listbox named Language', () => $$('[role=listbox]').filter((e) => /^language$/i.test(accName(e)))],
|
|
100
|
+
["css ul[aria-label='Language']", () => $$("ul[aria-label='Language']")],
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function findTextarea() {
|
|
105
|
+
return findOne('description textarea', [
|
|
106
|
+
['textarea labelled "description"', () => $$('textarea').filter((e) => /description/i.test(accName(e)))],
|
|
107
|
+
["css textarea[maxlength='16000']", () => $$("textarea[maxlength='16000']")],
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function findSaveDraft() {
|
|
112
|
+
const r = findOne('"Save draft" button', [
|
|
113
|
+
['button named Save draft', () => $$('button, [role=button]').filter((e) => /^save draft$/i.test(clean(e.textContent) || accName(e)))],
|
|
114
|
+
]);
|
|
115
|
+
if (/submit|publish|review/i.test(clean(r.el.textContent))) throw new Error('Refusing: save button text looks like submit/publish.');
|
|
116
|
+
return r.el;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Full pointer/mouse sequence. Some UI kits open on mousedown, others on click.
|
|
120
|
+
function realClick(el) {
|
|
121
|
+
const o = { bubbles: true, cancelable: true, view: window, button: 0 };
|
|
122
|
+
if (typeof PointerEvent === 'function') el.dispatchEvent(new PointerEvent('pointerdown', o));
|
|
123
|
+
el.dispatchEvent(new MouseEvent('mousedown', o));
|
|
124
|
+
if (typeof PointerEvent === 'function') el.dispatchEvent(new PointerEvent('pointerup', o));
|
|
125
|
+
el.dispatchEvent(new MouseEvent('mouseup', o));
|
|
126
|
+
el.dispatchEvent(new MouseEvent('click', o));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function waitFor(fn, what, timeout) {
|
|
130
|
+
const end = Date.now() + (timeout || 8000);
|
|
131
|
+
for (;;) {
|
|
132
|
+
let v;
|
|
133
|
+
try { v = fn(); } catch (e) { v = null; }
|
|
134
|
+
if (v) return v;
|
|
135
|
+
if (Date.now() > end) throw new Error('Timed out waiting for ' + what);
|
|
136
|
+
await sleep(50);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Resolve when the DOM has had no mutations for `quietMs`.
|
|
141
|
+
function waitQuiet(quietMs, timeout) {
|
|
142
|
+
quietMs = quietMs || 250;
|
|
143
|
+
return new Promise((resolve) => {
|
|
144
|
+
let t;
|
|
145
|
+
const done = () => { obs.disconnect(); clearTimeout(cap); resolve(); };
|
|
146
|
+
const obs = new MutationObserver(() => { clearTimeout(t); t = setTimeout(done, quietMs); });
|
|
147
|
+
obs.observe(document.body, { subtree: true, childList: true, attributes: true, characterData: true });
|
|
148
|
+
t = setTimeout(done, quietMs);
|
|
149
|
+
const cap = setTimeout(done, timeout || 8000);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Names the dashboard may show for a code, e.g. pt-BR -> "Portuguese (Brazil)" / "Brazilian Portuguese".
|
|
154
|
+
function displayNames(code) {
|
|
155
|
+
const out = new Set();
|
|
156
|
+
for (const languageDisplay of ['standard', 'dialect']) {
|
|
157
|
+
try {
|
|
158
|
+
const dn = new Intl.DisplayNames(['en'], { type: 'language', languageDisplay });
|
|
159
|
+
out.add(dn.of(code).toLowerCase());
|
|
160
|
+
} catch (e) { /* unknown code */ }
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function optionInfo(opt) {
|
|
166
|
+
return { el: opt, value: opt.getAttribute('data-value'), text: clean(opt.textContent) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function matchOption(locale, options) {
|
|
170
|
+
const code = toDashboardCode(locale).toLowerCase();
|
|
171
|
+
const withValue = options.filter((o) => o.value);
|
|
172
|
+
if (withValue.length) {
|
|
173
|
+
const m = withValue.filter((o) => o.value.toLowerCase() === code);
|
|
174
|
+
return m.length === 1 ? { opt: m[0], how: 'data-value' } : null;
|
|
175
|
+
}
|
|
176
|
+
// No codes in the markup: match on visible name, or a "(pt-BR)" style code in the text.
|
|
177
|
+
const names = displayNames(code);
|
|
178
|
+
const m = options.filter((o) => {
|
|
179
|
+
const t = o.text.toLowerCase();
|
|
180
|
+
return names.has(t) || t.includes('(' + code + ')') || t.endsWith(' ' + code) || t.endsWith(' - ' + code);
|
|
181
|
+
});
|
|
182
|
+
return m.length === 1 ? { opt: m[0], how: 'visible name' } : null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function closeList() {
|
|
186
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
|
|
187
|
+
realClick(document.body);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function listDashboardLanguages() {
|
|
191
|
+
const trig = findTrigger().el;
|
|
192
|
+
if (!findList()) realClick(trig);
|
|
193
|
+
const list = await waitFor(findList, 'language list to open');
|
|
194
|
+
const opts = $$('[role=option], li', list.el).map(optionInfo);
|
|
195
|
+
closeList();
|
|
196
|
+
await waitQuiet(100);
|
|
197
|
+
return opts.map((o) => ({ value: o.value, text: o.text }));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Does the closed dropdown show this option? The real dashboard puts extra text in the trigger
|
|
201
|
+
// (Material icon names like "arrow_drop_down" are text ligatures), so test "contains", not "equals".
|
|
202
|
+
// Fallback: the language code as a whole word, e.g. "en" in "English – en (default)".
|
|
203
|
+
function triggerShows(opt) {
|
|
204
|
+
const t = clean(findTrigger().el.textContent);
|
|
205
|
+
if (opt.text && t.includes(opt.text)) return true;
|
|
206
|
+
if (!opt.value) return false;
|
|
207
|
+
const esc = opt.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
208
|
+
return new RegExp('(^|[^\\w-])' + esc + '($|[^\\w-])', 'i').test(t);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function selectLocale(locale) {
|
|
212
|
+
const trig = findTrigger().el;
|
|
213
|
+
if (!findList()) realClick(trig);
|
|
214
|
+
const list = await waitFor(findList, 'language list to open');
|
|
215
|
+
const options = $$('[role=option], li', list.el).map(optionInfo);
|
|
216
|
+
const m = matchOption(locale, options);
|
|
217
|
+
if (!m) {
|
|
218
|
+
closeList();
|
|
219
|
+
await waitQuiet(100);
|
|
220
|
+
return { ok: false, reason: 'no matching language in dropdown', available: options.map((o) => o.value || o.text) };
|
|
221
|
+
}
|
|
222
|
+
const wanted = m.opt.text;
|
|
223
|
+
if (triggerShows(m.opt)) {
|
|
224
|
+
closeList();
|
|
225
|
+
await waitQuiet(100);
|
|
226
|
+
return { ok: true, how: m.how, already: true };
|
|
227
|
+
}
|
|
228
|
+
realClick(m.opt.el);
|
|
229
|
+
// Settle: trigger shows the new language, nothing is busy, the textarea is back, DOM is quiet.
|
|
230
|
+
try {
|
|
231
|
+
await waitFor(() => triggerShows(m.opt), 'dropdown to show ' + wanted);
|
|
232
|
+
} catch (e) {
|
|
233
|
+
let now = '?';
|
|
234
|
+
try { now = clean(findTrigger().el.textContent); } catch (e2) { /* trigger gone */ }
|
|
235
|
+
throw new Error(e.message + ' (dropdown text now: "' + now.slice(0, 80) + '")');
|
|
236
|
+
}
|
|
237
|
+
await waitFor(() => !document.querySelector('[aria-busy=true]') && findTextarea(), 'description box for ' + wanted);
|
|
238
|
+
await waitQuiet(200);
|
|
239
|
+
return { ok: true, how: m.how };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// The important part: React tracks the last value it saw. A plain `el.value = x`
|
|
243
|
+
// updates React's tracker too, so the 'input' event looks like "no change" and the
|
|
244
|
+
// text is lost on the next render. Calling the native prototype setter skips the tracker.
|
|
245
|
+
function setText(el, text, naive) {
|
|
246
|
+
el.focus();
|
|
247
|
+
if (naive) {
|
|
248
|
+
el.value = text;
|
|
249
|
+
} else {
|
|
250
|
+
const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set;
|
|
251
|
+
setter.call(el, text);
|
|
252
|
+
}
|
|
253
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
254
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
255
|
+
el.blur();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function waitForUserSave(log, timeout) {
|
|
259
|
+
return new Promise((resolve, reject) => {
|
|
260
|
+
const onClick = (e) => {
|
|
261
|
+
const b = e.target.closest && e.target.closest('button, [role=button]');
|
|
262
|
+
if (b && /^save draft$/i.test(clean(b.textContent))) { cleanup(); resolve(); }
|
|
263
|
+
};
|
|
264
|
+
const cleanup = () => { document.removeEventListener('click', onClick, true); clearTimeout(t); };
|
|
265
|
+
document.addEventListener('click', onClick, true);
|
|
266
|
+
const t = setTimeout(() => { cleanup(); reject(new Error('Timed out waiting for you to click Save draft')); }, timeout || 10 * 60 * 1000);
|
|
267
|
+
log('Check the text, then click "Save draft". I will continue after that.');
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* listings: { "pt_BR": { description: "..." }, ... }
|
|
273
|
+
* opts:
|
|
274
|
+
* mode: 'fill-all' fill every language, then one Save at the end (works if the dashboard keeps unsaved text)
|
|
275
|
+
* 'save-each' save after each language (needed if the dashboard drops unsaved text on switch)
|
|
276
|
+
* save: 'none' (default) | 'wait' (wait for the user to click Save draft) | 'auto' (click Save draft; never submit)
|
|
277
|
+
* verify: true (default) walk all languages again and compare
|
|
278
|
+
* naive: true use plain `el.value =` (only to prove the mock is hostile)
|
|
279
|
+
* log: function
|
|
280
|
+
*/
|
|
281
|
+
async function run(listings, opts) {
|
|
282
|
+
opts = Object.assign({ mode: 'fill-all', save: 'none', verify: true, naive: false }, opts || {});
|
|
283
|
+
const log = opts.log || ((...a) => console.log('[listing-fill]', ...a));
|
|
284
|
+
const report = { version: VERSION, mode: opts.mode, save: opts.save, filled: [], unmatched: [], tooLong: [], errors: [], verify: null, hint: null };
|
|
285
|
+
|
|
286
|
+
const locales = Object.keys(listings).sort();
|
|
287
|
+
for (const loc of locales) {
|
|
288
|
+
const text = norm(listings[loc] && listings[loc].description);
|
|
289
|
+
if (!text) { report.errors.push({ locale: loc, error: 'empty description' }); continue; }
|
|
290
|
+
if (text.length > MAX_LEN) { report.tooLong.push({ locale: loc, length: text.length }); continue; }
|
|
291
|
+
try {
|
|
292
|
+
const sel = await selectLocale(loc);
|
|
293
|
+
if (!sel.ok) { report.unmatched.push({ locale: loc, dashboardCode: toDashboardCode(loc), available: sel.available }); log('No dashboard language for', loc); continue; }
|
|
294
|
+
const ta = findTextarea();
|
|
295
|
+
setText(ta.el, text, opts.naive);
|
|
296
|
+
await waitQuiet(150);
|
|
297
|
+
const now = norm(findTextarea().el.value);
|
|
298
|
+
report.filled.push({ locale: loc, length: text.length, matchedBy: sel.how, textareaBy: ta.how, readBackOk: now === text });
|
|
299
|
+
log('Filled', loc, '(' + text.length + ' chars)');
|
|
300
|
+
if (opts.mode === 'save-each') {
|
|
301
|
+
if (opts.save === 'auto') { realClick(findSaveDraft()); await waitQuiet(300); }
|
|
302
|
+
else if (opts.save === 'wait') await waitForUserSave(log, opts.waitTimeoutMs);
|
|
303
|
+
}
|
|
304
|
+
} catch (e) {
|
|
305
|
+
report.errors.push({ locale: loc, error: e.message });
|
|
306
|
+
log('Error for', loc + ':', e.message);
|
|
307
|
+
if (/Selectors need an update|markup may have changed/.test(e.message)) break; // stop early, do not guess
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (opts.verify && report.filled.length) {
|
|
312
|
+
const mismatches = [];
|
|
313
|
+
for (const f of report.filled) {
|
|
314
|
+
try {
|
|
315
|
+
const sel = await selectLocale(f.locale);
|
|
316
|
+
const got = sel.ok ? norm(findTextarea().el.value) : null;
|
|
317
|
+
const want = norm(listings[f.locale].description);
|
|
318
|
+
if (got !== want) mismatches.push({ locale: f.locale, gotLength: got == null ? null : got.length, wantLength: want.length });
|
|
319
|
+
} catch (e) {
|
|
320
|
+
mismatches.push({ locale: f.locale, error: e.message });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
report.verify = { ok: mismatches.length === 0, mismatches };
|
|
324
|
+
if (mismatches.length && opts.mode === 'fill-all') {
|
|
325
|
+
report.hint = 'Text was lost after switching language. The dashboard probably drops unsaved text. Run again with mode "save-each".';
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (opts.mode === 'fill-all' && report.filled.length) {
|
|
330
|
+
if (opts.save === 'auto') { realClick(findSaveDraft()); await waitQuiet(300); log('Clicked Save draft.'); }
|
|
331
|
+
else log('Done. Review the languages, then click "Save draft" yourself.');
|
|
332
|
+
}
|
|
333
|
+
log('Report:', JSON.stringify(report, null, 2));
|
|
334
|
+
return report;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
root.ListingFill = { VERSION, MAX_LEN, run, toDashboardCode, selectLocale, listDashboardLanguages, findTextarea, setText };
|
|
338
|
+
})(typeof globalThis !== 'undefined' ? globalThis : window);
|
package/dist/batch.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function itemSize(item) {
|
|
2
|
+
return item.text.length + (item.description?.length ?? 0) + (item.markerHints?.join("").length ?? 0);
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Group items into batches: at most maxItems per batch and roughly maxChars
|
|
6
|
+
* of text. An item bigger than maxChars gets its own batch.
|
|
7
|
+
*/
|
|
8
|
+
export function makeBatches(items, limits) {
|
|
9
|
+
const batches = [];
|
|
10
|
+
let current = [];
|
|
11
|
+
let size = 0;
|
|
12
|
+
for (const item of items) {
|
|
13
|
+
const s = itemSize(item);
|
|
14
|
+
if (current.length > 0 && (current.length >= limits.maxItems || size + s > limits.maxChars)) {
|
|
15
|
+
batches.push(current);
|
|
16
|
+
current = [];
|
|
17
|
+
size = 0;
|
|
18
|
+
}
|
|
19
|
+
current.push(item);
|
|
20
|
+
size += s;
|
|
21
|
+
}
|
|
22
|
+
if (current.length)
|
|
23
|
+
batches.push(current);
|
|
24
|
+
return batches;
|
|
25
|
+
}
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { RTL_LOCALES, isChromeLocale } from "./locales.js";
|
|
4
|
+
import { compareFormat, summarizeFormat, tokenize } from "./placeholders.js";
|
|
5
|
+
import { listLocaleDirs, messagesPath, readMessagesFile, stripJsonComments } from "./messages.js";
|
|
6
|
+
import { checkListing, listingDir } from "./listing.js";
|
|
7
|
+
import { findStale } from "./stale.js";
|
|
8
|
+
/** Message keys may only use these characters (Chrome i18n docs). */
|
|
9
|
+
export const KEY_RE = /^[A-Za-z0-9_@]+$/;
|
|
10
|
+
/** Manifest fields with Chrome Web Store length limits. Warning level. */
|
|
11
|
+
const MANIFEST_LIMITS = { name: 75, short_name: 12, description: 132 };
|
|
12
|
+
/** Hebrew, Arabic (incl. Persian) script blocks and presentation forms. */
|
|
13
|
+
const RTL_CHAR_RE = /[--ۿݐ-ݿࢠ-ࣿיִ-﷿ﹰ-]/;
|
|
14
|
+
function plainText(message) {
|
|
15
|
+
return tokenize(message)
|
|
16
|
+
.filter((s) => s.kind === "text")
|
|
17
|
+
.map((s) => s.raw)
|
|
18
|
+
.join("");
|
|
19
|
+
}
|
|
20
|
+
function asEntry(value) {
|
|
21
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
22
|
+
}
|
|
23
|
+
/** Checks on key names and message shape that apply to every locale file. */
|
|
24
|
+
function checkEntries(data, locale) {
|
|
25
|
+
const issues = [];
|
|
26
|
+
const err = (key, message) => issues.push({ level: "error", locale, key, message });
|
|
27
|
+
const lowerSeen = new Map();
|
|
28
|
+
for (const [key, value] of Object.entries(data)) {
|
|
29
|
+
if (key.startsWith("@@"))
|
|
30
|
+
err(key, `keys starting with "@@" are reserved by Chrome`);
|
|
31
|
+
else if (!KEY_RE.test(key))
|
|
32
|
+
err(key, `invalid key name; use only A-Z a-z 0-9 _ @`);
|
|
33
|
+
const lower = key.toLowerCase();
|
|
34
|
+
const prev = lowerSeen.get(lower);
|
|
35
|
+
if (prev !== undefined)
|
|
36
|
+
err(key, `duplicate of "${prev}" (keys are case-insensitive)`);
|
|
37
|
+
else
|
|
38
|
+
lowerSeen.set(lower, key);
|
|
39
|
+
const entry = asEntry(value);
|
|
40
|
+
if (!entry) {
|
|
41
|
+
err(key, `value must be an object with a "message" field`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (typeof entry.message !== "string") {
|
|
45
|
+
err(key, `missing "message" string`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (entry.message.trim() === "")
|
|
49
|
+
err(key, `empty message`);
|
|
50
|
+
// Every $name$ used must be defined in this file's own placeholders block.
|
|
51
|
+
const defined = new Set();
|
|
52
|
+
const ph = asEntry(entry.placeholders);
|
|
53
|
+
if (entry.placeholders !== undefined && !ph)
|
|
54
|
+
err(key, `"placeholders" must be an object`);
|
|
55
|
+
if (ph) {
|
|
56
|
+
for (const [name, def] of Object.entries(ph)) {
|
|
57
|
+
defined.add(name.toLowerCase());
|
|
58
|
+
if (!KEY_RE.test(name))
|
|
59
|
+
err(key, `invalid placeholder name "${name}"`);
|
|
60
|
+
const d = asEntry(def);
|
|
61
|
+
if (!d || typeof d.content !== "string") {
|
|
62
|
+
err(key, `placeholder "${name}" needs a "content" string`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const name of summarizeFormat(entry.message).named) {
|
|
67
|
+
if (!defined.has(name))
|
|
68
|
+
err(key, `$${name}$ is used but not defined in "placeholders"`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return issues;
|
|
72
|
+
}
|
|
73
|
+
/** Validate the default locale file on its own. */
|
|
74
|
+
export function checkSource(data, locale) {
|
|
75
|
+
const issues = checkEntries(data, locale);
|
|
76
|
+
for (const [key, value] of Object.entries(data)) {
|
|
77
|
+
const entry = asEntry(value);
|
|
78
|
+
if (!entry || typeof entry.message !== "string")
|
|
79
|
+
continue;
|
|
80
|
+
const s = summarizeFormat(entry.message);
|
|
81
|
+
if (s.lone > 0) {
|
|
82
|
+
issues.push({ level: "warning", locale, key, message: `stray "$" in message; use "$$" for a literal dollar sign` });
|
|
83
|
+
}
|
|
84
|
+
const ph = asEntry(entry.placeholders);
|
|
85
|
+
if (ph) {
|
|
86
|
+
for (const name of Object.keys(ph)) {
|
|
87
|
+
if (!s.named.includes(name.toLowerCase())) {
|
|
88
|
+
issues.push({ level: "warning", locale, key, message: `placeholder "${name}" is defined but not used` });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return issues;
|
|
94
|
+
}
|
|
95
|
+
/** Validate one translated locale against the source. Pure; used by `check` and tests. */
|
|
96
|
+
export function checkLocale(source, target, locale) {
|
|
97
|
+
const issues = checkEntries(target, locale);
|
|
98
|
+
const err = (key, message) => issues.push({ level: "error", locale, key, message });
|
|
99
|
+
const warn = (key, message) => issues.push({ level: "warning", locale, key, message });
|
|
100
|
+
const targetByLower = new Map(Object.keys(target).map((k) => [k.toLowerCase(), k]));
|
|
101
|
+
const sourceLower = new Set(Object.keys(source).map((k) => k.toLowerCase()));
|
|
102
|
+
for (const [key, srcValue] of Object.entries(source)) {
|
|
103
|
+
const tKey = targetByLower.get(key.toLowerCase());
|
|
104
|
+
if (tKey === undefined) {
|
|
105
|
+
err(key, `missing (present in source)`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (tKey !== key)
|
|
109
|
+
warn(key, `key spelled "${tKey}" here; same key for Chrome, but keep the source spelling`);
|
|
110
|
+
const src = asEntry(srcValue);
|
|
111
|
+
const tgt = asEntry(target[tKey]);
|
|
112
|
+
if (!src || typeof src.message !== "string" || !tgt || typeof tgt.message !== "string")
|
|
113
|
+
continue;
|
|
114
|
+
for (const p of compareFormat(src.message, tgt.message))
|
|
115
|
+
(p.severity === "error" ? err : warn)(key, p.message);
|
|
116
|
+
if (RTL_LOCALES.has(locale)) {
|
|
117
|
+
const text = plainText(tgt.message);
|
|
118
|
+
if (/\p{L}/u.test(text) && !RTL_CHAR_RE.test(text)) {
|
|
119
|
+
warn(key, `no right-to-left letters; message may be untranslated`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const key of Object.keys(target)) {
|
|
124
|
+
if (!sourceLower.has(key.toLowerCase()))
|
|
125
|
+
err(key, `extra key (not in source)`);
|
|
126
|
+
}
|
|
127
|
+
return issues;
|
|
128
|
+
}
|
|
129
|
+
/** Look up a __MSG_key__ value in a locale (case-insensitive key). */
|
|
130
|
+
function lookupMessage(data, key) {
|
|
131
|
+
const lower = key.toLowerCase();
|
|
132
|
+
for (const [k, v] of Object.entries(data)) {
|
|
133
|
+
const e = asEntry(v);
|
|
134
|
+
if (k.toLowerCase() === lower && e && typeof e.message === "string")
|
|
135
|
+
return e.message;
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
/** Run every check on an extension folder. */
|
|
140
|
+
export function checkProject(root, opts = {}) {
|
|
141
|
+
const issues = [];
|
|
142
|
+
const listingRel = (opts.listingDir ?? "listing").replace(/\\/g, "/").replace(/\/+$/, "");
|
|
143
|
+
let staleCheck = {
|
|
144
|
+
mode: opts.stale ?? "warning",
|
|
145
|
+
status: "no-source",
|
|
146
|
+
stale: 0,
|
|
147
|
+
untracked: 0,
|
|
148
|
+
note: "The source messages could not be read, so stale strings were not checked.",
|
|
149
|
+
};
|
|
150
|
+
const report = () => {
|
|
151
|
+
annotate(root, issues, listingRel);
|
|
152
|
+
return {
|
|
153
|
+
defaultLocale,
|
|
154
|
+
locales,
|
|
155
|
+
issues,
|
|
156
|
+
errors: issues.filter((i) => i.level === "error").length,
|
|
157
|
+
warnings: issues.filter((i) => i.level === "warning").length,
|
|
158
|
+
stale: issues.filter((i) => i.kind === "stale").length,
|
|
159
|
+
staleCheck,
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
let defaultLocale = null;
|
|
163
|
+
let locales = [];
|
|
164
|
+
const manifestPath = join(root, "manifest.json");
|
|
165
|
+
if (!existsSync(manifestPath)) {
|
|
166
|
+
issues.push({ level: "error", locale: null, message: `no manifest.json in ${root}` });
|
|
167
|
+
return report();
|
|
168
|
+
}
|
|
169
|
+
let manifest;
|
|
170
|
+
try {
|
|
171
|
+
let raw = readFileSync(manifestPath, "utf8");
|
|
172
|
+
if (raw.charCodeAt(0) === 0xfeff)
|
|
173
|
+
raw = raw.slice(1);
|
|
174
|
+
manifest = JSON.parse(stripJsonComments(raw));
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
issues.push({ level: "error", locale: null, message: `manifest.json does not parse: ${e.message}` });
|
|
178
|
+
return report();
|
|
179
|
+
}
|
|
180
|
+
const localesDir = join(root, "_locales");
|
|
181
|
+
locales = listLocaleDirs(localesDir);
|
|
182
|
+
const dl = manifest["default_locale"];
|
|
183
|
+
if (typeof dl !== "string" || !dl) {
|
|
184
|
+
if (locales.length)
|
|
185
|
+
issues.push({ level: "error", locale: null, message: `_locales/ exists but manifest.json has no "default_locale"` });
|
|
186
|
+
return report();
|
|
187
|
+
}
|
|
188
|
+
defaultLocale = dl;
|
|
189
|
+
if (!locales.includes(dl)) {
|
|
190
|
+
issues.push({ level: "error", locale: dl, message: `default_locale "${dl}" has no _locales/${dl}/messages.json` });
|
|
191
|
+
return report();
|
|
192
|
+
}
|
|
193
|
+
const parsed = new Map();
|
|
194
|
+
for (const locale of locales) {
|
|
195
|
+
if (!isChromeLocale(locale)) {
|
|
196
|
+
issues.push({ level: "warning", locale, message: `"${locale}" is not a locale Chrome supports; it will be ignored or rejected` });
|
|
197
|
+
}
|
|
198
|
+
const path = messagesPath(localesDir, locale);
|
|
199
|
+
if (!existsSync(path)) {
|
|
200
|
+
issues.push({ level: "error", locale, message: `folder has no messages.json (Chrome refuses to load the extension)` });
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const r = readMessagesFile(path);
|
|
204
|
+
if (!r.ok) {
|
|
205
|
+
issues.push({ level: "error", locale, message: `${r.error} (Chrome drops this locale)` });
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (r.hadBom)
|
|
209
|
+
issues.push({ level: "warning", locale, message: `file starts with a byte order mark (BOM); save as UTF-8 without BOM` });
|
|
210
|
+
parsed.set(locale, r.data);
|
|
211
|
+
}
|
|
212
|
+
const source = parsed.get(dl);
|
|
213
|
+
if (!source)
|
|
214
|
+
return report();
|
|
215
|
+
issues.push(...checkSource(source, dl));
|
|
216
|
+
// Manifest __MSG_key__ references must exist in the source.
|
|
217
|
+
const manifestText = JSON.stringify(manifest);
|
|
218
|
+
for (const m of manifestText.matchAll(/__MSG_([A-Za-z0-9_@]+)__/g)) {
|
|
219
|
+
const key = m[1];
|
|
220
|
+
if (key.startsWith("@@"))
|
|
221
|
+
continue;
|
|
222
|
+
if (lookupMessage(source, key) === undefined) {
|
|
223
|
+
issues.push({ level: "error", locale: dl, key, message: `manifest.json uses __MSG_${key}__ but the key is missing`, file: "manifest.json" });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const [locale, data] of parsed) {
|
|
227
|
+
if (locale !== dl)
|
|
228
|
+
issues.push(...checkLocale(source, data, locale));
|
|
229
|
+
// Store length limits for localized manifest fields.
|
|
230
|
+
for (const [field, max] of Object.entries(MANIFEST_LIMITS)) {
|
|
231
|
+
const value = manifest[field];
|
|
232
|
+
const ref = typeof value === "string" ? /^__MSG_([A-Za-z0-9_@]+)__$/.exec(value) : null;
|
|
233
|
+
if (!ref)
|
|
234
|
+
continue;
|
|
235
|
+
const text = lookupMessage(data, ref[1]);
|
|
236
|
+
if (text !== undefined && [...text].length > max) {
|
|
237
|
+
issues.push({ level: "warning", locale, key: ref[1], message: `manifest "${field}" is ${[...text].length} chars; the limit is ${max}` });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Store listing (only when the listing folder exists).
|
|
242
|
+
issues.push(...checkListing({ manifest, localesDir, defaultLocale: dl }, listingDir(root, opts.listingDir), opts.stores ?? ["chrome"]));
|
|
243
|
+
// Stale strings (from .localizer/state.json).
|
|
244
|
+
const stale = findStale({ root, manifest, localesDir, defaultLocale: dl }, source, listingRel, opts.stale ?? "warning");
|
|
245
|
+
issues.push(...stale.issues);
|
|
246
|
+
staleCheck = stale.check;
|
|
247
|
+
return report();
|
|
248
|
+
}
|
|
249
|
+
export function formatIssue(i) {
|
|
250
|
+
const where = [i.locale ?? "manifest", i.key].filter(Boolean).join(" > ");
|
|
251
|
+
return `${i.level === "error" ? "ERROR" : "warn "} ${where}: ${i.message}`;
|
|
252
|
+
}
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
// File and line for each issue
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
function escapeRegExp(s) {
|
|
257
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
258
|
+
}
|
|
259
|
+
function lineAt(text, index) {
|
|
260
|
+
let n = 1;
|
|
261
|
+
for (let i = 0; i < index && i < text.length; i++)
|
|
262
|
+
if (text.charCodeAt(i) === 10)
|
|
263
|
+
n++;
|
|
264
|
+
return n;
|
|
265
|
+
}
|
|
266
|
+
/** Fill in `file` (when not set) and `line` (when it can be found) for every issue. */
|
|
267
|
+
export function annotate(root, issues, listingRel) {
|
|
268
|
+
const texts = new Map();
|
|
269
|
+
const read = (file) => {
|
|
270
|
+
if (!texts.has(file)) {
|
|
271
|
+
try {
|
|
272
|
+
texts.set(file, readFileSync(join(root, file), "utf8"));
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
texts.set(file, null);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return texts.get(file) ?? null;
|
|
279
|
+
};
|
|
280
|
+
for (const issue of issues) {
|
|
281
|
+
const isListing = issue.key?.startsWith("listing.") || issue.message.startsWith("listing");
|
|
282
|
+
if (!issue.file) {
|
|
283
|
+
if (issue.locale === null)
|
|
284
|
+
issue.file = "manifest.json";
|
|
285
|
+
else if (isListing)
|
|
286
|
+
issue.file = `${listingRel}/${issue.locale}.json`;
|
|
287
|
+
else
|
|
288
|
+
issue.file = `_locales/${issue.locale}/messages.json`;
|
|
289
|
+
}
|
|
290
|
+
if (issue.line !== undefined)
|
|
291
|
+
continue;
|
|
292
|
+
const text = read(issue.file);
|
|
293
|
+
if (text === null)
|
|
294
|
+
continue;
|
|
295
|
+
// JSON syntax errors: Node reports "(line N column M)" or "at position P".
|
|
296
|
+
const lineMatch = /\(line (\d+) column \d+\)/.exec(issue.message);
|
|
297
|
+
const posMatch = /at position (\d+)/.exec(issue.message);
|
|
298
|
+
if (lineMatch) {
|
|
299
|
+
issue.line = Number(lineMatch[1]);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (posMatch) {
|
|
303
|
+
issue.line = lineAt(text, Number(posMatch[1]));
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (!issue.key)
|
|
307
|
+
continue;
|
|
308
|
+
let re;
|
|
309
|
+
if (issue.file === "manifest.json")
|
|
310
|
+
re = new RegExp(`__MSG_${escapeRegExp(issue.key)}__`, "i");
|
|
311
|
+
else if (issue.key.startsWith("listing."))
|
|
312
|
+
re = new RegExp(`"${escapeRegExp(issue.key.slice(8))}"\\s*:`);
|
|
313
|
+
else
|
|
314
|
+
re = new RegExp(`^\\s*"${escapeRegExp(issue.key)}"\\s*:`, "im");
|
|
315
|
+
const m = re.exec(text);
|
|
316
|
+
if (m)
|
|
317
|
+
issue.line = lineAt(text, m.index + (m[0].length - m[0].trimStart().length));
|
|
318
|
+
}
|
|
319
|
+
}
|