Merge pull request #7 from akashvacher/silentSend

Added more options for left-click on extension icon and contextmenu item click behavior
This commit is contained in:
Виктор Диктор 2022-12-25 00:55:34 +03:00 committed by GitHub
commit 491dcb791b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 40 deletions

View File

@ -11,10 +11,19 @@ chrome.runtime.onInstalled.addListener(function() {
], ],
contexts: ['link'], contexts: ['link'],
}); });
// Set sane defaults for click behaviors at extension install time
chrome.storage.sync.set({
"clickBehavior": "go-to-metube",
"contextMenuClickBehavior": "context-menu-send-current-url-and-switch"
});
}); });
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);
if (typeof callback !== 'function'){
callback = function(){};
}
fetch(metubeUrl + "/add", { fetch(metubeUrl + "/add", {
method: 'POST', method: 'POST',
headers: { headers: {
@ -25,54 +34,62 @@ function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) {
"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') {
callback();
} }
// todo fix it else {
alert("error :: code " + res.status); console.log("Ran into an error when submitting URL to meTube: " + response.msg);
}).then(function(result) {
if (result.status === "ok") {
openTab(metubeUrl, tab);
} else {
// todo fix it
alert("error :: " + json);
} }
}).catch(function(res) { })
alert("error :: " + res); .catch(e => console.log("Ran into an unexpected error: " + e));
});
} }
chrome.contextMenus.onClicked.addListener(function(item, tab) { chrome.contextMenus.onClicked.addListener(function(item, tab) {
chrome.storage.sync.get(['metube'], function(data) { chrome.storage.sync.get(['metube', 'contextMenuClickBehavior'], function(data) {
if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") {
openTab(chrome.runtime.getURL('options.html'), tab); openTab(chrome.runtime.getURL('options.html'), tab);
return return
} }
sendVideoUrlToMetubeAndSwitchTab(item.linkUrl, data.metube, tab); let needToSwitch = (data.contextMenuClickBehavior === 'context-menu-send-current-url-and-switch');
sendVideoUrlToMetube(item.linkUrl, data.metube, function() {
if (!needToSwitch) {
return;
}
openTab(data.metube, tab);
});
}); });
}); });
chrome.action.onClicked.addListener(function(tab) { chrome.action.onClicked.addListener(function(tab) {
chrome.storage.sync.get(['metube', 'sendOnClick'], function(data) { chrome.storage.sync.get(['metube', 'clickBehavior'], function(data) {
if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") {
openTab(chrome.runtime.getURL('options.html'), tab); openTab(chrome.runtime.getURL('options.html'), tab);
return return
} }
chrome.tabs.query({ let needToSwitch = (data.clickBehavior === 'send-current-url-and-switch');
active: true, if (data.clickBehavior == 'do-nothing') {
lastFocusedWindow: true return
}, function(tabs) { } else if (data.clickBehavior == 'go-to-metube') {
// use this tab to get the youtube video URL console.log("Going to Metube URL...");
let videoUrl = tabs[0].url; openTab(data.metube, tab);
if (data.sendOnClick) { } else {
sendVideoUrlToMetubeAndSwitchTab(videoUrl, data.metube, tab); chrome.tabs.query({
} active: true,
else { lastFocusedWindow: true
console.log("Going to Metube URL..."); }, function(tabs) {
openTab(data.metube, tab); // use this tab to get the youtube video URL
} let videoUrl = tabs[0].url;
}); sendVideoUrlToMetube(videoUrl, data.metube, function() {
if (!needToSwitch) {
return;
}
openTab(data.metube, tab);
});
});
}
}); });
}); });

View File

@ -33,7 +33,17 @@
<h3>Settings</h3> <h3>Settings</h3>
<label for="metube">Url of MeTube</label><input type="url" placeholder="http://0.0.0.0:8081/" name="metube" id="metube"> <label for="metube">Url of MeTube</label><input type="url" placeholder="http://0.0.0.0:8081/" name="metube" id="metube">
<br> <br>
<label for="send-on-click">Enable left-click on extension icon to send current page URL to Metube</label><input type="checkbox" name="send-on-click" id="send-on-click">
<h4>When you left-click on the extension icon, you'd like it to:</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="go-to-metube" value="go-to-metube"><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>
<h4>When you right-click on video preview, you'd like it to:</h4>
<input type="radio" name="context-menu-click-behavior" id="context-menu-send-current-url" value="context-menu-send-current-url"><label for="context-menu-send-current-url">Send the video URL to MeTube (no tab switch)</label><br>
<input type="radio" name="context-menu-click-behavior" id="context-menu-send-current-url-and-switch" value="context-menu-send-current-url-and-switch"><label for="context-menu-send-current-url-and-switch">Send the video 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>
<p><textarea name="additional" id="additional" placeholder="https://vimeo.com/* <p><textarea name="additional" id="additional" placeholder="https://vimeo.com/*

View File

@ -7,9 +7,16 @@ async function saveOptions() {
let sites = document.getElementById("additional").value; let sites = document.getElementById("additional").value;
let sendOnClick = document.getElementById("send-on-click").checked; let clickBehavior = document.querySelector('input[name="click-behavior"]:checked').value;
chrome.storage.sync.set({ "metube": url, "sites": sites, "sendOnClick": sendOnClick}, function () { let contextMenuClickBehavior = document.querySelector('input[name="context-menu-click-behavior"]:checked').value;
chrome.storage.sync.set({
"metube": url,
"sites": sites,
"clickBehavior": clickBehavior,
"contextMenuClickBehavior": contextMenuClickBehavior
}, function () {
document.getElementById("saved").classList.remove('hidden'); document.getElementById("saved").classList.remove('hidden');
setTimeout(function () { setTimeout(function () {
@ -41,7 +48,12 @@ async function saveOptions() {
} }
async function restoreOptions() { async function restoreOptions() {
chrome.storage.sync.get(['metube', 'sites', 'sendOnClick'], function (data) { chrome.storage.sync.get([
'metube',
'sites',
'clickBehavior',
'contextMenuClickBehavior'
], function (data) {
if (data.metube != undefined) { if (data.metube != undefined) {
document.getElementById("metube").value = data.metube; document.getElementById("metube").value = data.metube;
} }
@ -49,11 +61,15 @@ async function restoreOptions() {
if (data.sites != undefined) { if (data.sites != undefined) {
document.getElementById("additional").value = data.sites; document.getElementById("additional").value = data.sites;
} }
// document.getElementById("send-on-click").checked = true;
if (data.sendOnClick) { if (data.clickBehavior != undefined) {
document.getElementById("send-on-click").checked = true; document.getElementById(data.clickBehavior).checked = true;
} }
});
if (data.contextMenuClickBehavior != undefined) {
document.getElementById(data.contextMenuClickBehavior).checked = true;
}
})
} }
function splitLines(t) { return t.split(/\r\n|\r|\n/); } function splitLines(t) { return t.split(/\r\n|\r|\n/); }