-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyt-music-control.js
executable file
·91 lines (83 loc) · 3.32 KB
/
yt-music-control.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
var video = document.querySelector("video"); // get HTML elements of YT music page like the video, play pause button, etc...
var playPauseButton = document.getElementById("play-pause-button");
var nextButton = document.getElementsByClassName("next-button")[0];
var previousButton = document.getElementsByClassName("previous-button")[0];
var shuffleButton = document.querySelector(".shuffle.ytmusic-player-bar");
var albumImageURL =
document.querySelector("#song-image").children[0].children[0].src;
if (albumImageURL != null && albumImageURL != undefined) {
browser.runtime.sendMessage({
greeting: "Song change",
newURL: albumImageURL,
});
}
browser.runtime.onMessage.addListener(handleMessage);
/* To check if url (song) changes */
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
URLChange();
}
}).observe(document, { subtree: true, childList: true });
function URLChange() {
browser.runtime.sendMessage({
greeting: "Song change",
newURL: document.querySelector("#song-image").children[0].children[0].src,
});
}
function handleMessage(request, sender, sendResponse) {
if (request.greeting == "Play-Pause") {
playPauseButton.click();
} else if (request.greeting == "Shuffle") {
shuffleButton.click();
} else if (request.greeting == "Next") {
nextButton.click();
browser.runtime.sendMessage({
greeting: "Song change",
newURL: document.querySelector("#song-image").children[0].children[0].src,
songInfo:
document.getElementsByClassName("ytp-title-link")[0].innerHTML +
" • " +
document.querySelector(".byline.ytmusic-player-bar").title,
playPauseStatus: document
.getElementById("play-pause-button")
.getAttribute("title"),
});
} else if (request.greeting == "Previous") {
previousButton.click();
browser.runtime.sendMessage({
greeting: "Song change",
newURL: document.querySelector("#song-image").children[0].children[0].src,
songInfo:
document.getElementsByClassName("ytp-title-link")[0].innerHTML +
" • " +
document.querySelector(".byline.ytmusic-player-bar").textContent,
playPauseStatus: document
.getElementById("play-pause-button")
.getAttribute("title"),
});
} else if (request.greeting == "Volume") {
//change volume
if (video) {
document.getElementsByClassName("volume-slider")[0].setAttribute("value", request.vol); // update volume slider in UI
video.volume = request.vol / 100; // update video volume, divide by 100 as volume must be between [0,1]
}
} else if (request.greeting == "Album art") {
//update album art and volume
return Promise.resolve({
response:
document.querySelector("#song-image").children[0].children[0].src, //send back album art to popup
songInfo:
document.getElementsByClassName("ytp-title-link")[0].innerHTML +
" • " +
document.querySelector(".byline.ytmusic-player-bar").textContent, //send back track info to popup
volume: video.volume * 100, // send back volume info to popup
playPauseStatus: document
.getElementById("play-pause-button")
.getAttribute("title"),
});
}
return Promise.resolve({ response: "0" }); //return 0 if no response needed
}