diff --git a/extension/content/helpers.js b/extension/content/helpers.js index ced442837..8a16787c6 100644 --- a/extension/content/helpers.js +++ b/extension/content/helpers.js @@ -41,13 +41,18 @@ this.helpers = (function() { waitForSelector(selector, options) { const interval = (options && options.interval) || 50; const timeout = (options && options.timeout) || 1000; + const minCount = (options && options.minCount) || 1; return new Promise((resolve, reject) => { const start = Date.now(); const id = setInterval(() => { - const result = document.querySelector(selector); - if (result) { + const result = document.querySelectorAll(selector); + if (result.length && result.length >= minCount) { clearTimeout(id); - resolve(result); + if (options && options.all) { + resolve(result); + } else { + resolve(result[0]); + } return; } if (Date.now() > start + timeout) { diff --git a/extension/services/spotify/player.js b/extension/services/spotify/player.js index 7bb1147f2..4ff15a714 100644 --- a/extension/services/spotify/player.js +++ b/extension/services/spotify/player.js @@ -1,8 +1,7 @@ -/* globals log, helpers */ +/* globals helpers */ this.player = (function() { - const SEARCH_PLAY = ".tracklist-play-pause"; - const TOP_PLAY = ".top-artist .cover-art-playback"; + const SEARCH_PLAY = "#searchPage button[aria-label='Play']"; class Player extends helpers.Runner { action_play() { @@ -13,25 +12,14 @@ this.player = (function() { async action_search({ query, thenPlay }) { const searchButton = this.querySelector("a[aria-label='Search']"); searchButton.click(); - const input = await this.waitForSelector(".SearchInputBox input"); + const input = await this.waitForSelector("div[role=search] input"); this.setReactInputValue(input, query); if (thenPlay) { - await this.waitForSelector(`${SEARCH_PLAY}, ${TOP_PLAY}`, { + const playerButton = await this.waitForSelector(SEARCH_PLAY, { timeout: 2000, + // There seem to be 3 fixed buttons that appear early before the search results + minCount: 4, }); - const mainPlayer = this.querySelectorAll(TOP_PLAY); - let playerButton; - if (mainPlayer.length) { - if (mainPlayer.length > 1) { - log.info( - "Got multiple results for query .top-artist .cover-art-playback :", - mainPlayer - ); - } - playerButton = mainPlayer[0]; - } else { - playerButton = this.querySelector(SEARCH_PLAY); - } playerButton.click(); } }