From 5a59c4c80bf48a3705dde198a187ee01a7f24a78 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Sat, 11 Feb 2023 16:47:59 -0800 Subject: [PATCH] split youtube message --- extension/background.js | 49 ++++++++++++++++++++++------------------- extension/popup.js | 2 +- extension/script.js | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/extension/background.js b/extension/background.js index fda5825..43a6cd6 100644 --- a/extension/background.js +++ b/extension/background.js @@ -84,7 +84,7 @@ async function getCookieState() { return response; } -// send ping to server, return response +// send ping to server async function verifyConnection() { const path = 'api/ping/'; let message = await sendGet(path); @@ -100,34 +100,34 @@ async function verifyConnection() { } // send youtube link from injected buttons -async function youtubeLink(youtubeMessage) { - let path; - let payload; - - if (youtubeMessage.action === 'download') { - path = 'api/download/'; - payload = { +async function download(url) { + return await sendData( + 'api/download/', + { data: [ { - youtube_id: youtubeMessage.url, + youtube_id: url, status: 'pending', }, ], - }; - } else if (youtubeMessage.action === 'subscribe') { - path = 'api/channel/'; - payload = { + }, + 'POST' + ); +} + +async function subscribe(url) { + return await sendData( + 'api/channel/', + { data: [ { - channel_id: youtubeMessage.url, + channel_id: url, channel_subscribed: true, }, ], - }; - } - - let response = await sendData(path, payload, 'POST'); - return response; + }, + 'POST' + ); } async function cookieStr(cookieLines) { @@ -185,7 +185,8 @@ type Message = | { type: 'verify' } | { type: 'cookieState' } | { type: 'sendCookie' } - | { type: 'youtube', action: 'download' | 'subscribe', url: string } + | { type: 'download', url: string } + | { type: 'subscribe', url: string } */ function handleMessage(request, sender, sendResponse) { console.log('message background.js listener got message', request); @@ -204,9 +205,11 @@ function handleMessage(request, sender, sendResponse) { case 'sendCookie': { return await sendCookies(); } - case 'youtube': { - // TODO split this up - return await youtubeLink(request.youtube); + case 'download': { + return await download(request.url); + } + case 'subscribe': { + return await subscribe(request.url); } default: { let err = new Error(`unknown message type ${JSON.stringify(request.type)}`); diff --git a/extension/popup.js b/extension/popup.js index 68fc3fc..35a032c 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -102,7 +102,7 @@ function sendCookie() { // send ping message to TA backend function pingBackend() { clearError(); - function handleResponse(message) { + function handleResponse() { console.log('connection validated'); setStatusIcon(true); } diff --git a/extension/script.js b/extension/script.js index 34388ef..c9a511b 100644 --- a/extension/script.js +++ b/extension/script.js @@ -373,7 +373,7 @@ function sendUrl(url, action, button) { buttonError(button); } - let message = { type: 'youtube', action, url }; + let message = { type: action, url }; console.log('youtube link: ' + JSON.stringify(message));