diff --git a/background.js b/background.js index 0ce1c4d4e..0b9c0cadf 100644 --- a/background.js +++ b/background.js @@ -12,212 +12,230 @@ # LOCALE --------------------------------------------------------------*/ - function getLocale(language, callback) { - language = language.replace('-', '_'); - - fetch('_locales/' + language + '/messages.json').then(function (response) { - if (response.ok) { - response.json().then(callback); - } else { - getLocale('en', callback); - } - }).catch(function () { - getLocale('en', callback); - }); + language = language.replace("-", "_"); + + fetch("_locales/" + language + "/messages.json") + .then(function (response) { + if (response.ok) { + response.json().then(callback); + } else { + getLocale("en", callback); + } + }) + .catch(function () { + getLocale("en", callback); + }); } - /*-------------------------------------------------------------- # CONTEXT MENU --------------------------------------------------------------*/ function updateContextMenu(language) { - if (!language) { - language = chrome.i18n.getUILanguage(); - } - - getLocale(language, function (response) { - var items = [ - 'donate', - 'rateMe', - 'GitHub' - ]; - - chrome.contextMenus.removeAll(); - - for (var i = 0; i < 3; i++) { - var item = items[i], - text = response[item]; - - if (text) { - text = text.message; - } else { - text = item; - } - - chrome.contextMenus.create({ - id: String(i), - title: text, - contexts: ['action'] - }); - } - - chrome.contextMenus.onClicked.addListener(function (info) { - var links = [ - 'https://www.improvedtube.com/donate', - 'https://chrome.google.com/webstore/detail/improve-youtube-video-you/bnomihfieiccainjcjblhegjgglakjdd', - 'https://github.com/code4charity/YouTube-Extension' - ]; - - window.open(links[info.menuItemId]); - }); - }); + if (!language) { + language = chrome.i18n.getUILanguage(); + } + + getLocale(language, function (response) { + var items = ["donate", "rateMe", "GitHub"]; + + chrome.contextMenus.removeAll(); + + for (var i = 0; i < 3; i++) { + var item = items[i], + text = response[item]; + + if (text) { + text = text.message; + } else { + text = item; + } + + chrome.contextMenus.create({ + id: String(i), + title: text, + contexts: ["action"], + }); + } + + chrome.contextMenus.onClicked.addListener(function (info) { + var links = [ + "https://www.improvedtube.com/donate", + "https://chrome.google.com/webstore/detail/improve-youtube-video-you/bnomihfieiccainjcjblhegjgglakjdd", + "https://github.com/code4charity/YouTube-Extension", + ]; + + window.open(links[info.menuItemId]); + }); + }); } chrome.runtime.onInstalled.addListener(function (details) { - chrome.storage.local.get(function (items) { - var language = items.language; + chrome.storage.local.get(function (items) { + var language = items.language; - updateContextMenu(language); - }); + updateContextMenu(language); + }); }); chrome.storage.onChanged.addListener(function (changes) { - for (var key in changes) { - if (key === 'language') { - updateContextMenu(changes[key].newValue); - } - } + for (var key in changes) { + if (key === "language") { + updateContextMenu(changes[key].newValue); + } + } }); - /*-------------------------------------------------------------- # TAB FOCUS/BLUR --------------------------------------------------------------*/ chrome.tabs.onActivated.addListener(function (activeInfo) { - chrome.tabs.sendMessage(activeInfo.tabId, { - action: 'focus' - }); - - chrome.tabs.query({ - windowId: activeInfo.windowId - }, function (tabs) { - if (tabs) { - for (var i = 0, l = tabs.length; i < l; i++) { - if (tabs[i].id !== activeInfo.tabId) { - chrome.tabs.sendMessage(tabs[i].id, { - action: 'blur' - }); - } - } - } - }); + chrome.tabs.sendMessage(activeInfo.tabId, { + action: "focus", + }); + + chrome.tabs.query( + { + windowId: activeInfo.windowId, + }, + function (tabs) { + if (tabs) { + for (var i = 0, l = tabs.length; i < l; i++) { + if (tabs[i].id !== activeInfo.tabId) { + chrome.tabs.sendMessage(tabs[i].id, { + action: "blur", + }); + } + } + } + } + ); }); chrome.windows.onFocusChanged.addListener(function (windowId) { - chrome.windows.getAll(function (windows) { - for (var i = 0, l = windows.length; i < l; i++) { - if (windows[i].focused === true) { - chrome.tabs.query({ - windowId: windows[i].id - }, function (tabs) { - if (tabs) { - for (var j = 0, k = tabs.length; j < k; j++) { - var tab = tabs[j]; - - if (tab.active) { - chrome.tabs.sendMessage(tab.id, { - action: 'focus' - }); - } - } - } - }); - } else { - chrome.tabs.query({ - windowId: windows[i].id - }, function (tabs) { - if (tabs) { - for (var j = 0, k = tabs.length; j < k; j++) { - var tab = tabs[j]; - - chrome.tabs.sendMessage(tab.id, { - action: 'blur' - }); - } - } - }); - } - } - }); + chrome.windows.getAll(function (windows) { + for (var i = 0, l = windows.length; i < l; i++) { + if (windows[i].focused === true) { + chrome.tabs.query( + { + windowId: windows[i].id, + }, + function (tabs) { + if (tabs) { + for (var j = 0, k = tabs.length; j < k; j++) { + var tab = tabs[j]; + + if (tab.active) { + chrome.tabs.sendMessage(tab.id, { + action: "focus", + }); + } + } + } + } + ); + } else { + chrome.tabs.query( + { + windowId: windows[i].id, + }, + function (tabs) { + if (tabs) { + for (var j = 0, k = tabs.length; j < k; j++) { + var tab = tabs[j]; + + chrome.tabs.sendMessage(tab.id, { + action: "blur", + }); + } + } + } + ); + } + } + }); }); - /*-------------------------------------------------------------- # MESSAGE LISTENER --------------------------------------------------------------*/ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { - var name = request.name; - - 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.downloads.download({ - url: URL.createObjectURL(blob), - filename: request.filename, - saveAs: true - }); - } catch (error) { - console.error(error); - } - } else { - console.error('Permission is not granted.'); - } - }); - } + var name = request.name; + + 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.downloads.download({ + url: URL.createObjectURL(blob), + filename: request.filename, + saveAs: true, + }); + } catch (error) { + console.error(error); + } + } else { + console.error("Permission is not granted."); + } + } + ); + } }); +let prevTabsLength = 0; + chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { - var action = message.action || message; - - if (action === 'play') { - chrome.tabs.query({}, function (tabs) { - for (var i = 0, l = tabs.length; i < l; i++) { - var tab = tabs[i]; - - 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') { - sendResponse({ - hostname: new URL(sender.url).hostname, - tabId: sender.tab.id - }); - } + var action = message.action || message; + + if (action === "play") { + chrome.tabs.query({}, function (tabs) { + if (tabs.length > prevTabsLength) { + prevTabsLength = tabs.length; + for (var i = 0, l = tabs.length; i < l; i++) { + var tab = tabs[i]; + chrome.tabs.sendMessage(tab.id, { + action: "new-tab-opened", + }); + } + } else { + prevTabsLength = tabs.length; + } + for (var i = 0, l = tabs.length; i < l; i++) { + var tab = tabs[i]; + + 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") { + sendResponse({ + hostname: new URL(sender.url).hostname, + tabId: sender.tab.id, + }); + } }); - /*-------------------------------------------------------------- # UNINSTALL URL --------------------------------------------------------------*/ -chrome.runtime.setUninstallURL('https://improvedtube.com/uninstalled'); +chrome.runtime.setUninstallURL("https://improvedtube.com/uninstalled"); diff --git a/content-scripts/extension-context/init.js b/content-scripts/extension-context/init.js index b5e2f21e6..47b51ee4a 100644 --- a/content-scripts/extension-context/init.js +++ b/content-scripts/extension-context/init.js @@ -2,232 +2,260 @@ >>> INITIALIZATION --------------------------------------------------------------*/ -extension.features.youtubeHomePage('init'); +extension.features.youtubeHomePage("init"); -document.documentElement.setAttribute('it-pathname', location.pathname); +document.documentElement.setAttribute("it-pathname", location.pathname); -window.addEventListener('yt-navigate-finish', function () { - document.documentElement.setAttribute('it-pathname', location.pathname); +window.addEventListener("yt-navigate-finish", function () { + document.documentElement.setAttribute("it-pathname", location.pathname); - extension.features.trackWatchedVideos(); - extension.features.thumbnailsQuality(); + extension.features.trackWatchedVideos(); + extension.features.thumbnailsQuality(); }); extension.messages.create(); extension.messages.listener(); -extension.events.on('init', function (resolve) { - extension.storage.listener(); - extension.storage.load(function () { - resolve(); - }); -}, { - async: true -}); +extension.events.on( + "init", + function (resolve) { + extension.storage.listener(); + extension.storage.load(function () { + resolve(); + }); + }, + { + async: true, + } +); function bodyReady() { - if (extension.ready && extension.domReady) { - extension.features.addScrollToTop(); - extension.features.font(); - extension.features.showHeaderOnSearch(); - } + if (extension.ready && extension.domReady) { + extension.features.addScrollToTop(); + extension.features.font(); + extension.features.showHeaderOnSearch(); + } } -extension.events.on('init', function () { - extension.features.bluelight(); - extension.features.dim(); - extension.features.youtubeHomePage(); - extension.features.collapseOfSubscriptionSections(); - extension.features.confirmationBeforeClosing(); - extension.features.defaultContentCountry(); - extension.features.popupWindowButtons(); - extension.features.markWatchedVideos(); - extension.features.relatedVideos(); - extension.features.comments(); - - bodyReady(); -}); - -chrome.runtime.sendMessage({ - action: 'tab-connected' -}, function (response) { - if (response) { - extension.tabId = response.tabId; - } +extension.events.on("init", function () { + extension.features.bluelight(); + extension.features.dim(); + extension.features.youtubeHomePage(); + extension.features.collapseOfSubscriptionSections(); + extension.features.confirmationBeforeClosing(); + extension.features.defaultContentCountry(); + extension.features.popupWindowButtons(); + extension.features.markWatchedVideos(); + extension.features.relatedVideos(); + extension.features.comments(); + + bodyReady(); }); -extension.inject([ - '/content-scripts/website-context/core.js', - '/content-scripts/website-context/functions.js', - '/content-scripts/website-context/youtube-features/appearance.js', - '/content-scripts/website-context/youtube-features/themes.js', - '/content-scripts/website-context/youtube-features/player.js', - '/content-scripts/website-context/youtube-features/playlist.js', - '/content-scripts/website-context/youtube-features/channel.js', - '/content-scripts/website-context/youtube-features/shortcuts.js', - '/content-scripts/website-context/youtube-features/blacklist.js', - '/content-scripts/website-context/youtube-features/settings.js', - '/content-scripts/website-context/init.js', - '/content-scripts/website-context/mutations.js' -], function () { - extension.ready = true; - - extension.events.trigger('init'); -}); - -document.addEventListener('DOMContentLoaded', function () { - extension.domReady = true; - - bodyReady(); +chrome.runtime.sendMessage( + { + action: "tab-connected", + }, + function (response) { + if (response) { + extension.tabId = response.tabId; + } + } +); + +extension.inject( + [ + "/content-scripts/website-context/core.js", + "/content-scripts/website-context/functions.js", + "/content-scripts/website-context/youtube-features/appearance.js", + "/content-scripts/website-context/youtube-features/themes.js", + "/content-scripts/website-context/youtube-features/player.js", + "/content-scripts/website-context/youtube-features/playlist.js", + "/content-scripts/website-context/youtube-features/channel.js", + "/content-scripts/website-context/youtube-features/shortcuts.js", + "/content-scripts/website-context/youtube-features/blacklist.js", + "/content-scripts/website-context/youtube-features/settings.js", + "/content-scripts/website-context/init.js", + "/content-scripts/website-context/mutations.js", + ], + function () { + extension.ready = true; + + extension.events.trigger("init"); + } +); + +document.addEventListener("DOMContentLoaded", function () { + extension.domReady = true; + + bodyReady(); }); +let prevRequestAction = ""; chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { - if (request.action === 'focus') { - extension.messages.send({ - focus: true - }); - } else if (request.action === 'blur') { - extension.messages.send({ - blur: true - }); - } else if (request.action === 'pause') { - extension.messages.send({ - pause: true - }); - } else if (request.action === 'set-volume') { - extension.messages.send({ - setVolume: request.value - }); - } else if (request.action === 'set-playback-speed') { - extension.messages.send({ - setPlaybackSpeed: request.value - }); - } else if (request.action === 'mixer') { - extension.messages.send({ - mixer: true - }, sendResponse, 'mixer'); - - return true; - } else if (request.action === 'delete-youtube-cookies') { - extension.messages.send({ - deleteCookies: true - }); - } else if (request.action === 'another-video-started-playing') { - extension.features.onlyOnePlayerInstancePlaying(); - } + if (request.action === "focus") { + extension.messages.send({ + focus: true, + }); + } else if (request.action === "blur") { + extension.messages.send({ + blur: true, + }); + } else if (request.action === "pause") { + extension.messages.send({ + pause: true, + }); + } else if (request.action === "set-volume") { + extension.messages.send({ + setVolume: request.value, + }); + } else if (request.action === "set-playback-speed") { + extension.messages.send({ + setPlaybackSpeed: request.value, + }); + } else if (request.action === "mixer") { + extension.messages.send( + { + mixer: true, + }, + sendResponse, + "mixer" + ); + + return true; + } else if (request.action === "delete-youtube-cookies") { + 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") { + extension.features.onlyOnePlayerInstancePlaying(); + } + + prevRequestAction = request.action; }); -document.addEventListener('it-message-from-youtube', function () { - var provider = document.querySelector('#it-messages-from-youtube'); - - if (provider) { - var message = provider.textContent; - - document.dispatchEvent(new CustomEvent('it-message-from-youtube--readed')); - - try { - message = JSON.parse(message); - } catch (error) { - console.log(error); - } - - //console.log(message); - - if (message.requestOptionsUrl === true) { - extension.messages.send({ - responseOptionsUrl: chrome.runtime.getURL('options-page/index.html') - }); - } else if (message.onlyOnePlayer === true) { - chrome.runtime.sendMessage({ - name: 'only-one-player' - }); - } else if (message.action === 'analyzer') { - if (extension.storage.data.analyzer_activation === true) { - var data = message.name, - date = new Date().toDateString(), - hours = new Date().getHours() + ':00'; - - if (!extension.storage.data.analyzer) { - extension.storage.data.analyzer = {}; - } - - if (!extension.storage.data.analyzer[date]) { - extension.storage.data.analyzer[date] = {}; - } - - if (!extension.storage.data.analyzer[date][hours]) { - extension.storage.data.analyzer[date][hours] = {}; - } - - if (!extension.storage.data.analyzer[date][hours][data]) { - extension.storage.data.analyzer[date][hours][data] = 0; - } - - extension.storage.data.analyzer[date][hours][data]++; - - chrome.storage.local.set({ - analyzer: extension.storage.data.analyzer - }); - } - } else if (message.action === 'blacklist') { - var type = message.type, - id = message.id, - title = message.title; - - if (!extension.storage.data.blacklist || typeof extension.storage.data.blacklist !== 'object') { - extension.storage.data.blacklist = {}; - } - - if (type === 'channel') { - if (!extension.storage.data.blacklist.channels) { - extension.storage.data.blacklist.channels = {}; - } - - extension.storage.data.blacklist.channels[id] = { - title: title, - preview: message.preview - }; - } - - if (type === 'video') { - if (!extension.storage.data.blacklist.videos) { - extension.storage.data.blacklist.videos = {}; - } - - extension.storage.data.blacklist.videos[id] = { - title: title - }; - } - - chrome.storage.local.set({ - blacklist: extension.storage.data.blacklist - }); - } else if (message.action === 'watched') { - if (!extension.storage.data.watched || typeof extension.storage.data.watched !== 'object') { - extension.storage.data.watched = {}; - } - - if (message.type === 'add') { - extension.storage.data.watched[message.id] = { - title: message.title - }; - } - - if (message.type === 'remove') { - delete extension.storage.data.watched[message.id]; - } - - chrome.storage.local.set({ - watched: extension.storage.data.watched - }); - } - } +document.addEventListener("it-message-from-youtube", function () { + var provider = document.querySelector("#it-messages-from-youtube"); + + if (provider) { + var message = provider.textContent; + + document.dispatchEvent(new CustomEvent("it-message-from-youtube--readed")); + + try { + message = JSON.parse(message); + } catch (error) { + console.log(error); + } + + //console.log(message); + + if (message.requestOptionsUrl === true) { + extension.messages.send({ + responseOptionsUrl: chrome.runtime.getURL("options-page/index.html"), + }); + } else if (message.onlyOnePlayer === true) { + chrome.runtime.sendMessage({ + name: "only-one-player", + }); + } else if (message.action === "analyzer") { + if (extension.storage.data.analyzer_activation === true) { + var data = message.name, + date = new Date().toDateString(), + hours = new Date().getHours() + ":00"; + + if (!extension.storage.data.analyzer) { + extension.storage.data.analyzer = {}; + } + + if (!extension.storage.data.analyzer[date]) { + extension.storage.data.analyzer[date] = {}; + } + + if (!extension.storage.data.analyzer[date][hours]) { + extension.storage.data.analyzer[date][hours] = {}; + } + + if (!extension.storage.data.analyzer[date][hours][data]) { + extension.storage.data.analyzer[date][hours][data] = 0; + } + + extension.storage.data.analyzer[date][hours][data]++; + + chrome.storage.local.set({ + analyzer: extension.storage.data.analyzer, + }); + } + } else if (message.action === "blacklist") { + var type = message.type, + id = message.id, + title = message.title; + + if ( + !extension.storage.data.blacklist || + typeof extension.storage.data.blacklist !== "object" + ) { + extension.storage.data.blacklist = {}; + } + + if (type === "channel") { + if (!extension.storage.data.blacklist.channels) { + extension.storage.data.blacklist.channels = {}; + } + + extension.storage.data.blacklist.channels[id] = { + title: title, + preview: message.preview, + }; + } + + if (type === "video") { + if (!extension.storage.data.blacklist.videos) { + extension.storage.data.blacklist.videos = {}; + } + + extension.storage.data.blacklist.videos[id] = { + title: title, + }; + } + + chrome.storage.local.set({ + blacklist: extension.storage.data.blacklist, + }); + } else if (message.action === "watched") { + if ( + !extension.storage.data.watched || + typeof extension.storage.data.watched !== "object" + ) { + extension.storage.data.watched = {}; + } + + if (message.type === "add") { + extension.storage.data.watched[message.id] = { + title: message.title, + }; + } + + if (message.type === "remove") { + delete extension.storage.data.watched[message.id]; + } + + chrome.storage.local.set({ + watched: extension.storage.data.watched, + }); + } + } }); -document.addEventListener('it-play', function (event) { - var videos = document.querySelectorAll('video'); +document.addEventListener("it-play", function (event) { + var videos = document.querySelectorAll("video"); - chrome.runtime.sendMessage({ - action: 'play' - }); -}); \ No newline at end of file + chrome.runtime.sendMessage({ + action: "play", + }); +});