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'],
});
// 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);
if (typeof callback !== 'function'){
callback = function(){};
}
fetch(metubeUrl + "/add", {
method: 'POST',
headers: {
@ -25,55 +34,63 @@ function sendVideoUrlToMetubeAndSwitchTab(videoUrl, metubeUrl, tab) {
"quality": "best",
"url": videoUrl
})
}).then(function(res) {
if (res.ok === true && res.status === 200) {
return res.json();
})
.then(response=>response.json())
.then(function (response) {
if (response.status === 'ok') {
callback();
}
// 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);
else {
console.log("Ran into an error when submitting URL to meTube: " + response.msg);
}
}).catch(function(res) {
alert("error :: " + res);
});
})
.catch(e => console.log("Ran into an unexpected error: " + e));
}
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 === "") {
openTab(chrome.runtime.getURL('options.html'), tab);
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.storage.sync.get(['metube', 'sendOnClick'], function(data) {
chrome.storage.sync.get(['metube', 'clickBehavior'], function(data) {
if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") {
openTab(chrome.runtime.getURL('options.html'), tab);
return
}
let needToSwitch = (data.clickBehavior === 'send-current-url-and-switch');
if (data.clickBehavior == 'do-nothing') {
return
} else if (data.clickBehavior == 'go-to-metube') {
console.log("Going to Metube URL...");
openTab(data.metube, tab);
} else {
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
// use this tab to get the youtube video URL
let videoUrl = tabs[0].url;
if (data.sendOnClick) {
sendVideoUrlToMetubeAndSwitchTab(videoUrl, data.metube, tab);
sendVideoUrlToMetube(videoUrl, data.metube, function() {
if (!needToSwitch) {
return;
}
else {
console.log("Going to Metube URL...");
openTab(data.metube, tab);
}
});
});
}
});
});
function openTab(url, currentTab) {

View File

@ -33,7 +33,17 @@
<h3>Settings</h3>
<label for="metube">Url of MeTube</label><input type="url" placeholder="http://0.0.0.0:8081/" name="metube" id="metube">
<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>
<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/*

View File

@ -7,9 +7,16 @@ async function saveOptions() {
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');
setTimeout(function () {
@ -41,7 +48,12 @@ async function saveOptions() {
}
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) {
document.getElementById("metube").value = data.metube;
}
@ -49,11 +61,15 @@ async function restoreOptions() {
if (data.sites != undefined) {
document.getElementById("additional").value = data.sites;
}
// document.getElementById("send-on-click").checked = true;
if (data.sendOnClick) {
document.getElementById("send-on-click").checked = true;
if (data.clickBehavior != undefined) {
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/); }