Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Fix #386, fix Spotify
Browse files Browse the repository at this point in the history
Spotify appears to have undergone a redesign.
  • Loading branch information
ianb committed Oct 10, 2019
1 parent 8c7b623 commit 258740d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
11 changes: 8 additions & 3 deletions extension/content/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 6 additions & 18 deletions extension/services/spotify/player.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 258740d

Please sign in to comment.