diff --git a/src/background.js b/src/background.js index bb2dfa5..3d4a5cf 100644 --- a/src/background.js +++ b/src/background.js @@ -13,53 +13,66 @@ 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); }); }); chrome.action.onClicked.addListener(function(tab) { - chrome.storage.sync.get(['metube'], function(data) { + chrome.storage.sync.get(['metube', 'sendOnClick'], function(data) { if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { openTab(chrome.runtime.getURL('options.html'), tab); return } - - openTab(data.metube, tab); + 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); + } + else { + console.log("Going to Metube URL..."); + openTab(data.metube, tab); + } + }); }); }); diff --git a/src/options.html b/src/options.html index 22f7df5..97c2e75 100644 --- a/src/options.html +++ b/src/options.html @@ -32,6 +32,8 @@