From 91e73455120e6fbf87d26a3d4ca31acd84c3c4e8 Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 30 May 2022 18:02:42 +0700 Subject: [PATCH] add subscribe button with video url --- extension/background.js | 23 +++++++++++++++ extension/popup.js | 63 ++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/extension/background.js b/extension/background.js index 7345256..d333dc5 100644 --- a/extension/background.js +++ b/extension/background.js @@ -115,6 +115,24 @@ async function downloadLink(toDownload) { } +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 + +} + // process and return message if needed function handleMessage(request, sender, sendResponse) { @@ -132,6 +150,11 @@ function handleMessage(request, sender, sendResponse) { response.then(message => { sendResponse(message) }) + } else if (request.subscribe) { + let response = subscribeLink(request.subscribe.url); + response.then(message => { + sendResponse(message) + }) } return true; diff --git a/extension/popup.js b/extension/popup.js index 32dfa83..16d7eb4 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -78,8 +78,7 @@ function setStatusIcon(connected) { } -// download link -document.getElementById("download").addEventListener("click", function () { +function downloadEvent() { let button = document.getElementById("downloadButton"); let payload = { @@ -94,7 +93,7 @@ document.getElementById("download").addEventListener("click", function () { let download = document.getElementById("download"); download.innerHTML = "" let message = document.createElement("p"); - message.innerText = "Link sent to Tube Archivist" + message.innerText = "Download link sent to Tube Archivist" download.appendChild(message) download.appendChild(document.createElement("hr")); }) @@ -107,7 +106,38 @@ document.getElementById("download").addEventListener("click", function () { let sending = browserType.runtime.sendMessage(payload); sending.then(handleResponse, handleError) -}) +} + + +function subscribeEvent() { + + let button = document.getElementById("subscribeButton"); + let payload = { + "subscribe": { + "url": button.getAttribute("data-id") + } + }; + + function handleResponse(message) { + console.log("popup.js response: " + JSON.stringify(message)); + browserType.storage.local.remove("youtube").then(response => { + let download = document.getElementById("download"); + download.innerHTML = "" + let message = document.createElement("p"); + message.innerText = "Subscribe link sent to Tube Archivist" + download.appendChild(message) + download.appendChild(document.createElement("hr")); + }) + } + + function handleError(error) { + console.log(`Error: ${error}`); + } + + let sending = browserType.runtime.sendMessage(payload); + sending.then(handleResponse, handleError) + +} // fill in form @@ -135,25 +165,36 @@ document.addEventListener("DOMContentLoaded", async () => { browserType.storage.local.get("youtube", function(result) { if (result.youtube) { - downlodButton(result); + createButtons(result); } }) }) -function downlodButton(result) { + +function createButtons(result) { let download = document.getElementById("download"); let title = document.createElement("p"); title.innerText = result.youtube.title; - let button = document.createElement("button"); - button.innerText = "download"; - button.id = "downloadButton"; - button.setAttribute("data-id", result.youtube.url); + // dl button + let downloadButton = document.createElement("button"); + downloadButton.innerText = "download"; + downloadButton.id = "downloadButton"; + downloadButton.setAttribute("data-id", result.youtube.url); + downloadButton.addEventListener("click", function(){downloadEvent()}, false); + + // subscribe button + let subscribeButton = document.createElement("button"); + subscribeButton.innerText = "subscribe"; + subscribeButton.id = "subscribeButton"; + subscribeButton.setAttribute("data-id", result.youtube.url); + subscribeButton.addEventListener("click", function(){subscribeEvent()}, false); download.appendChild(title); - download.appendChild(button); + download.appendChild(downloadButton); + download.appendChild(subscribeButton); download.appendChild(document.createElement("hr")); }