refactor message passing between popup/background

pull/18/head
Kevin Gibbons 1 year ago
parent c7069d90cb
commit fbf1db60bb

@ -172,33 +172,45 @@ async function sendCookies() {
return response; 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) { function handleMessage(request, sender, sendResponse) {
console.log('message background.js listener: ' + JSON.stringify(request)); console.log('message background.js listener got message', request);
if (request.verify === true) { // this function must return the value `true` in chrome to signal the response will be async;
let response = verifyConnection(); // it cannot return a promise
response.then(message => { // so in order to use async/await, we need a wrapper
sendResponse(message); (async () => {
}); switch (request.type) {
} else if (request.youtube) { case 'verify': {
let response = youtubeLink(request.youtube); return await verifyConnection();
response.then(message => { }
sendResponse(message); case 'cookieState': {
}); return await getCookieState();
} else if (request.cookieState) { }
let response = getCookieState(); case 'sendCookie': {
response.then(message => { return await sendCookies();
sendResponse(message); }
}); case 'youtube': {
} else if (request.sendCookie) { // TODO split this up
console.log('backgound: ' + JSON.stringify(request)); return await youtubeLink(request.youtube);
let response = sendCookies(); }
response.then(message => { default: {
sendResponse(message); 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; return true;
} }

@ -15,11 +15,19 @@ function getBrowser() {
return chrome; return chrome;
} }
} else { } else {
console.log('failed to dedect browser'); console.log('failed to detect browser');
throw 'browser detection error'; 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 // store access details
document.getElementById('save-login').addEventListener('click', function () { document.getElementById('save-login').addEventListener('click', function () {
let url = document.getElementById('full-url').value; let url = document.getElementById('full-url').value;
@ -75,7 +83,7 @@ function sendCookie() {
if (checked === false) { if (checked === false) {
return; return;
} }
let sending = browserType.runtime.sendMessage({ sendCookie: true }); let sending = sendMessage({ type: 'sendCookie' });
sending.then(handleResponse, handleError); sending.then(handleResponse, handleError);
} }
@ -89,12 +97,12 @@ function pingBackend() {
} }
function handleError(error) { function handleError(error) {
console.log(`Error: ${error}`); console.log(`Verify got error: ${error}`);
setStatusIcon(false); setStatusIcon(false);
} }
console.log('ping TA server'); console.log('ping TA server');
let sending = browserType.runtime.sendMessage({ verify: true }); let sending = sendMessage({ type: 'verify' });
sending.then(handleResponse, handleError); sending.then(handleResponse, handleError);
} }
@ -118,7 +126,7 @@ function setCookieState() {
} }
console.log('set cookie state'); console.log('set cookie state');
let sending = browserType.runtime.sendMessage({ cookieState: true }); let sending = sendMessage({ type: 'cookieState' });
sending.then(handleResponse, handleError); sending.then(handleResponse, handleError);
document.getElementById('sendCookies').checked = true; document.getElementById('sendCookies').checked = true;
} }

@ -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 = `<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" const downloadIcon = `<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve"> viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<style type="text/css"> <style type="text/css">
@ -362,18 +370,14 @@ function sendUrl(url, action, button) {
function handleError(error) { function handleError(error) {
console.log('error'); console.log('error');
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
buttonError(button);
} }
let payload = { let message = { type: 'youtube', action, url };
youtube: {
url: url,
action: action,
},
};
console.log('youtube link: ' + JSON.stringify(payload)); console.log('youtube link: ' + JSON.stringify(message));
let sending = browserType.runtime.sendMessage(payload); let sending = sendMessage(message);
sending.then(handleResponse, handleError); sending.then(handleResponse, handleError);
} }

Loading…
Cancel
Save