-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-queue.user.js
81 lines (77 loc) · 2.54 KB
/
youtube-queue.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ==UserScript==
// @name YoutubeAllResultsPushToQueuePlay
// @namespace https://userscript.snomiao.com/
// @version 0.0.7
// @description Youtube Search Results Pages Push To Queue To Play Button
// @author [email protected]
// @copyright 2017 - 2023, @snomiao <snomiao.com>
// @match *://www.youtube.com/results*
// @match *://youtube.com/results*
// @supportURL https://github.com/snomiao/userscript.js/issues
// @contributionURL https://snomiao.com/donate
// @grant none
// @noframes
// @license GPL-3.0+
// ==/UserScript==
const $$ = (sel) => [...document.querySelectorAll(sel)];
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const waitFor = async (qf, timeout = 1000, interval = 500) => {
const ts = +new Date();
while (+new Date() - ts <= timeout) {
let re = await qf();
if (undefined !== re && null !== re) return re;
await sleep(interval);
}
return null;
};
const waitForElement = async (e, sel) =>
await waitFor(() => e.querySelector(sel));
const menuClick = async (e) => {
e.style.background = "#FF0";
// expand and click
(await waitForElement(e, ".dropdown-trigger button")).click();
// await 睡(500);
(
await waitForElement(
document,
"tp-yt-iron-dropdown[focused] ytd-menu-service-item-renderer"
)
).click();
// fold... and wait it close
(await waitForElement(e, ".dropdown-trigger button")).click();
await waitForElement(
document,
`tp-yt-iron-dropdown[aria-hidden] ytd-menu-service-item-renderer`
);
e.style.background = "";
await sleep(100);
};
const AllResultsPushToQueuePlay = async function () {
// clean list
// ytp-miniplayer-close-button
const vs = $$("ytd-video-renderer");
for await (const e of vs) {
await menuClick(e);
}
};
const elementCreate = (innerHTML, attributes = {}) => {
return Object.assign(
Object.assign(document.createElement("div"), { innerHTML }).children[0],
attributes
);
};
function btnAdd() {
const onclick = () => AllResultsPushToQueuePlay();
const e = elementCreate(
`<button><div>依次播放捜索結果<br>Queue All Results</div></button>`,
{ onclick }
);
const filterBtn = $$("ytd-toggle-button-renderer")[0];
if (!filterBtn) return setTimeout(btnAdd, 1000);
filterBtn.AllResultsPushToQueuePlay?.remove();
filterBtn.AllResultsPushToQueuePlay = e;
filterBtn.parentElement.append(e);
}
document.addEventListener("load", btnAdd, false);
window.addEventListener("load", btnAdd, false);
btnAdd();