Skip to content

Instantly share code, notes, and snippets.

@Kreozot
Last active January 19, 2023 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kreozot/f22c6f625d66f2122bb9d5f04c6c0ec7 to your computer and use it in GitHub Desktop.
Save Kreozot/f22c6f625d66f2122bb9d5f04c6c0ec7 to your computer and use it in GitHub Desktop.
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