fix: localize popup operation errors
This commit is contained in:
parent
9a96883c87
commit
d6c6ca2cc5
|
|
@ -29,7 +29,7 @@ assert.ok(Object.values(ru).every((value) => typeof value === 'string'));
|
||||||
const tEn = i18n.createTranslator({ en, ru }, 'en');
|
const tEn = i18n.createTranslator({ en, ru }, 'en');
|
||||||
const tRu = i18n.createTranslator({ en, ru }, 'ru');
|
const tRu = i18n.createTranslator({ en, ru }, 'ru');
|
||||||
assert.strictEqual(tRu('status.queued'), 'В очереди до запуска Верстака');
|
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', null, 'Fallback'), 'Fallback');
|
||||||
assert.strictEqual(tRu('missing.key'), 'missing.key');
|
assert.strictEqual(tRu('missing.key'), 'missing.key');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,8 @@ const elements = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
let savedSettings = null;
|
let savedSettings = null;
|
||||||
|
let nextRequestError = null;
|
||||||
|
const technicalWarnings = [];
|
||||||
const initialState = {
|
const initialState = {
|
||||||
settings: {
|
settings: {
|
||||||
receiverUrl: 'http://127.0.0.1:47731/api/browser-inbox/v1/captures',
|
receiverUrl: 'http://127.0.0.1:47731/api/browser-inbox/v1/captures',
|
||||||
|
|
@ -80,6 +82,11 @@ const browser = {
|
||||||
runtime: {
|
runtime: {
|
||||||
getURL(relativePath) { return `extension://${relativePath}`; },
|
getURL(relativePath) { return `extension://${relativePath}`; },
|
||||||
sendMessage(message) {
|
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 === 'getState') return Promise.resolve(initialState);
|
||||||
if (message.action === 'saveSettings') {
|
if (message.action === 'saveSettings') {
|
||||||
savedSettings = message.settings;
|
savedSettings = message.settings;
|
||||||
|
|
@ -107,10 +114,24 @@ function fetchCatalog(url) {
|
||||||
}
|
}
|
||||||
const i18nPath = path.join(__dirname, '..', 'shared', 'i18n.js');
|
const i18nPath = path.join(__dirname, '..', 'shared', 'i18n.js');
|
||||||
const popupPath = path.join(__dirname, '..', 'shared', 'popup', 'popup.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;
|
context.globalThis = context;
|
||||||
vm.runInContext(fs.readFileSync(i18nPath, 'utf8'), context, { filename: i18nPath });
|
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() {
|
async function flush() {
|
||||||
for (let i = 0; i < 16; i += 1) await Promise.resolve();
|
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.language, 'en');
|
||||||
assert.strictEqual(savedSettings.passiveActivityEnabled, true);
|
assert.strictEqual(savedSettings.passiveActivityEnabled, true);
|
||||||
assert.deepStrictEqual(Array.from(savedSettings.passiveActivityExcludedDomains), ['youtube.com', 'x.com']);
|
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');
|
console.log('browser extension popup localization/settings tests passed');
|
||||||
})().catch((error) => {
|
})().catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,10 @@
|
||||||
"error.chooseFile": "Choose a file first",
|
"error.chooseFile": "Choose a file first",
|
||||||
"error.fileTooLarge": "File is too large for browser capture",
|
"error.fileTooLarge": "File is too large for browser capture",
|
||||||
"error.invalidReceiverUrl": "Receiver URL must start with http:// or https://",
|
"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.sendPage": "Send page to Verstak",
|
||||||
"context.sendSelection": "Send selection to Verstak",
|
"context.sendSelection": "Send selection to Verstak",
|
||||||
"context.sendLink": "Send link to Verstak"
|
"context.sendLink": "Send link to Verstak"
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,10 @@
|
||||||
"error.chooseFile": "Сначала выберите файл",
|
"error.chooseFile": "Сначала выберите файл",
|
||||||
"error.fileTooLarge": "Файл слишком велик для отправки из браузера",
|
"error.fileTooLarge": "Файл слишком велик для отправки из браузера",
|
||||||
"error.invalidReceiverUrl": "URL приёмника должен начинаться с http:// или https://",
|
"error.invalidReceiverUrl": "URL приёмника должен начинаться с http:// или https://",
|
||||||
"error.value": "Ошибка: {error}",
|
"error.loadState": "Не удалось загрузить данные расширения. Повторите попытку.",
|
||||||
|
"error.saveSettings": "Не удалось сохранить настройки. Повторите попытку.",
|
||||||
|
"error.sendCapture": "Не удалось отправить материал. Повторите попытку.",
|
||||||
|
"error.readFile": "Не удалось прочитать файл. Выберите его ещё раз.",
|
||||||
"context.sendPage": "Отправить страницу в Верстак",
|
"context.sendPage": "Отправить страницу в Верстак",
|
||||||
"context.sendSelection": "Отправить выделение в Верстак",
|
"context.sendSelection": "Отправить выделение в Верстак",
|
||||||
"context.sendLink": "Отправить ссылку в Верстак"
|
"context.sendLink": "Отправить ссылку в Верстак"
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,11 @@
|
||||||
statusEl.textContent = text;
|
statusEl.textContent = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function reportError(key, fallback, error) {
|
||||||
|
console.warn('[verstak.popup] request failed:', error);
|
||||||
|
setStatus(t(key, null, fallback));
|
||||||
|
}
|
||||||
|
|
||||||
function request(message) {
|
function request(message) {
|
||||||
return Promise.resolve(ext.runtime.sendMessage(message)).then(function (result) {
|
return Promise.resolve(ext.runtime.sendMessage(message)).then(function (result) {
|
||||||
if (result && result.error) throw new Error(result.error);
|
if (result && result.error) throw new Error(result.error);
|
||||||
|
|
@ -148,7 +153,7 @@
|
||||||
applyLocale(state.settings && state.settings.language || 'system');
|
applyLocale(state.settings && state.settings.language || 'system');
|
||||||
return state;
|
return state;
|
||||||
}).catch(function (error) {
|
}).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'));
|
if (successMessage) setStatus(t('status.saved', null, 'Saved'));
|
||||||
return state;
|
return state;
|
||||||
}).catch(function (error) {
|
}).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'));
|
setStatus(t('status.done', null, 'Done'));
|
||||||
}
|
}
|
||||||
}).catch(function (error) {
|
}).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])
|
fileDataBase64: arrayBufferToBase64(results[0])
|
||||||
});
|
});
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
setStatus(error && error.message ? error.message : String(error));
|
reportError('error.readFile', 'Could not read the file. Choose it again.', error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue