From ab03085b0e156aa0bfccf6448222a003f2690386 Mon Sep 17 00:00:00 2001 From: dessant Date: Thu, 29 Apr 2021 22:44:21 +0300 Subject: [PATCH] feat: automatically clear non-critical error notifications Closes #253. --- src/background/main.js | 16 ++++++++++++---- src/utils/app.js | 31 ++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/background/main.js b/src/background/main.js index ce9bc75..ca8b589 100644 --- a/src/background/main.js +++ b/src/background/main.js @@ -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'); diff --git a/src/utils/app.js b/src/utils/app.js index aac2539..f3501f9 100755 --- a/src/utils/app.js +++ b/src/utils/app.js @@ -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') {