-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsoundcloud-toggle-continuous-play.user.js
159 lines (149 loc) · 4.21 KB
/
soundcloud-toggle-continuous-play.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ==UserScript==
// @name SoundCloud Toggle Continuous Play and Autoplay
// @namespace https://greasyfork.org/users/649
// @version 1.1.21
// @description Adds options to toggle continuous play and autoplay in SoundCloud
// @author Adrien Pyke
// @match *://soundcloud.com/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @require https://cdn.jsdelivr.net/gh/kufii/My-UserScripts@22210afba13acf7303fc91590b8265faf3c7eda7/libs/gm_config.js
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// ==/UserScript==
(() => {
'use strict';
const SCRIPT_NAME = 'SoundCloud Toggle Continuous Play and Autoplay';
const Util = {
log(...args) {
args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
console.log(...args);
},
q(query, context = document) {
return context.querySelector(query);
},
qq(query, context = document) {
return Array.from(context.querySelectorAll(query));
},
createCheckbox(lbl) {
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
label.appendChild(checkbox);
label.appendChild(document.createTextNode(lbl));
return label;
}
};
const Config = GM_config([
{
key: 'autoplay',
label: 'Autoplay',
default: false,
type: 'bool'
},
{
key: 'continuousPlay',
label: 'Continuous Play',
default: false,
type: 'bool'
}
]);
GM_registerMenuCommand('SoundCloud Autoplay', Config.setup);
const App = {
playButton: Util.q('.playControl'),
isPlaying() {
return App.playButton.classList.contains('playing');
},
pause() {
if (App.isPlaying()) {
App.playButton.click();
}
},
play() {
if (!App.isPlaying()) {
App.playButton.click();
}
},
getPlaying() {
const link = Util.q('a.playbackSoundBadge__title');
if (link) {
return link.href;
}
return null;
},
addAutoplayControl() {
const container = Util.q('.playControls__inner');
const label = Util.createCheckbox('Autoplay');
label.setAttribute(
'style',
'position: absolute; bottom: 0; right: 0; z-index: 1;'
);
container.appendChild(label);
const check = Util.q('input', label);
check.checked = Config.load().continuousPlay;
check.onchange = () => {
const cfg = Config.load();
cfg.continuousPlay = check.checked;
Config.save(cfg);
};
return check;
}
};
// disable autoplay
if (!Config.load().autoplay) {
App.pause();
}
const autoplayControl = App.addAutoplayControl();
let current = App.getPlaying();
let timeout;
// every time the song changes
waitForElems({
context: Util.q('.playControls__soundBadge'),
onchange() {
const next = App.getPlaying();
if (!autoplayControl.checked && current && next !== current) {
timeout = setTimeout(() => {
Util.log('Pausing...');
App.pause();
}, 0);
}
current = next;
}
});
// override the click event for elements that shouldn't trigger a pause
waitForElems({
sel: '.skipControl, .playButton, .compactTrackList__item, .fullListenHero__foreground',
onmatch(elem) {
elem.addEventListener('click', () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
});
}
});
// waveforms need to be handled differently
waitForElems({
sel: '.waveform__layer',
onmatch(elem) {
elem.addEventListener('click', () => {
setTimeout(() => {
if (!App.isPlaying()) {
Util.log('Playing via Waveform');
App.play();
}
}, 0);
});
}
});
// fix for buttons constantly showing buffering
waitForElems({
sel: '.sc-button-buffering',
onmatch(button) {
if (!App.isPlaying()) {
button.classList.remove('sc-button-buffering');
button.title = button.textContent = 'Play';
}
}
});
})();