From 21cfe5e1279463e277311d05bdf49a81ba2b6dd3 Mon Sep 17 00:00:00 2001 From: Akash Vacher Date: Fri, 30 Sep 2022 22:59:56 -0700 Subject: [PATCH] Refactored code to allow code reuse for future Extracted functionality from the contextmenu onclick method to a separate function to allow code reuse in future. --- src/background.js | 61 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/background.js b/src/background.js index bb2dfa5..f67d5e6 100644 --- a/src/background.js +++ b/src/background.js @@ -13,42 +13,43 @@ chrome.runtime.onInstalled.addListener(function() { }); }); +function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) { + console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl); + fetch(metubeUrl + "/add", { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + "quality": "best", + "url": videoUrl + }) + }).then(function(res) { + if (res.ok === true && res.status === 200) { + return res.json(); + } + // todo fix it + alert("error :: code " + res.status); + }).then(function(result) { + if (result.status === "ok") { + openTab(metubeUrl, tab); + } else { + // todo fix it + alert("error :: " + json); + } + }).catch(function(res) { + alert("error :: " + res); + }); +} + chrome.contextMenus.onClicked.addListener(function(item, tab) { chrome.storage.sync.get(['metube'], function(data) { if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { openTab(chrome.runtime.getURL('options.html'), tab); return } - - fetch(data.metube + "/add", { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - "quality": "best", - "url": item.linkUrl - }) - }) - .then(function(res) { - if (res.ok === true && res.status === 200) { - return res.json(); - } - // todo fix it - alert("error :: code " + res.status); - }) - .then(function(result) { - if (result.status === "ok") { - openTab(data.metube, tab); - } else { - // todo fix it - alert("error :: " + json); - } - }) - .catch(function(res) { - alert("error :: " + res); - }) + sendVideoUrlToMetubeAndSwitchTab(item.linkUrl, data.metube, tab); }); });