From 7a104e2e8cf9b468c0021c4ab8afdc48ef10983a Mon Sep 17 00:00:00 2001 From: Akash Vacher Date: Sat, 1 Oct 2022 12:41:36 -0700 Subject: [PATCH] Added option for silently sending current tab URL to MeTube via extension icon click (no tab switch) Added the new option as another radio button that user may select when deciding the extention icon click behavior --- src/background.js | 35 ++++++++++++++++++++++++++--------- src/options.html | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/background.js b/src/background.js index 19f5f60..ce2c8b8 100644 --- a/src/background.js +++ b/src/background.js @@ -13,7 +13,7 @@ chrome.runtime.onInstalled.addListener(function() { }); }); -function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) { +function sendVideoUrlToMetube(videoUrl, metubeUrl, callback) { console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl); fetch(metubeUrl + "/add", { method: 'POST', @@ -29,15 +29,19 @@ function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) { if (res.ok === true && res.status === 200) { return res.json(); } - console.log("error :: code " + res.status); + console.log("error :: code" + res.status); }).then(function(result) { - if (result.status === "ok") { - openTab(metubeUrl, tab); - } else { + 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'); + } } - }).catch(function(res) { - console.log("Final catch - error :: " + res); + }).then(function() { + typeof callback === 'function' && callback(); + }).catch(function(e) { + console.log("Ran into an error :: ", e); }); } @@ -47,7 +51,9 @@ chrome.contextMenus.onClicked.addListener(function(item, tab) { openTab(chrome.runtime.getURL('options.html'), tab); return } - sendVideoUrlToMetubeAndSwitchTab(item.linkUrl, data.metube, tab); + sendVideoUrlToMetube(item.linkUrl, data.metube, function() { + openTab(data.metube, tab); + }); }); }); @@ -62,6 +68,15 @@ chrome.action.onClicked.addListener(function(tab) { } else if (data.clickBehavior == 'go-to-metube') { console.log("Going to Metube URL..."); openTab(data.metube, tab); + } else if (data.clickBehavior == 'send-current-url') { + chrome.tabs.query({ + active: true, + lastFocusedWindow: true + }, function(tabs) { + // use this tab to get the youtube video URL + let videoUrl = tabs[0].url; + sendVideoUrlToMetube(videoUrl, data.metube); + }); } else if (data.clickBehavior == 'send-current-url-and-switch') { chrome.tabs.query({ active: true, @@ -69,7 +84,9 @@ chrome.action.onClicked.addListener(function(tab) { }, function(tabs) { // use this tab to get the youtube video URL let videoUrl = tabs[0].url; - sendVideoUrlToMetubeAndSwitchTab(videoUrl, data.metube, tab); + sendVideoUrlToMetube(videoUrl, data.metube, function() { + openTab(data.metube, tab); + }); }); } else { console.log("Unknown clickBehavior value: " + data.clickBehavior); diff --git a/src/options.html b/src/options.html index 6b4f7d7..3ae68ec 100644 --- a/src/options.html +++ b/src/options.html @@ -36,6 +36,7 @@

Left-click on extension icon behavior



+

Additional sites

Youtube-dl support many another sites except youtube. You can add mask url's of this sites in textfield bellow but it can affect performance and we have not tested them support