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.
This commit is contained in:
Akash Vacher 2022-09-30 22:59:56 -07:00
parent 7caefb975c
commit 21cfe5e127
1 changed files with 31 additions and 30 deletions

View File

@ -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);
});
});