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

View File

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