From 0c9a8248bfc8796b110f887787a589ccb720b6b3 Mon Sep 17 00:00:00 2001 From: dessant Date: Sat, 8 Dec 2018 23:09:09 +0200 Subject: [PATCH] fix: set IBM API location instead of URL --- src/_locales/en/messages.json | 40 ++++++++++++++++++------- src/options/App.vue | 24 ++++++++------- src/solve/main.js | 35 ++++++++++------------ src/storage/versions/local/ONiJBs00o.js | 4 +-- src/storage/versions/sync/ONiJBs00o.js | 4 +-- src/utils/data.js | 18 +++++++++-- 6 files changed, 80 insertions(+), 45 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index eb32f85..5081797 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -34,9 +34,34 @@ "description": "Value of the option." }, - "inputLabel_url": { - "message": "URL", - "description": "Placeholder of the input." + "optionTitle_ibmSpeechApiLoc": { + "message": "API location", + "description": "Title of the option." + }, + + "optionValue_ibmSpeechApiLoc_frankfurt": { + "message": "Frankfurt", + "description": "Value of the option." + }, + + "optionValue_ibmSpeechApiLoc_dallas": { + "message": "Dallas", + "description": "Value of the option." + }, + + "optionValue_ibmSpeechApiLoc_washington": { + "message": "Washington DC", + "description": "Value of the option." + }, + + "optionValue_ibmSpeechApiLoc_sydney": { + "message": "Sydney", + "description": "Value of the option." + }, + + "optionValue_ibmSpeechApiLoc_tokyo": { + "message": "Tokyo", + "description": "Value of the option." }, "inputLabel_apiKey": { @@ -80,12 +105,6 @@ "description": "Error message." }, - "error_missingApiUrl": { - "message": - "API URL missing. Visit the options page to configure the service.", - "description": "Error message." - }, - "error_missingApiKey": { "message": "API key missing. Visit the options page to configure the service.", @@ -93,7 +112,8 @@ }, "error_internalError": { - "message": "Something went wrong.", + "message": + "Something went wrong. Open the browser console for more details.", "description": "Error message." } } diff --git a/src/options/App.vue b/src/options/App.vue index 2b802cb..b2e7942 100644 --- a/src/options/App.vue +++ b/src/options/App.vue @@ -17,11 +17,12 @@ :label="getText('inputLabel_apiKey')"> -
- - + +
@@ -58,13 +59,20 @@ export default { 'googleSpeechApiDemo', 'googleSpeechApi', 'ibmSpeechApi' + ], + ibmSpeechApiLoc: [ + 'frankfurt', + 'dallas', + 'washington', + 'sydney', + 'tokyo' ] }), options: { speechService: '', googleSpeechApiKey: '', - ibmSpeechApiUrl: '', + ibmSpeechApiLoc: '', ibmSpeechApiKey: '' } }; @@ -80,11 +88,7 @@ export default { for (const option of Object.keys(this.options)) { this.options[option] = options[option]; this.$watch(`options.${option}`, async function(value) { - if ( - ['googleSpeechApiKey', 'ibmSpeechApiUrl', 'ibmSpeechApiKey'].includes( - option - ) - ) { + if (['googleSpeechApiKey', 'ibmSpeechApiKey'].includes(option)) { value = value.trim(); } await storage.set({[option]: value}, 'sync'); diff --git a/src/solve/main.js b/src/solve/main.js index dfd708f..7a4eec9 100644 --- a/src/solve/main.js +++ b/src/solve/main.js @@ -5,7 +5,8 @@ import storage from 'storage/storage'; import {getText, waitForElement, arrayBufferToBase64} from 'utils/common'; import { captchaGoogleSpeechApiLangCodes, - captchaIbmSpeechApiLangCodes + captchaIbmSpeechApiLangCodes, + ibmSpeechApiUrls } from 'utils/data'; let solverWorking = false; @@ -188,16 +189,9 @@ async function solve() { if (speechService === 'ibmSpeechApi') { const { - ibmSpeechApiUrl: apiUrl, + ibmSpeechApiLoc: apiLoc, ibmSpeechApiKey: apiKey - } = await storage.get(['ibmSpeechApiUrl', 'ibmSpeechApiKey'], 'sync'); - if (!apiUrl) { - browser.runtime.sendMessage({ - id: 'notification', - messageId: 'error_missingApiUrl' - }); - return; - } + } = await storage.get(['ibmSpeechApiLoc', 'ibmSpeechApiKey'], 'sync'); if (!apiKey) { browser.runtime.sendMessage({ id: 'notification', @@ -207,15 +201,18 @@ async function solve() { } const model = captchaIbmSpeechApiLangCodes[lang] || 'en-US_BroadbandModel'; - const rsp = await fetch(`${apiUrl}?model=${model}&profanity_filter=false`, { - referrer: '', - mode: 'cors', - method: 'POST', - headers: { - Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey) - }, - body: new Blob([audioContent], {type: 'audio/wav'}) - }); + const rsp = await fetch( + `${ibmSpeechApiUrls[apiLoc]}?model=${model}&profanity_filter=false`, + { + referrer: '', + mode: 'cors', + method: 'POST', + headers: { + Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey) + }, + body: new Blob([audioContent], {type: 'audio/wav'}) + } + ); if (rsp.status !== 200) { throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`); diff --git a/src/storage/versions/local/ONiJBs00o.js b/src/storage/versions/local/ONiJBs00o.js index f5ad1eb..2897fa4 100644 --- a/src/storage/versions/local/ONiJBs00o.js +++ b/src/storage/versions/local/ONiJBs00o.js @@ -9,7 +9,7 @@ const storage = browser.storage.local; async function upgrade() { const changes = { - ibmSpeechApiUrl: '', + ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo ibmSpeechApiKey: '' }; @@ -19,7 +19,7 @@ async function upgrade() { async function downgrade() { const changes = {}; - await storage.remove(['ibmSpeechApiUrl', 'ibmSpeechApiKey']); + await storage.remove(['ibmSpeechApiLoc', 'ibmSpeechApiKey']); changes.storageVersion = downRevision; return storage.set(changes); diff --git a/src/storage/versions/sync/ONiJBs00o.js b/src/storage/versions/sync/ONiJBs00o.js index b667b61..758f989 100644 --- a/src/storage/versions/sync/ONiJBs00o.js +++ b/src/storage/versions/sync/ONiJBs00o.js @@ -9,7 +9,7 @@ const storage = browser.storage.sync; async function upgrade() { const changes = { - ibmSpeechApiUrl: '', + ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo ibmSpeechApiKey: '' }; @@ -19,7 +19,7 @@ async function upgrade() { async function downgrade() { const changes = {}; - await storage.remove(['ibmSpeechApiUrl', 'ibmSpeechApiKey']); + await storage.remove(['ibmSpeechApiLoc', 'ibmSpeechApiKey']); changes.storageVersion = downRevision; return storage.set(changes); diff --git a/src/utils/data.js b/src/utils/data.js index 28d2b5a..fdffe1c 100755 --- a/src/utils/data.js +++ b/src/utils/data.js @@ -3,7 +3,7 @@ import browser from 'webextension-polyfill'; const optionKeys = [ 'speechService', 'googleSpeechApiKey', - 'ibmSpeechApiUrl', + 'ibmSpeechApiLoc', 'ibmSpeechApiKey' ]; @@ -103,8 +103,22 @@ const captchaIbmSpeechApiLangCodes = { 'es-419': 'es-ES_BroadbandModel' }; +// https://cloud.ibm.com/apidocs/speech-to-text#service-endpoint +const ibmSpeechApiUrls = { + frankfurt: + 'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize', + dallas: 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize', + washington: + 'https://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize', + sydney: + 'https://gateway-syd.watsonplatform.net/speech-to-text/api/v1/recognize', + tokyo: + 'https://gateway-tok.watsonplatform.net/speech-to-text/api/v1/recognize' +}; + export { optionKeys, captchaGoogleSpeechApiLangCodes, - captchaIbmSpeechApiLangCodes + captchaIbmSpeechApiLangCodes, + ibmSpeechApiUrls };