diff --git a/background.js b/background.js index 598ba40..03c9f67 100644 --- a/background.js +++ b/background.js @@ -16,8 +16,8 @@ chrome.runtime.onInstalled.addListener(function () { chrome.contextMenus.onClicked.addListener(function (item, tab) { chrome.storage.sync.get(['metube'], function (data) { - if(data === undefined || !data.hasOwnProperty('metube') ){ - // todo notify user about settings + if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { + openTab(chrome.extension.getURL('options.html'), tab); return } @@ -29,13 +29,7 @@ chrome.contextMenus.onClicked.addListener(function (item, tab) { if (xhr.readyState === 4 && xhr.status === 200) { let json = JSON.parse(xhr.responseText); if (json.status === "ok") { - chrome.tabs.query({url: url + "/*"}, function (tabs) { - if (tabs.length !== 0) { - chrome.tabs.update(tabs[0].id, {'active': true}, () => {}); - } else { - chrome.tabs.create({url: url, index: tab.index + 1}); - } - }); + openTab(data.metube, tab); } else { alert("error :: " + json); } @@ -44,3 +38,24 @@ chrome.contextMenus.onClicked.addListener(function (item, tab) { xhr.send(JSON.stringify({"quality": "best", "url": item.linkUrl})); }); }); + +chrome.browserAction.onClicked.addListener(function (tab) { + chrome.storage.sync.get(['metube'], function (data) { + if (data === undefined || !data.hasOwnProperty('metube') || data.metube === "") { + openTab(chrome.extension.getURL('options.html'), tab); + return + } + + openTab(data.metube, tab); + }); +}); + +function openTab(url, currentTab) { + chrome.tabs.query({url: url + "/*"}, function (tabs) { + if (tabs.length !== 0) { + chrome.tabs.update(tabs[0].id, {'active': true}, () => {}); + } else { + chrome.tabs.create({url: url, index: currentTab.index + 1}); + } + }); +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 4a5cf4e..11562ce 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,8 @@ "browser_style": true }, "browser_action": { - "default_popup": "options.html" + "default_title": "", + "default_icon": "icon128.png" }, "icons": { "16": "icon16.png",