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;
}
// 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)}`);

View File

@ -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);
}

View File

@ -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));