Skip to content

Commit

Permalink
feat: automatically clear non-critical error notifications
Browse files Browse the repository at this point in the history
Closes #253.
  • Loading branch information
dessant committed Apr 29, 2021
1 parent 9d092c2 commit ab03085
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -506,7 +513,7 @@ async function transcribeAudio(audioUrl, lang) {
}

if (!solution) {
showNotification({messageId: 'error_captchaNotSolved'});
showNotification({messageId: 'error_captchaNotSolved', timeout: 6000});
} else {
return solution;
}
Expand All @@ -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');
Expand Down
31 changes: 24 additions & 7 deletions src/utils/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit ab03085

Please sign in to comment.