sendUrl from injected button

This commit is contained in:
simon 2022-11-24 08:36:22 +07:00
parent 2485715818
commit a0e4a10f1f
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 35 additions and 58 deletions

View File

@ -101,47 +101,36 @@ async function verifyConnection() {
}
// store last youtube link
function setYoutubeLink(data) {
browserType.storage.local.set(data, function() {
console.log("Stored history: " + JSON.stringify(data));
});
}
// send youtube link from injected buttons
async function youtubeLink(youtubeMessage) {
let path;
let payload;
// send download task to server, return response
async function downloadLink(toDownload) {
const path = "api/download/";
let payload = {
"data": [
{
"youtube_id": toDownload,
"status": "pending",
}
]
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,
}
]
}
}
let response = await sendData(path, payload, "POST")
return response
}
async function subscribeLink(toSubscribe) {
const path = "api/channel/";
let payload = {
"data": [
{
"channel_id": toSubscribe,
"channel_subscribed": true,
}
]
}
let response = await sendData(path, payload, "POST");
return response
}
@ -210,14 +199,7 @@ function handleMessage(request, sender, sendResponse) {
sendResponse(message);
})
} else if (request.youtube) {
setYoutubeLink(request)
} else if (request.download) {
let response = downloadLink(request.download.url);
response.then(message => {
sendResponse(message)
})
} else if (request.subscribe) {
let response = subscribeLink(request.subscribe.url);
let response = youtubeLink(request.youtube);
response.then(message => {
sendResponse(message)
})

View File

@ -126,7 +126,8 @@ function buildSubLink(channelContainer) {
subLink.innerText = "Subscribe";
subLink.addEventListener('click', e => {
e.preventDefault();
console.log("subscribe to: " + currentLocation)
console.log("subscribe to: " + currentLocation);
sendUrl(currentLocation, "subscribe");
});
subLink.addEventListener("mouseover", e => {
let subText
@ -161,6 +162,7 @@ function buildDlLink(channelContainer) {
dlLink.addEventListener('click', e => {
e.preventDefault();
console.log("download: " + currentLocation)
sendUrl(currentLocation, "download");
});
dlLink.addEventListener("mouseover", e => {
let channelName = channelContainer.querySelector("#text").textContent;
@ -217,6 +219,7 @@ function buildVideoButton(thumbContainer) {
e.preventDefault();
let videoLink = thumbContainer.href;
console.log("download: " + videoLink);
sendUrl(videoLink, "download");
});
dlButton.addEventListener('mouseover', e => {
Object.assign(dlButton.style, {
@ -286,26 +289,18 @@ function ensureTALinks() {
}
function sendUrl() {
let url = document.URL
let urlType = detectUrlType(url);
if (urlType == false) {
console.log("not relevant")
return
}
function sendUrl(url, action) {
let payload = {
"youtube": {
"url": url,
"title": document.title,
"type": urlType,
"action": action,
}
}
console.log("youtube link: " + JSON.stringify(payload));
browserType.runtime.sendMessage(payload, function(response) {
console.log(response.farewell);
console.log("sendUrl response: " + JSON.stringify(response))
});
};