feat: automatically clear non-critical error notifications

Closes #253.
This commit is contained in:
dessant 2021-04-29 22:44:21 +03:00
parent 9d092c2c39
commit ab03085b0e
2 changed files with 36 additions and 11 deletions

View File

@ -287,6 +287,7 @@ async function getWitSpeechApiResult(apiKey, audioContent) {
if (rsp.status !== 200) {
if (rsp.status === 429) {
result.errorId = 'error_apiQuotaExceeded';
result.errorTimeout = 6000;
} else {
throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`);
}
@ -377,7 +378,10 @@ async function transcribeAudio(audioUrl, lang) {
const result = await getWitSpeechApiResult(apiKey, audioContent);
if (result.errorId) {
showNotification({messageId: result.errorId});
showNotification({
messageId: result.errorId,
timeout: result.errorTimeout
});
return;
}
solution = result.text;
@ -390,7 +394,10 @@ async function transcribeAudio(audioUrl, lang) {
}
const result = await getWitSpeechApiResult(apiKey, audioContent);
if (result.errorId) {
showNotification({messageId: result.errorId});
showNotification({
messageId: result.errorId,
timeout: result.errorTimeout
});
return;
}
solution = result.text;
@ -506,7 +513,7 @@ async function transcribeAudio(audioUrl, lang) {
}
if (!solution) {
showNotification({messageId: 'error_captchaNotSolved'});
showNotification({messageId: 'error_captchaNotSolved', timeout: 6000});
} else {
return solution;
}
@ -518,7 +525,8 @@ async function onMessage(request, sender) {
message: request.message,
messageId: request.messageId,
title: request.title,
type: request.type
type: request.type,
timeout: request.timeout
});
} else if (request.id === 'captchaSolved') {
let {useCount} = await storage.get('useCount', 'sync');

View File

@ -9,19 +9,36 @@ import {
sleep
} from 'utils/common';
function showNotification({message, messageId, title, type = 'info'}) {
async function showNotification({
message,
messageId,
title,
type = 'info',
timeout = 0
}) {
if (!title) {
title = getText('extensionName');
}
if (messageId) {
message = getText(messageId);
}
return browser.notifications.create(`sbi-notification-${type}`, {
type: 'basic',
title: title,
message: message,
iconUrl: '/src/icons/app/icon-48.png'
});
const notification = await browser.notifications.create(
`bc-notification-${type}`,
{
type: 'basic',
title,
message,
iconUrl: '/src/icons/app/icon-64.png'
}
);
if (timeout) {
window.setTimeout(() => {
browser.notifications.clear(notification);
}, timeout);
}
return notification;
}
function getOptionLabels(data, scope = 'optionValue') {