-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt-music-stop-current.user.js
53 lines (43 loc) · 1.38 KB
/
yt-music-stop-current.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
// ==UserScript==
// @name YT Music Pause After Current Song
// @namespace http://tampermonkey.net/
// @version 2023-12-28
// @description Stop playing music after the current song
// @author pnicto
// @match https://music.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
function timeToSeconds(time) {
const [minutes, seconds] = time.split(":");
return Number(minutes) * 60 + Number(seconds);
}
(function () {
"use strict";
let btn = document.createElement("button");
btn.innerHTML = "Stop after current song";
btn.id = "stop-after-current-song";
btn.style.position = "fixed";
btn.style.padding = "10px";
btn.style.bottom = "400px";
btn.style.right = "10px";
document.body.appendChild(btn);
btn.addEventListener("click", function () {
const duration = document
.querySelector("#progress-bar")
.getAttribute("aria-valuetext");
const [current, total] = duration.split(" of ");
const currentSeconds = timeToSeconds(current);
const totalSeconds = timeToSeconds(total);
const left = totalSeconds - currentSeconds;
setTimeout(
function () {
var pauseBtn = document.getElementById("play-pause-button");
if (pauseBtn) {
pauseBtn.click();
}
},
(left - 2) * 1000,
);
});
})();