Last active
January 19, 2023 14:57
-
-
Save Kreozot/f22c6f625d66f2122bb9d5f04c6c0ec7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SEARCH_INPUT_SELECTOR = 'div[role="search"] input[role="searchbox"]'; | |
const ADD_BUTTON_SELECTOR = 'div[role="presentation"] > div[role="row"] button[aria-label="Add to Playlist"]'; | |
const input = document.querySelector(SEARCH_INPUT_SELECTOR); | |
const tracks = ['test']; | |
const wait = async (timeout) => new Promise((resolve) => setTimeout(resolve, timeout)); | |
const findTracks = async (track) => { | |
input.value = ''; | |
input.focus(); | |
document.execCommand('insertText', false, track); | |
await wait(3000); | |
} | |
const addFirstFoundTrack = async () => { | |
const button = document.querySelector(ADD_BUTTON_SELECTOR); | |
if (button) { | |
button.click(); | |
await wait(2000); | |
} else { | |
console.warn(`Track "${track}" not found`) | |
} | |
}; | |
const findAndAddTrack = async (track) => { | |
await findTracks(track); | |
await addFirstFoundTrack(); | |
}; | |
const findAndAddTracks = async (tracks) => { | |
await tracks.reduce( | |
async (previousPromise, track) => { | |
await previousPromise; | |
await findAndAddTrack(track); | |
}, | |
Promise.resolve(null) | |
); | |
}; | |
findAndAddTracks(tracks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment