Simplified the promise chain in sendVideoUrlToMetube

This commit is contained in:
Akash Vacher 2022-11-13 14:40:50 -08:00
parent 282a3bf9bc
commit b0b2b7c2e1
1 changed files with 12 additions and 18 deletions

View File

@ -21,6 +21,9 @@ chrome.runtime.onInstalled.addListener(function() {
function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) { function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) {
console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl); console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl);
if (typeof callback !== 'function'){
callback = function(){};
}
fetch(metubeUrl + "/add", { fetch(metubeUrl + "/add", {
method: 'POST', method: 'POST',
headers: { headers: {
@ -31,26 +34,17 @@ function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) {
"quality": "best", "quality": "best",
"url": videoUrl "url": videoUrl
}) })
}).then(function(res) { })
if (res.ok === true && res.status === 200) { .then(response=>response.json())
return res.json(); .then(function (response) {
} if (response.status === 'ok') {
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'){
callback(); callback();
} }
}).catch(function(e) { else {
console.log("Ran into an error :: ", e); 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) { chrome.contextMenus.onClicked.addListener(function(item, tab) {