From b0b2b7c2e1c3bc3ad30d6c9a30c4766169d8235b Mon Sep 17 00:00:00 2001 From: Akash Vacher Date: Sun, 13 Nov 2022 14:40:50 -0800 Subject: [PATCH] Simplified the promise chain in sendVideoUrlToMetube --- src/background.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/background.js b/src/background.js index cc67649..d913e3d 100644 --- a/src/background.js +++ b/src/background.js @@ -21,6 +21,9 @@ chrome.runtime.onInstalled.addListener(function() { function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) { console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl); + if (typeof callback !== 'function'){ + callback = function(){}; + } fetch(metubeUrl + "/add", { method: 'POST', headers: { @@ -31,26 +34,17 @@ function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) { "quality": "best", "url": videoUrl }) - }).then(function(res) { - if (res.ok === true && res.status === 200) { - return res.json(); - } - console.log("error :: code" + res.status); - }).then(function(result) { - if (result.status !== "ok") { - console.log("error :: ", result ); - if (result.msg.includes('URLError')) { - // Go straight to catch block and skip the callback - throw new Error('Error when adding URL to MyTube'); - } - } - }).then(function() { - if (typeof callback === 'function'){ + }) + .then(response=>response.json()) + .then(function (response) { + if (response.status === 'ok') { callback(); } - }).catch(function(e) { - console.log("Ran into an error :: ", e); - }); + else { + console.log("Ran into an error when submitting URL to meTube: " + response.msg); + } + }) + .catch(e => console.log("Ran into an unexpected error: " + e)); } chrome.contextMenus.onClicked.addListener(function(item, tab) {