From 3434311138ccdaffd35a6c4cd7e8a0268f88063e Mon Sep 17 00:00:00 2001 From: Oleksandr Karpov Date: Fri, 16 Nov 2018 09:24:43 +0200 Subject: [PATCH 1/2] added YouTube music support --- background.js | 1 + content.js | 76 ++++++++++++++++++++++++++++++--------------------- package.json | 2 +- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/background.js b/background.js index c07ebed..42b63f3 100644 --- a/background.js +++ b/background.js @@ -11,6 +11,7 @@ var debug = false; var providersList = ["vk.com", "new.vk.com", + "music.youtube.com", "gaming.youtube.com", "youtube.com", "vimeo.com", diff --git a/content.js b/content.js index dbc9ea3..d3722d3 100644 --- a/content.js +++ b/content.js @@ -1,5 +1,15 @@ /* StoPlay Content JS */ +function safeGetElementTextContentByQuery(query) { + try { + const element = document.querySelector(query); + + return element.textContent; + } catch (e) { + return ""; + } +} + var StoPlay = { injectScript: function (scriptText) { var script = document.createElement('script'); @@ -18,8 +28,6 @@ var Status = { }; var Provider = function () { - var _this = this; - this.allowed = []; this.enabled = true; this.LOG = 'STOPLAY'; @@ -73,15 +81,16 @@ Provider.prototype._parseChanges = function(changes) { }; Provider.prototype._parseAllowedProviders = function(providers) { - if (!providers.length) return; - var allowed = []; - allowed = providers.filter(function(provider) { + if (!providers.length) { + return; + } + + this.allowed = providers.filter(function (provider) { // check if any of the providers is disabled return provider.enabled === true; - }).map(function(provider) { + }).map(function (provider) { return provider.uri; }); - this.allowed = allowed; }; Provider.prototype._detectProviderAndStartCheckInterval = function () { @@ -161,7 +170,7 @@ Provider.prototype.attachEvents = function () { }; Provider.prototype.__changeState = function (status) { - if (status != this.status || status == Status.PLAYING) { + if (status !== this.status || status === Status.PLAYING) { switch(status) { case Status.PLAYING: this.trigger( 'start' ); @@ -175,23 +184,25 @@ Provider.prototype.__changeState = function (status) { }; Provider.prototype.getTitle = function () { - var title = ''; + let songName; + let artistName; - switch(this.host) { + switch (this.host) { + case "music.youtube.com": + songName = safeGetElementTextContentByQuery(".ytmusic-player-bar.title"); + artistName = safeGetElementTextContentByQuery(".ytmusic-player-bar.byline a:first-child"); + break; case "play.google.com": - var songNameNode = document.getElementById('currently-playing-title'); - var songArtistNode = document.getElementById('player-artist'); - - if (songNameNode && songArtistNode) { - var songName = songNameNode.textContent; - var songArtist = songArtistNode.textContent; - - title = songArtist + ' - ' + songName; - } + songName = safeGetElementTextContentByQuery("#currently-playing-title"); + artistName = safeGetElementTextContentByQuery("#player-artist"); break; } - return title; + if (artistName && songName) { + return `${artistName} - ${songName}`; + } + + return ""; }; Provider.prototype.checkTitle = function () { @@ -284,12 +295,13 @@ Provider.prototype.checkStatus = function () { case "ted.com": case "facebook.com": case "kickstarter.com": - var videos = document.getElementsByTagName('video'); + case "music.youtube.com": + const videos = document.getElementsByTagName("video"); if (videos.length > 0) { status = Status.PAUSED; - for (var i = 0; i < videos.length; i++) { + for (let i = 0; i < videos.length; i++) { if (videos[i] && !videos[i].paused) { status = Status.PLAYING; } @@ -403,7 +415,7 @@ Provider.prototype.checkStatus = function () { } break; case "deezer.com": - localStorageState = window.localStorage.getItem('stoplaystate'); + var localStorageState = window.localStorage.getItem('stoplaystate'); status = localStorageState ? localStorageState : null; break; case "coursera.org": @@ -476,7 +488,7 @@ Provider.prototype.checkAnnoyingLightboxes = function () { Provider.prototype.pause = function () { var p; - if (this.status == Status.PLAYING) { + if (this.status === Status.PLAYING) { switch(this.host) { case "vk.com": document.querySelector('.top_audio_player_play').click(); @@ -520,9 +532,10 @@ Provider.prototype.pause = function () { case "ted.com": case 'facebook.com': case "kickstarter.com": - var videos = document.getElementsByTagName('video'); + case "music.youtube.com": + const videos = document.getElementsByTagName("video"); - for (var i = 0; i < videos.length; i++) { + for (let i = 0; i < videos.length; i++) { if (videos[i] && !videos[i].paused) { videos[i].pause(); } @@ -686,7 +699,7 @@ Provider.prototype.pause = function () { Provider.prototype.play = function () { var p; - if (this.status != Status.PLAYING) { + if (this.status !== Status.PLAYING) { switch(this.host) { case "vk.com": document.querySelector('.top_audio_player_play').click(); @@ -730,9 +743,10 @@ Provider.prototype.play = function () { case "ted.com": case 'facebook.com': case "kickstarter.com": - var videos = document.getElementsByTagName('video'); + case "music.youtube.com": + const videos = document.getElementsByTagName("video"); - for (var i = 0; i < videos.length; i++) { + for (let i = 0; i < videos.length; i++) { if (videos[i] && videos[i].paused && videos[i].played.length > 0) { videos[i].play(); } @@ -899,11 +913,11 @@ var ProviderInstance = new Provider(); if (ProviderInstance) { chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { - if (request.action == 'pause') { + if (request.action === 'pause') { ProviderInstance.pause(); } - if (request.action == 'play') { + if (request.action === 'play') { ProviderInstance.play(); } }); diff --git a/package.json b/package.json index f2b31b1..39d23c2 100644 --- a/package.json +++ b/package.json @@ -15,4 +15,4 @@ "grunt-webstore-upload": "^0.9.21", "grunt-zip": "*" } -} \ No newline at end of file +} From 6e22385575f77bc9ddb97ad236c54d0c0fe2c7a9 Mon Sep 17 00:00:00 2001 From: Oleksandr Karpov Date: Fri, 16 Nov 2018 13:44:40 +0200 Subject: [PATCH 2/2] refactor code --- content.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/content.js b/content.js index 17ca9fe..cfa5514 100644 --- a/content.js +++ b/content.js @@ -307,13 +307,9 @@ Provider.prototype.checkStatus = function () { const videos = document.getElementsByTagName("video"); if (videos.length > 0) { - status = Status.PAUSED; + var hasPlayingVideo = Array.from(videos).some((player) => !player.paused); - for (let i = 0; i < videos.length; i++) { - if (videos[i] && !videos[i].paused) { - status = Status.PLAYING; - } - } + status = hasPlayingVideo ? Status.PLAYING : Status.PAUSED; } break; @@ -555,11 +551,11 @@ Provider.prototype.pause = function () { case "music.youtube.com": const videos = document.getElementsByTagName("video"); - for (let i = 0; i < videos.length; i++) { - if (videos[i] && !videos[i].paused) { - videos[i].pause(); - } - } + Array.from(videos) + .filter((player) => !player.paused) + .forEach((player) => { + player.pause(); + }); break; case "gaming.youtube.com": @@ -778,11 +774,11 @@ Provider.prototype.play = function () { case "music.youtube.com": const videos = document.getElementsByTagName("video"); - for (let i = 0; i < videos.length; i++) { - if (videos[i] && videos[i].paused && videos[i].played.length > 0) { - videos[i].play(); - } - } + Array.from(videos) + .filter((player) => player.paused && player.played.length > 0) + .forEach((player) => { + player.play(); + }); break; case "gaming.youtube.com":