2022-04-01 01:40:39 +00:00
|
|
|
/*
|
|
|
|
extension background script listening for events
|
|
|
|
*/
|
|
|
|
|
2022-12-03 02:57:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
console.log('running background.js');
|
2022-04-01 01:40:39 +00:00
|
|
|
|
|
|
|
let browserType = getBrowser();
|
|
|
|
|
|
|
|
// boilerplate to dedect browser type api
|
|
|
|
function getBrowser() {
|
2022-12-03 02:51:15 +00:00
|
|
|
if (typeof chrome !== 'undefined') {
|
|
|
|
if (typeof browser !== 'undefined') {
|
|
|
|
return browser;
|
2022-04-01 01:40:39 +00:00
|
|
|
} else {
|
2022-12-03 02:51:15 +00:00
|
|
|
return chrome;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('failed to detect browser');
|
|
|
|
throw 'browser detection error';
|
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// send get request to API backend
|
2022-06-20 11:40:17 +00:00
|
|
|
async function sendGet(path) {
|
2022-12-03 02:51:15 +00:00
|
|
|
let access = await getAccess();
|
|
|
|
const url = `${access.url}:${access.port}/${path}`;
|
|
|
|
console.log('GET: ' + url);
|
|
|
|
|
|
|
|
const rawResponse = await fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: 'Token ' + access.apiKey,
|
|
|
|
mode: 'no-cors',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const content = await rawResponse.json();
|
|
|
|
return content;
|
|
|
|
}
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
// send post/put request to API backend
|
|
|
|
async function sendData(path, payload, method) {
|
|
|
|
let access = await getAccess();
|
|
|
|
const url = `${access.url}:${access.port}/${path}`;
|
|
|
|
console.log(`${method}: ${url}`);
|
|
|
|
console.log(`${method}: ${JSON.stringify(payload)}`);
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
try {
|
2022-04-03 13:54:29 +00:00
|
|
|
const rawResponse = await fetch(url, {
|
2022-12-03 02:51:15 +00:00
|
|
|
method: method,
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: 'Token ' + access.apiKey,
|
|
|
|
mode: 'no-cors',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(payload),
|
2022-04-03 13:54:29 +00:00
|
|
|
});
|
|
|
|
const content = await rawResponse.json();
|
|
|
|
return content;
|
2022-12-03 02:51:15 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-03 13:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read access details from storage.local
|
|
|
|
async function getAccess() {
|
2022-12-03 02:52:33 +00:00
|
|
|
let storage = await browserType.storage.local.get('access');
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return storage.access;
|
2022-04-03 13:54:29 +00:00
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-06-25 13:15:19 +00:00
|
|
|
// check if cookie is valid
|
|
|
|
async function getCookieState() {
|
2022-12-03 02:51:15 +00:00
|
|
|
const path = 'api/cookie/';
|
|
|
|
let response = await sendGet(path);
|
|
|
|
console.log('cookie state: ' + JSON.stringify(response));
|
2022-06-25 13:15:19 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return response;
|
2022-06-25 13:15:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// send ping to server, return response
|
|
|
|
async function verifyConnection() {
|
2022-12-03 02:51:15 +00:00
|
|
|
const path = 'api/ping/';
|
|
|
|
let response = await sendGet(path);
|
|
|
|
console.log('verify connection: ' + JSON.stringify(response));
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return response;
|
2022-04-03 13:54:29 +00:00
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-11-24 01:36:22 +00:00
|
|
|
// send youtube link from injected buttons
|
|
|
|
async function youtubeLink(youtubeMessage) {
|
2022-12-03 02:51:15 +00:00
|
|
|
let path;
|
|
|
|
let payload;
|
|
|
|
|
|
|
|
if (youtubeMessage.action === 'download') {
|
|
|
|
path = 'api/download/';
|
|
|
|
payload = {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
youtube_id: youtubeMessage.url,
|
|
|
|
status: 'pending',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
} else if (youtubeMessage.action === 'subscribe') {
|
|
|
|
path = 'api/channel/';
|
|
|
|
payload = {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
channel_id: youtubeMessage.url,
|
|
|
|
channel_subscribed: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
let response = await sendData(path, payload, 'POST');
|
|
|
|
return response;
|
2022-05-30 11:02:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:46:37 +00:00
|
|
|
async function cookieStr(cookieLines) {
|
2022-12-03 02:51:15 +00:00
|
|
|
const path = 'api/cookie/';
|
|
|
|
let payload = {
|
|
|
|
cookie: cookieLines.join('\n'),
|
|
|
|
};
|
|
|
|
let response = await sendData(path, payload, 'PUT');
|
2022-06-22 12:46:37 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return response;
|
2022-06-22 12:46:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 10:35:57 +00:00
|
|
|
function buildCookieLine(cookie) {
|
2022-12-03 02:51:15 +00:00
|
|
|
return [
|
|
|
|
cookie.domain,
|
|
|
|
'TRUE',
|
|
|
|
cookie.path,
|
|
|
|
cookie.httpOnly.toString().toUpperCase(),
|
|
|
|
Math.trunc(cookie.expirationDate) || 0,
|
|
|
|
cookie.name,
|
|
|
|
cookie.value,
|
|
|
|
].join('\t');
|
2022-06-20 10:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function sendCookies() {
|
2022-12-03 02:51:15 +00:00
|
|
|
console.log('function sendCookies');
|
|
|
|
|
|
|
|
let cookieStores = await browserType.cookies.getAllCookieStores();
|
2022-12-03 02:52:33 +00:00
|
|
|
let cookieLines = [
|
2022-12-03 02:51:15 +00:00
|
|
|
'# Netscape HTTP Cookie File',
|
|
|
|
'# https://curl.haxx.se/rfc/cookie_spec.html',
|
|
|
|
'# This is a generated file! Do not edit.\n',
|
|
|
|
];
|
|
|
|
for (let i = 0; i < cookieStores.length; i++) {
|
|
|
|
const cookieStore = cookieStores[i];
|
2022-12-03 02:52:33 +00:00
|
|
|
let allCookiesStore = await browserType.cookies.getAll({
|
2022-12-03 02:51:15 +00:00
|
|
|
domain: '.youtube.com',
|
|
|
|
storeId: cookieStore['id'],
|
|
|
|
});
|
|
|
|
for (let j = 0; j < allCookiesStore.length; j++) {
|
|
|
|
const cookie = allCookiesStore[j];
|
|
|
|
cookieLines.push(buildCookieLine(cookie));
|
2022-06-20 10:35:57 +00:00
|
|
|
}
|
2022-12-03 02:51:15 +00:00
|
|
|
}
|
2022-06-22 12:46:37 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
let response = cookieStr(cookieLines);
|
2022-06-20 10:35:57 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return response;
|
2022-06-20 10:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 04:58:50 +00:00
|
|
|
// process and return message if needed
|
|
|
|
function handleMessage(request, sender, sendResponse) {
|
2022-12-03 02:51:15 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2022-04-04 04:58:50 +00:00
|
|
|
|
2022-12-03 02:51:15 +00:00
|
|
|
return true;
|
2022-04-04 04:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
browserType.runtime.onMessage.addListener(handleMessage);
|