fix: localize popup operation errors

This commit is contained in:
mirivlad 2026-07-14 22:00:24 +08:00
parent 9a96883c87
commit d6c6ca2cc5
5 changed files with 49 additions and 9 deletions

View File

@ -29,7 +29,7 @@ assert.ok(Object.values(ru).every((value) => typeof value === 'string'));
const tEn = i18n.createTranslator({ en, ru }, 'en');
const tRu = i18n.createTranslator({ en, ru }, 'ru');
assert.strictEqual(tRu('status.queued'), 'В очереди до запуска Верстака');
assert.strictEqual(tEn('error.value', { error: 'offline' }), 'Error: offline');
assert.strictEqual(tEn('error.sendCapture'), 'Could not send the capture. Please try again.');
assert.strictEqual(tRu('missing', null, 'Fallback'), 'Fallback');
assert.strictEqual(tRu('missing.key'), 'missing.key');

View File

@ -65,6 +65,8 @@ const elements = {};
});
let savedSettings = null;
let nextRequestError = null;
const technicalWarnings = [];
const initialState = {
settings: {
receiverUrl: 'http://127.0.0.1:47731/api/browser-inbox/v1/captures',
@ -80,6 +82,11 @@ const browser = {
runtime: {
getURL(relativePath) { return `extension://${relativePath}`; },
sendMessage(message) {
if (nextRequestError) {
const error = nextRequestError;
nextRequestError = null;
return Promise.reject(new Error(error));
}
if (message.action === 'getState') return Promise.resolve(initialState);
if (message.action === 'saveSettings') {
savedSettings = message.settings;
@ -107,10 +114,24 @@ function fetchCatalog(url) {
}
const i18nPath = path.join(__dirname, '..', 'shared', 'i18n.js');
const popupPath = path.join(__dirname, '..', 'shared', 'popup', 'popup.js');
const context = vm.createContext({ browser, console, document, Promise, btoa, fetch: fetchCatalog, navigator: { language: 'en-US' } });
const popupSource = fs.readFileSync(popupPath, 'utf8');
assert.equal(/setStatus\(\s*(?:error\b|err\b|String\()/.test(popupSource), false);
const context = vm.createContext({
browser,
console: {
error: console.error,
log: console.log,
warn(...args) { technicalWarnings.push(args.map(String).join(' ')); },
},
document,
Promise,
btoa,
fetch: fetchCatalog,
navigator: { language: 'en-US' },
});
context.globalThis = context;
vm.runInContext(fs.readFileSync(i18nPath, 'utf8'), context, { filename: i18nPath });
vm.runInContext(fs.readFileSync(popupPath, 'utf8'), context, { filename: popupPath });
vm.runInContext(popupSource, context, { filename: popupPath });
async function flush() {
for (let i = 0; i < 16; i += 1) await Promise.resolve();
@ -156,6 +177,14 @@ async function flush() {
assert.strictEqual(savedSettings.language, 'en');
assert.strictEqual(savedSettings.passiveActivityEnabled, true);
assert.deepStrictEqual(Array.from(savedSettings.passiveActivityExcludedDomains), ['youtube.com', 'x.com']);
nextRequestError = '[plugin:verstak.browser-inbox] captures.create failed: receiver unavailable';
elements['capture-page'].click();
await flush();
assert.strictEqual(elements.status.textContent, 'Could not send the capture. Please try again.');
assert.equal(elements.status.textContent.includes('[plugin:'), false);
assert.ok(technicalWarnings.some((message) => message.includes('captures.create failed')));
console.log('browser extension popup localization/settings tests passed');
})().catch((error) => {
console.error(error);

View File

@ -30,7 +30,10 @@
"error.chooseFile": "Choose a file first",
"error.fileTooLarge": "File is too large for browser capture",
"error.invalidReceiverUrl": "Receiver URL must start with http:// or https://",
"error.value": "Error: {error}",
"error.loadState": "Could not load the extension state. Please try again.",
"error.saveSettings": "Could not save settings. Please try again.",
"error.sendCapture": "Could not send the capture. Please try again.",
"error.readFile": "Could not read the file. Choose it again.",
"context.sendPage": "Send page to Verstak",
"context.sendSelection": "Send selection to Verstak",
"context.sendLink": "Send link to Verstak"

View File

@ -30,7 +30,10 @@
"error.chooseFile": "Сначала выберите файл",
"error.fileTooLarge": "Файл слишком велик для отправки из браузера",
"error.invalidReceiverUrl": "URL приёмника должен начинаться с http:// или https://",
"error.value": "Ошибка: {error}",
"error.loadState": "Не удалось загрузить данные расширения. Повторите попытку.",
"error.saveSettings": "Не удалось сохранить настройки. Повторите попытку.",
"error.sendCapture": "Не удалось отправить материал. Повторите попытку.",
"error.readFile": "Не удалось прочитать файл. Выберите его ещё раз.",
"context.sendPage": "Отправить страницу в Верстак",
"context.sendSelection": "Отправить выделение в Верстак",
"context.sendLink": "Отправить ссылку в Верстак"

View File

@ -118,6 +118,11 @@
statusEl.textContent = text;
}
function reportError(key, fallback, error) {
console.warn('[verstak.popup] request failed:', error);
setStatus(t(key, null, fallback));
}
function request(message) {
return Promise.resolve(ext.runtime.sendMessage(message)).then(function (result) {
if (result && result.error) throw new Error(result.error);
@ -148,7 +153,7 @@
applyLocale(state.settings && state.settings.language || 'system');
return state;
}).catch(function (error) {
setStatus(error && error.message ? error.message : String(error));
reportError('error.loadState', 'Could not load the extension state. Please try again.', error);
});
}
@ -174,7 +179,7 @@
if (successMessage) setStatus(t('status.saved', null, 'Saved'));
return state;
}).catch(function (error) {
setStatus(error && error.message ? error.message : String(error));
reportError('error.saveSettings', 'Could not save settings. Please try again.', error);
});
}
@ -188,7 +193,7 @@
setStatus(t('status.done', null, 'Done'));
}
}).catch(function (error) {
setStatus(error && error.message ? error.message : String(error));
reportError('error.sendCapture', 'Could not send the capture. Please try again.', error);
});
}
@ -218,7 +223,7 @@
fileDataBase64: arrayBufferToBase64(results[0])
});
}).catch(function (error) {
setStatus(error && error.message ? error.message : String(error));
reportError('error.readFile', 'Could not read the file. Choose it again.', error);
});
});