diff --git a/extension/background.js b/extension/background.js index f10a1a8..8068e5f 100644 --- a/extension/background.js +++ b/extension/background.js @@ -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 download task to server, return response -async function downloadLink(toDownload) { - - const path = "api/download/"; - let payload = { - "data": [ - { - "youtube_id": toDownload, - "status": "pending", - } - ] +// send youtube link from injected buttons +async function youtubeLink(youtubeMessage) { + + 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, + } + ] + } } - 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) }) diff --git a/extension/script.js b/extension/script.js index 152aefe..8925832 100644 --- a/extension/script.js +++ b/extension/script.js @@ -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)) }); };