split youtube message

This commit is contained in:
Kevin Gibbons 2023-02-11 16:47:59 -08:00
parent ce827efb05
commit 5a59c4c80b
3 changed files with 28 additions and 25 deletions

View File

@ -84,7 +84,7 @@ async function getCookieState() {
return response; return response;
} }
// send ping to server, return response // send ping to server
async function verifyConnection() { async function verifyConnection() {
const path = 'api/ping/'; const path = 'api/ping/';
let message = await sendGet(path); let message = await sendGet(path);
@ -100,34 +100,34 @@ async function verifyConnection() {
} }
// send youtube link from injected buttons // send youtube link from injected buttons
async function youtubeLink(youtubeMessage) { async function download(url) {
let path; return await sendData(
let payload; 'api/download/',
{
if (youtubeMessage.action === 'download') {
path = 'api/download/';
payload = {
data: [ data: [
{ {
youtube_id: youtubeMessage.url, youtube_id: url,
status: 'pending', status: 'pending',
}, },
], ],
}; },
} else if (youtubeMessage.action === 'subscribe') { 'POST'
path = 'api/channel/'; );
payload = { }
async function subscribe(url) {
return await sendData(
'api/channel/',
{
data: [ data: [
{ {
channel_id: youtubeMessage.url, channel_id: url,
channel_subscribed: true, channel_subscribed: true,
}, },
], ],
}; },
} 'POST'
);
let response = await sendData(path, payload, 'POST');
return response;
} }
async function cookieStr(cookieLines) { async function cookieStr(cookieLines) {
@ -185,7 +185,8 @@ type Message =
| { type: 'verify' } | { type: 'verify' }
| { type: 'cookieState' } | { type: 'cookieState' }
| { type: 'sendCookie' } | { type: 'sendCookie' }
| { type: 'youtube', action: 'download' | 'subscribe', url: string } | { type: 'download', url: string }
| { type: 'subscribe', url: string }
*/ */
function handleMessage(request, sender, sendResponse) { function handleMessage(request, sender, sendResponse) {
console.log('message background.js listener got message', request); console.log('message background.js listener got message', request);
@ -204,9 +205,11 @@ function handleMessage(request, sender, sendResponse) {
case 'sendCookie': { case 'sendCookie': {
return await sendCookies(); return await sendCookies();
} }
case 'youtube': { case 'download': {
// TODO split this up return await download(request.url);
return await youtubeLink(request.youtube); }
case 'subscribe': {
return await subscribe(request.url);
} }
default: { default: {
let err = new Error(`unknown message type ${JSON.stringify(request.type)}`); let err = new Error(`unknown message type ${JSON.stringify(request.type)}`);

View File

@ -102,7 +102,7 @@ function sendCookie() {
// send ping message to TA backend // send ping message to TA backend
function pingBackend() { function pingBackend() {
clearError(); clearError();
function handleResponse(message) { function handleResponse() {
console.log('connection validated'); console.log('connection validated');
setStatusIcon(true); setStatusIcon(true);
} }

View File

@ -373,7 +373,7 @@ function sendUrl(url, action, button) {
buttonError(button); buttonError(button);
} }
let message = { type: 'youtube', action, url }; let message = { type: action, url };
console.log('youtube link: ' + JSON.stringify(message)); console.log('youtube link: ' + JSON.stringify(message));