diff --git a/extension/background.js b/extension/background.js index 745d6ef..a41d4b1 100644 --- a/extension/background.js +++ b/extension/background.js @@ -172,33 +172,45 @@ async function sendCookies() { return response; } -// process and return message if needed +/* +process and return message if needed +the following messages are supported: +type Message = + | { type: 'verify' } + | { type: 'cookieState' } + | { type: 'sendCookie' } + | { type: 'youtube', action: 'download' | 'subscribe', url: string } +*/ function handleMessage(request, sender, sendResponse) { - console.log('message background.js listener: ' + JSON.stringify(request)); - - if (request.verify === true) { - let response = verifyConnection(); - response.then(message => { - sendResponse(message); - }); - } else if (request.youtube) { - let response = youtubeLink(request.youtube); - response.then(message => { - sendResponse(message); - }); - } else if (request.cookieState) { - let response = getCookieState(); - response.then(message => { - sendResponse(message); - }); - } else if (request.sendCookie) { - console.log('backgound: ' + JSON.stringify(request)); - let response = sendCookies(); - response.then(message => { - sendResponse(message); - }); - } - + console.log('message background.js listener got message', request); + + // this function must return the value `true` in chrome to signal the response will be async; + // it cannot return a promise + // so in order to use async/await, we need a wrapper + (async () => { + switch (request.type) { + case 'verify': { + return await verifyConnection(); + } + case 'cookieState': { + return await getCookieState(); + } + case 'sendCookie': { + return await sendCookies(); + } + case 'youtube': { + // TODO split this up + return await youtubeLink(request.youtube); + } + default: { + let err = new Error(`unknown message type ${JSON.stringify(request.type)}`); + console.log(err); + throw err; + } + } + })() + .then(value => sendResponse({ success: true, value })) + .catch(e => sendResponse({ success: false, value: e.message })); return true; } diff --git a/extension/popup.js b/extension/popup.js index 9e591d5..c2f19cd 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -15,11 +15,19 @@ function getBrowser() { return chrome; } } else { - console.log('failed to dedect browser'); + console.log('failed to detect browser'); throw 'browser detection error'; } } +async function sendMessage(message) { + let { success, value } = await browserType.runtime.sendMessage(message); + if (!success) { + throw value; + } + return value; +} + // store access details document.getElementById('save-login').addEventListener('click', function () { let url = document.getElementById('full-url').value; @@ -75,7 +83,7 @@ function sendCookie() { if (checked === false) { return; } - let sending = browserType.runtime.sendMessage({ sendCookie: true }); + let sending = sendMessage({ type: 'sendCookie' }); sending.then(handleResponse, handleError); } @@ -89,12 +97,12 @@ function pingBackend() { } function handleError(error) { - console.log(`Error: ${error}`); + console.log(`Verify got error: ${error}`); setStatusIcon(false); } console.log('ping TA server'); - let sending = browserType.runtime.sendMessage({ verify: true }); + let sending = sendMessage({ type: 'verify' }); sending.then(handleResponse, handleError); } @@ -118,7 +126,7 @@ function setCookieState() { } console.log('set cookie state'); - let sending = browserType.runtime.sendMessage({ cookieState: true }); + let sending = sendMessage({ type: 'cookieState' }); sending.then(handleResponse, handleError); document.getElementById('sendCookies').checked = true; } diff --git a/extension/script.js b/extension/script.js index 7cbaf55..34388ef 100644 --- a/extension/script.js +++ b/extension/script.js @@ -22,6 +22,14 @@ function getBrowser() { } } +async function sendMessage(message) { + let { success, value } = await browserType.runtime.sendMessage(message); + if (!success) { + throw value; + } + return value; +} + const downloadIcon = `