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
This commit is contained in:
Akash Vacher 2022-10-01 12:41:36 -07:00
parent 1a12fc0c99
commit 7a104e2e8c
2 changed files with 27 additions and 9 deletions

View File

@ -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); console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl);
fetch(metubeUrl + "/add", { fetch(metubeUrl + "/add", {
method: 'POST', method: 'POST',
@ -29,15 +29,19 @@ function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) {
if (res.ok === true && res.status === 200) { if (res.ok === true && res.status === 200) {
return res.json(); return res.json();
} }
console.log("error :: code " + res.status); console.log("error :: code" + res.status);
}).then(function(result) { }).then(function(result) {
if (result.status === "ok") { if (result.status !== "ok") {
openTab(metubeUrl, tab);
} else {
console.log("error :: ", result ); 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) { }).then(function() {
console.log("Final catch - error :: " + res); 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); openTab(chrome.runtime.getURL('options.html'), tab);
return 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') { } else if (data.clickBehavior == 'go-to-metube') {
console.log("Going to Metube URL..."); console.log("Going to Metube URL...");
openTab(data.metube, tab); 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') { } else if (data.clickBehavior == 'send-current-url-and-switch') {
chrome.tabs.query({ chrome.tabs.query({
active: true, active: true,
@ -69,7 +84,9 @@ chrome.action.onClicked.addListener(function(tab) {
}, function(tabs) { }, function(tabs) {
// use this tab to get the youtube video URL // use this tab to get the youtube video URL
let videoUrl = tabs[0].url; let videoUrl = tabs[0].url;
sendVideoUrlToMetubeAndSwitchTab(videoUrl, data.metube, tab); sendVideoUrlToMetube(videoUrl, data.metube, function() {
openTab(data.metube, tab);
});
}); });
} else { } else {
console.log("Unknown clickBehavior value: " + data.clickBehavior); console.log("Unknown clickBehavior value: " + data.clickBehavior);

View File

@ -36,6 +36,7 @@
<h4>Left-click on extension icon behavior</h4> <h4>Left-click on extension icon behavior</h4>
<input type="radio" name="click-behavior" id="do-nothing" value="do-nothing"><label for="do-nothing">Do Nothing</label><br> <input type="radio" name="click-behavior" id="do-nothing" value="do-nothing"><label for="do-nothing">Do Nothing</label><br>
<input type="radio" name="click-behavior" id="go-to-metube" value="go-to-metube" checked><label for="go-to-metube">Go to MeTube URL</label><br> <input type="radio" name="click-behavior" id="go-to-metube" value="go-to-metube" checked><label for="go-to-metube">Go to MeTube URL</label><br>
<input type="radio" name="click-behavior" id="send-current-url" value="send-current-url"><label for="send-current-url">Send current tab URL to MeTube (no tab switch)</label><br>
<input type="radio" name="click-behavior" id="send-current-url-and-switch" value="send-current-url-and-switch"><label for="send-current-url-and-switch">Send current tab URL to MeTube and switch to MeTube</label><br> <input type="radio" name="click-behavior" id="send-current-url-and-switch" value="send-current-url-and-switch"><label for="send-current-url-and-switch">Send current tab URL to MeTube and switch to MeTube</label><br>
<h3>Additional sites</h3> <h3>Additional sites</h3>
<p>Youtube-dl support <a href="https://github.com/ytdl-org/youtube-dl/blob/master/docs/supportedsites.md" target="_blank">many another sites</a> 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</p> <p>Youtube-dl support <a href="https://github.com/ytdl-org/youtube-dl/blob/master/docs/supportedsites.md" target="_blank">many another sites</a> 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</p>