diff --git a/background.js b/background.js index 03c9f67..9966adb 100644 --- a/background.js +++ b/background.js @@ -22,24 +22,35 @@ chrome.contextMenus.onClicked.addListener(function (item, tab) { } let url = data.metube; - let xhr = new XMLHttpRequest(); - xhr.open("POST", url + "/add", true); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onreadystatechange = function () { - if (xhr.readyState === 4 && xhr.status === 200) { - let json = JSON.parse(xhr.responseText); - if (json.status === "ok") { + + fetch(url + "/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(); + } + alert("error :: code " + res.status); + }) + .then(function (result) { + if (result.status === "ok") { openTab(data.metube, tab); } else { alert("error :: " + json); } - } - }; - xhr.send(JSON.stringify({"quality": "best", "url": item.linkUrl})); + }) + .catch(function (res) { + alert("error :: " + res); + }) }); }); -chrome.browserAction.onClicked.addListener(function (tab) { +chrome.action.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); @@ -53,7 +64,8 @@ chrome.browserAction.onClicked.addListener(function (tab) { function openTab(url, currentTab) { chrome.tabs.query({url: url + "/*"}, function (tabs) { if (tabs.length !== 0) { - chrome.tabs.update(tabs[0].id, {'active': true}, () => {}); + chrome.tabs.update(tabs[0].id, {'active': true}, () => { + }); } else { chrome.tabs.create({url: url, index: currentTab.index + 1}); } diff --git a/manifest.json b/manifest.json index 11562ce..bc8da02 100644 --- a/manifest.json +++ b/manifest.json @@ -2,19 +2,16 @@ "name": "MeTube Downloader", "description": "Use the context menu to send video into MeTube application", "version": "1.0", - "manifest_version": 2, + "manifest_version": 3, "permissions": ["contextMenus", "storage", "tabs"], "background": { - "scripts": [ - "background.js" - ], - "persistent": false + "service_worker": "background.js" }, "options_ui": { "page": "options.html", "browser_style": true }, - "browser_action": { + "action": { "default_title": "", "default_icon": "icon128.png" },