diff --git a/background.js b/background.js index 788823791..3f8c1d99d 100644 --- a/background.js +++ b/background.js @@ -210,110 +210,98 @@ chrome.windows.onFocusChanged.addListener(function (windowId) { /*-------------------------------------------------------------- # MESSAGE LISTENER --------------------------------------------------------------*/ -chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { - var name = request.name; +let tabConnected = {}; - if (name === 'download') { - chrome.permissions.request({ - permissions: ['downloads'], - origins: ['https://www.youtube.com/*'] - }, function (granted) { - if (granted) { - try { - var blob = new Blob([JSON.stringify(request.value)], { - type: 'application/json;charset=utf-8' - }); +chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { + console.log(message); + console.log(sender); - chrome.downloads.download({ - url: URL.createObjectURL(blob), - filename: request.filename, - saveAs: true - }); - } catch (error) { - console.error(error); + switch(message.action || message.name || message) { + case 'play': + chrome.tabs.query({ url: 'https://www.youtube.com/*' }).then(function (tabs) { + let tabIds = []; + for (let tab of tabs) { + tabIds.push(tab.id); + if (!tab.discarded && tab.id !== sender.tab.id && tabConnected[tab.id]) { + chrome.tabs.sendMessage(tab.id, {action: "another-video-started-playing"}); + } } - } else { - console.error('Permission is not granted.'); - } - }); - } -}); + // prune stale tab-connected data + for (let id in tabConnected) { + if (!tabIds.includes(Number(id))) { + delete tabConnected[id]; + } + } + }, function () {console.log("Error querying Tabs")}); + break -let prevTabsLength = 0; -chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { - var action = message.action || message; + case 'options-page-connected': + sendResponse({ + isTab: sender.hasOwnProperty('tab') + }); + break - if (action === "play") { - chrome.tabs.query({}, function (tabs) { - if (tabs.length > prevTabsLength) { - prevTabsLength = tabs.length; - for (let i = 0, l = tabs.length; i < l; i++) { - let tab = tabs[i]; - chrome.tabs.sendMessage(tab.id, {action: "new-tab-opened"}); - } - } else { - prevTabsLength = tabs.length; - } - for (let i = 0, l = tabs.length; i < l; i++) { - let tab = tabs[i]; + case 'tab-connected': + tabConnected[sender.tab.id] = true; + sendResponse({ + tabId: sender.tab.id + }); + break - if (sender.tab.id !== tab.id) { - chrome.tabs.sendMessage(tab.id, {action: "another-video-started-playing"}); - } - } - }); - } else if (action === 'options-page-connected') { - sendResponse({ - isTab: sender.hasOwnProperty('tab') - }); - } else if (action === 'tab-connected') { - try{ sendResponse({ - hostname: new URL(sender.url).hostname, - tabId: sender.tab.id - }); } catch (error) { console.error("invalid url?", error); } - } else if (action === 'fixPopup') { + case 'fixPopup': //~ get the current focused tab and convert it to a URL-less popup (with same state and size) - chrome.windows.getLastFocused(w => { - chrome.tabs.query({ - windowId: w.id, - active: true - }, ts => { - const tID = ts[0]?.id, - data = { type: 'popup', - state: w.state, - width: parseInt(message.width, 10), - height: parseInt(message.height, 10), - left: 0, - top: 20 - } + chrome.windows.getLastFocused(w => { + chrome.tabs.query({ + windowId: w.id, + active: true + }, ts => { + const tID = ts[0]?.id, + data = { type: 'popup', + state: w.state, + width: parseInt(message.width, 10), + height: parseInt(message.height, 10), + left: 0, + top: 20 + } - if (tID) {data.tabId = tID;} - chrome.windows.create(data, pw => {}); + if (tID) {data.tabId = tID;} + chrome.windows.create(data, pw => {}); - //append to title? - chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) { - if (tabId === tID && changeInfo.status === 'complete' && !message.title.startsWith("undefined")) { - chrome.tabs.onUpdated.removeListener(listener); - chrome.scripting.executeScript({ target: { tabId: tID }, func: () => { document.title = `${message.title} - ImprovedTube`; } }) //manifest3 - // chrome.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`}); //manifest2 - } + //append to title? + chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) { + if (tabId === tID && changeInfo.status === 'complete' && !message.title.startsWith("undefined")) { + chrome.tabs.onUpdated.removeListener(listener); + chrome.scripting.executeScript({ target: { tabId: tID }, func: () => { document.title = `${message.title} - ImprovedTube`; } }); //manifest3 + // chrome.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`}); //manifest2 + } + }); }); }); - }); + break + case 'download': + chrome.permissions.request({ + permissions: ['downloads'], + origins: ['https://www.youtube.com/*'] + }, function (granted) { + if (granted) { + try { + var blob = new Blob([JSON.stringify(request.value)], { + type: 'application/json;charset=utf-8' + }); + chrome.downloads.download({ + url: URL.createObjectURL(blob), + filename: request.filename, + saveAs: true + }); + } catch (error) { + console.error(error); + } else { + console.error('Permission is not granted.'); + } + }) + break } }); -/*------ search results in new tab --------- -chrome.storage.local.get('open_new_tab', function (result) -{if (result.open_new_tab === true){ - -chrome.runtime.onMessage.addListener(function (request) { - if (request.action === "createNewTab") { - chrome.tabs.create({ url: request.url }); - } -}); - -}}); */ - /*-----# UNINSTALL URL-----------------------------------*/ chrome.runtime.setUninstallURL('https://improvedtube.com/uninstalled'); diff --git a/js&css/extension/init.js b/js&css/extension/init.js index cdb6a00dc..ac9a91a25 100644 --- a/js&css/extension/init.js +++ b/js&css/extension/init.js @@ -66,8 +66,7 @@ extension.inject([ '/js&css/web-accessible/www.youtube.com/shortcuts.js', '/js&css/web-accessible/www.youtube.com/blocklist.js', '/js&css/web-accessible/www.youtube.com/settings.js', - '/js&css/web-accessible/init.js', - '/js&css/web-accessible/mutations.js' + '/js&css/web-accessible/init.js' ], function () { extension.ready = true; @@ -79,7 +78,7 @@ document.addEventListener('DOMContentLoaded', function () { bodyReady(); }); -let prevRequestAction = ""; + chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.action === 'focus') { extension.messages.send({ @@ -111,16 +110,9 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { extension.messages.send({ deleteCookies: true }); - } else if ( - request.action === "another-video-started-playing" && - prevRequestAction === "new-tab-opened" - ) { - console.log("Continue playing"); - } else if (request.action === "another-video-started-playing") { + } else if (request.action === "another-video-started-playing") { extension.features.onlyOnePlayerInstancePlaying(); - } - - prevRequestAction = request.action; + } }); document.addEventListener('it-message-from-youtube', function () { diff --git a/js&css/web-accessible/mutations.js b/js&css/web-accessible/mutations.js deleted file mode 100644 index 32b493533..000000000 --- a/js&css/web-accessible/mutations.js +++ /dev/null @@ -1,22 +0,0 @@ -/*-------------------------------------------------------------- ->>> MUTATIONS ----------------------------------------------------------------- -# Media element - # Play ---------------------------------------------------------------*/ - -/*-------------------------------------------------------------- -# MEDIA ELEMENT ---------------------------------------------------------------*/ - -/*-------------------------------------------------------------- -# PLAY ---------------------------------------------------------------*/ - -HTMLMediaElement.prototype.play = (function (original) { - return function () { - document.dispatchEvent(new CustomEvent('it-play')); - - return original.apply(this, arguments); - } -})(HTMLMediaElement.prototype.play); \ No newline at end of file diff --git a/js&css/web-accessible/www.youtube.com/player.js b/js&css/web-accessible/www.youtube.com/player.js index 74a679e76..171e5e326 100644 --- a/js&css/web-accessible/www.youtube.com/player.js +++ b/js&css/web-accessible/www.youtube.com/player.js @@ -28,7 +28,11 @@ ImprovedTube.autoplayDisable = function (videoElement) { setTimeout(function() { try { player.pauseVideo(); } catch (error) { console.log("autoplayDisable: Pausing"); videoElement.pause(); } }); + } else { + document.dispatchEvent(new CustomEvent('it-play')); } + } else { + document.dispatchEvent(new CustomEvent('it-play')); } }; /*------------------------------------------------------------------------------ diff --git a/manifest.json b/manifest.json index 86595c921..848404d70 100644 --- a/manifest.json +++ b/manifest.json @@ -54,6 +54,7 @@ "run_at": "document_start" } ], + "host_permissions": [ "https://www.youtube.com/*" ], "offline_enabled": true, "optional_permissions": [ "downloads" @@ -76,8 +77,7 @@ "js&css/web-accessible/www.youtube.com/shortcuts.js", "js&css/web-accessible/www.youtube.com/blocklist.js", "js&css/web-accessible/www.youtube.com/settings.js", - "js&css/web-accessible/init.js", - "js&css/web-accessible/mutations.js", + "js&css/web-accessible/init.js" "stuff/icons/48.png" ], "matches": [ @@ -86,4 +86,3 @@ } ] } - diff --git a/manifest2.json b/manifest2.json index 3c8c29f8a..57ddb70c7 100644 --- a/manifest2.json +++ b/manifest2.json @@ -56,6 +56,7 @@ "run_at": "document_start" } ], + "host_permissions": [ "https://www.youtube.com/*" ], "offline_enabled": true, "optional_permissions": [ "downloads" @@ -77,8 +78,6 @@ "js&css/web-accessible/www.youtube.com/shortcuts.js", "js&css/web-accessible/www.youtube.com/blocklist.js", "js&css/web-accessible/www.youtube.com/settings.js", - "js&css/web-accessible/init.js", - "js&css/web-accessible/mutations.js" + "js&css/web-accessible/init.js" ] } -