-
Notifications
You must be signed in to change notification settings - Fork 0
/
arc-boost.js
155 lines (137 loc) · 4.35 KB
/
arc-boost.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
const MAX_PROGRESS = 226;
const formatDuration = (duration) => {
if (!isNaN(duration)) {
const minutes = Math.floor(duration / 60);
const seconds = Math.floor(duration % 60);
return `${minutes.toString().padStart(2, "0")}:${seconds
.toString()
.padStart(2, "0")}`;
} else {
return "00:00";
}
};
const getAudioElement = () => {
if (!window.audioElement) {
window.audioElement = document.querySelector("audio[src*='bcbits.com']");
}
return window.audioElement;
};
const createPlayer = () => {
const newPlayer = document.createElement("div");
newPlayer.id = "new-player";
newPlayer.style.cssText = `
position: fixed;
bottom: 0;
width: 100%;
height: 10%;
display: flex;
align-items: center;
justify-content: center;
background-color: white;
flex: 1;
flex-direction: column;
z-index: 9999;
`;
// Add bottom padding to the body equal to the height of the new player
document.body.style.paddingBottom = "10%";
return newPlayer;
};
const createButton = (text, onClick) => {
const button = document.createElement("button");
button.innerText = text;
button.style.cssText = `
min-width: 54px;
min-height: 50px;
`;
button.onclick = onClick;
return button;
};
const createToolbarButtons = (existingPlayer, playButtonElement) => {
const toolbar = document.createElement("div");
toolbar.classList.add("toolbar");
toolbar.style.display = "flex";
toolbar.style.alignItems = "center";
const prevButton = createButton("⏮", () =>
existingPlayer.querySelector(".prevbutton").click()
);
const playButton = createButton(
playButtonElement.classList.contains("playing") ? "⏸" : "▶",
() => {
playButtonElement.click();
}
);
const nextButton = createButton("⏭", () =>
existingPlayer.querySelector(".nextbutton").click()
);
toolbar.append(prevButton, playButton, nextButton);
return toolbar;
};
const createProgressInput = () => {
const progressInput = document.createElement("input");
progressInput.type = "range";
progressInput.min = "0";
progressInput.max = MAX_PROGRESS.toString();
progressInput.value = "0";
progressInput.style.width = "450px";
progressInput.addEventListener("input", () => {
const audio = getAudioElement();
if (audio) {
const newTime = audio.duration * (progressInput.value / MAX_PROGRESS);
audio.currentTime = newTime;
audio.play();
}
});
return progressInput;
};
const createRange = () => {
const range = document.createElement("div");
range.classList.add("range");
range.style.display = "flex";
range.style.alignItems = "center";
const currentTimeElement = document.createElement("div");
const progressInput = createProgressInput();
const totalDurationElement = document.createElement("div");
range.append(currentTimeElement, progressInput, totalDurationElement);
return range;
};
const observeProgressBar = (existingPlayer, range) => {
const progressBar = existingPlayer.querySelector(".ui-draggable");
const observer = new MutationObserver(() => {
const audio = getAudioElement();
if (audio) {
const leftStyle = progressBar.style.left;
const widthPercent = parseFloat(leftStyle);
range.children[1].value = widthPercent;
range.children[0].textContent = formatDuration(audio.currentTime);
range.children[2].textContent = formatDuration(audio.duration);
}
});
observer.observe(progressBar, {
attributes: true,
attributeFilter: ["style"],
});
};
const observePlayButton = (existingPlayButton, newPlayButton) => {
const observer = new MutationObserver(() => {
newPlayButton.innerText = existingPlayButton.classList.contains("playing")
? "⏸"
: "▶";
});
observer.observe(existingPlayButton, {
attributes: true,
attributeFilter: ["class"],
});
};
const createToolbar = () => {
const existingPlayer = document.querySelector(".inline_player");
const playButtonElement = existingPlayer.querySelector(".playbutton");
const newPlayer = createPlayer();
const toolbar = createToolbarButtons(existingPlayer, playButtonElement);
const range = createRange();
newPlayer.appendChild(toolbar);
newPlayer.appendChild(range);
document.body.appendChild(newPlayer);
observeProgressBar(existingPlayer, range);
observePlayButton(playButtonElement, toolbar.children[1]);
};
window.onload = createToolbar;