Skip to content

Commit 5346527

Browse files
committed
refactor: use sleep function instead of setTimeout
1 parent 4e590e4 commit 5346527

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

user script/ytm-playlist-filter.user.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name YouTube Music Save to Playlist filter
33
// @namespace fred.vatin.ytm-playlists-filter
4-
// @version 1.0.0
4+
// @version 1.0.1
55
// @description Tap P key to open the “save to playlist” menu where your can type to filter
66
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
77
// @author Fred Vatin
@@ -345,10 +345,10 @@
345345
for (const mutation of mutationsList) {
346346
const AriaHidden = mutation.target.ariaHidden;
347347

348-
// console.log(
349-
// `mutation type: ${mutation.type}, AriaHidden: ${AriaHidden}, attribute changed: ${mutation.attributeName}`,
350-
// mutation.target
351-
// );
348+
console.log(
349+
`callback_MenuOpen triggered. mutation type: ${mutation.type}, AriaHidden: ${AriaHidden}, attribute changed: ${mutation.attributeName}`,
350+
mutation.target
351+
);
352352

353353
if (!AriaHidden) {
354354
setFocus(InputId);
@@ -562,26 +562,26 @@
562562

563563
// Collect all items
564564
listItems = Array.from(playlists.querySelectorAll(selector_ListItems));
565-
console.log("✅ autoTopList(playlist), listItems number: ", listItems.length);
565+
console.log("✅ autoTopList(playlists), listItems number: ", listItems.length);
566566

567567
// Separates the selected ones from the rest
568568
const selected = listItems.filter((item) => item.querySelector(selector_SelItems));
569569
const unselected = listItems.filter((item) => !item.querySelector(selector_SelItems));
570570

571571
if (selected.length > 0) {
572-
console.log(`✅ autoTopList(playlist), this video belongs to ${selected.length} playlist(s)`);
572+
console.log(`✅ autoTopList(playlists), this video belongs to ${selected.length} playlist(s)`);
573573
// Reinsert in the desired order
574574
[...selected, ...unselected].forEach((item) => {
575575
playlists.appendChild(item);
576576
});
577577

578-
console.log(`✅ autoTopList(playlist), playlists have been sorted.`);
578+
console.log(`✅ autoTopList(playlists), playlists have been sorted.`);
579579

580580
const sortedItems = Array.from(playlists.querySelectorAll(selector_ListItems));
581-
console.log("✅ autoTopList(playlist), sortedItems number: ", sortedItems.length);
581+
console.log("✅ autoTopList(playlists), sortedItems number: ", sortedItems.length);
582582
} else {
583583
console.log(
584-
"❌ autoTopList(playlist): this video doesn’t belong to any existing playlist. Not sorting needed."
584+
"❌ autoTopList(playlists): this video doesn’t belong to any existing playlist. Not sorting needed."
585585
);
586586
}
587587
}
@@ -593,7 +593,7 @@
593593
*
594594
* @param {string} el - The ID of the input element to focus.
595595
*/
596-
function setFocus(el) {
596+
async function setFocus(el) {
597597
const input = document.getElementById(el);
598598
if (input) {
599599
// reset filter
@@ -610,9 +610,8 @@
610610
const timeout = 100;
611611
console.log(`✅ "input" found. Set focus! Timeout = ${timeout}`);
612612
// delay required to set the focus
613-
setTimeout(() => {
614-
input.focus();
615-
}, timeout);
613+
await sleep(timeout);
614+
input.focus();
616615
} else {
617616
console.log(`❌ "input" not found. Focus not set!`);
618617
}

0 commit comments

Comments
 (0)