2022-04-01 01:40:39 +00:00
|
|
|
/*
|
|
|
|
extension background script listening for events
|
|
|
|
*/
|
|
|
|
|
|
|
|
console.log("running background.js");
|
|
|
|
|
|
|
|
let browserType = getBrowser();
|
|
|
|
|
|
|
|
|
|
|
|
// boilerplate to dedect browser type api
|
|
|
|
function getBrowser() {
|
|
|
|
if (typeof chrome !== "undefined") {
|
|
|
|
if (typeof browser !== "undefined") {
|
|
|
|
return browser;
|
|
|
|
} else {
|
|
|
|
return chrome;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("failed to dedect browser");
|
|
|
|
throw "browser detection error"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// send get request to API backend
|
|
|
|
async function sendGet(path, access) {
|
|
|
|
|
|
|
|
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-01 01:40:39 +00:00
|
|
|
// send post request to API backend
|
2022-04-03 13:54:29 +00:00
|
|
|
async function sendPost(path, access, payload) {
|
|
|
|
|
|
|
|
const url = `${access.url}:${access.port}/${path}`;
|
|
|
|
console.log("POST: " + url);
|
|
|
|
console.log("POST: " + JSON.stringify(payload))
|
2022-04-01 01:40:39 +00:00
|
|
|
|
|
|
|
const rawResponse = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json",
|
2022-04-03 13:54:29 +00:00
|
|
|
"Authorization": "Token " + access.apiKey,
|
2022-04-01 01:40:39 +00:00
|
|
|
"mode": "no-cors"
|
|
|
|
},
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
});
|
|
|
|
|
|
|
|
const content = await rawResponse.json();
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// read access details from storage.local
|
|
|
|
async function getAccess() {
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
var storage = await browserType.storage.local.get("access");
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
return storage.access
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// send ping to server, return response
|
|
|
|
async function verifyConnection() {
|
2022-04-01 01:40:39 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
const path = "api/ping/";
|
|
|
|
let access = await getAccess();
|
|
|
|
let response = await sendGet(path, access)
|
|
|
|
console.log("verify connection: " + JSON.stringify(response));
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// store last youtube link
|
|
|
|
function setYoutubeLink(data) {
|
|
|
|
browserType.storage.local.set(data, function() {
|
|
|
|
console.log("Stored history: " + JSON.stringify(data));
|
|
|
|
});
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
|
|
|
|
// send download task to server, return response
|
|
|
|
async function downloadLink(toDownload) {
|
|
|
|
|
|
|
|
const path = "api/download/";
|
|
|
|
let payload = {
|
|
|
|
"data": [
|
|
|
|
{
|
|
|
|
"youtube_id": toDownload,
|
|
|
|
"status": "pending",
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|
2022-04-03 13:54:29 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
let access = await getAccess();
|
|
|
|
let response = await sendPost(path, access, payload)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-30 11:02:42 +00:00
|
|
|
async function subscribeLink(toSubscribe) {
|
|
|
|
|
|
|
|
const path = "api/channel/";
|
|
|
|
let payload = {
|
|
|
|
"data": [
|
|
|
|
{
|
|
|
|
"channel_id": toSubscribe,
|
|
|
|
"channel_subscribed": true,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
let access = await getAccess();
|
|
|
|
let response = await sendPost(path, access, payload);
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-04-04 04:58:50 +00:00
|
|
|
// process and return message if needed
|
|
|
|
function handleMessage(request, sender, sendResponse) {
|
|
|
|
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) {
|
|
|
|
setYoutubeLink(request)
|
|
|
|
} else if (request.download) {
|
|
|
|
let response = downloadLink(request.download.url);
|
|
|
|
response.then(message => {
|
|
|
|
sendResponse(message)
|
|
|
|
})
|
2022-05-30 11:02:42 +00:00
|
|
|
} else if (request.subscribe) {
|
|
|
|
let response = subscribeLink(request.subscribe.url);
|
|
|
|
response.then(message => {
|
|
|
|
sendResponse(message)
|
|
|
|
})
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|
2022-04-04 04:58:50 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
browserType.runtime.onMessage.addListener(handleMessage);
|