-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediaSession-modifier.user.js
175 lines (166 loc) · 7.43 KB
/
mediaSession-modifier.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// ==UserScript==
// @name mediaSession-modifier
// @version 2024.05.04.01
// @author trmdi
// @namespace trmdi
// @include *
// @grant none
// @description Modify the mediaSession value of websites
// ==/UserScript==
const data = (function(w) {
const websites = {
//
// Add data for websites here
//
"zingmp3.vn": {
song: {
get title() {
let e = document.querySelector(".player-controls .song-title-item a");
return e? e.innerText : null;
},
get artist() {
let e = document.querySelector(".player-controls .media-content .subtitle");
return e? e.innerText.replace(/[ -]*/,"") : null;
},
get album() {
return "ZingMP3";
},
get artwork() {
let e = document.querySelector(".player-controls .thumbnail img");
return e? e.src.replace(/w\d+/,"w1366") : null;
}
},
action: {
play: function() { document.querySelector(".actions button.btn-play").click(); },
previoustrack: function() { document.querySelector(".actions button.btn-pre").click(); },
nexttrack: function() { document.querySelector(".actions button.btn-next").click(); },
pause: function() {
if (!document.querySelector("audio").paused) {
document.querySelector(".actions button.btn-play").click();
}
},
}
},
"open.spotify.com": {
song: {
get title() {
let e = document.querySelector(".track-info__name");
return e? e.innerText : null;
},
get artist() {
let e = document.querySelector(".track-info__artists");
return e? e.innerText : null;
},
get album() {
return "Spotify";
},
get artwork() {
let e = document.querySelector(".now-playing__cover-art .cover-art-image[style*=http]");
return e? e.getAttribute("style").match(/http.*(?=\")/)[0] : null;
}
},
action: {
play: function() { document.querySelector(".control-button[class*=spoticon-play], .control-button[class*=spoticon-pause]").click(); },
previoustrack: function() { document.querySelector(".control-button[class*=spoticon-skip-back]").click(); },
nexttrack: function() { document.querySelector(".control-button[class*=spoticon-skip-forward]").click(); },
}
},
"soundcloud.com": {
song: {
get title() {
let e = document.querySelector(".playbackSoundBadge__titleLink");
return e? e.getAttribute("title") : null;
},
get artist() {
let e = document.querySelector(".playbackSoundBadge__lightLink");
return e? e.getAttribute("title") : null;
},
get album() {
return "SoundCloud";
},
get artwork() {
let e = document.querySelector(".playControls__soundBadge [style*=background-image]");
return e? e.getAttribute("style").match(/http.*(?=\")/)[0].replace(/-t\d+x\d+\./, "-original.") : null;
}
},
action: {
play: function() { document.querySelector("button.playControls__play").click(); },
previoustrack: function() { document.querySelector("button.playControls__prev").click(); },
nexttrack: function() { document.querySelector("button.playControls__next").click(); },
},
updateAgain: function(mutationsList) {
// otherwise, it won't work properly for the first song played
mutationsList.forEach(function(mutation) {
if (this.updateAgainEnabled && mutation.target === document.querySelector(".playbackTimeline__timePassed")) {
this.updateAgainEnabled = false;
console.log("update again...");
updateMediaSession();
}
}, this);
}
},
"www.youtube.com": {
song: {
get title() {
return navigator.mediaSession.metadata? navigator.mediaSession.metadata.title : null;
},
get artist() {
return navigator.mediaSession.metadata? navigator.mediaSession.metadata.artist : null;
},
get album() {
return navigator.mediaSession.metadata? "YouTube" : null;
},
get artwork() {
return navigator.mediaSession.metadata? navigator.mediaSession.metadata.artwork[0].src.replace(/\/([^\/]*?default)(\.jpg)/, "/maxresdefault$2") : null;
}
},
action: {
play: function() { document.querySelector(".ytp-play-button").click(); },
previoustrack: function() { let b = document.querySelector(".ytp-prev-button"); b? b.click() : history.go(-1); },
nexttrack: function() { document.querySelector(".ytp-next-button").click(); },
},
updateAgain: function(mutationsList) {
mutationsList.forEach(function(mutation) {
if (this.updateAgainEnabled && mutation.target === document.querySelector(".ytp-time-current")) {
this.updateAgainEnabled = false;
updateMediaSession();
}
}, this);
}
},
//
// data end
//
};
return websites[w];
})(window.location.hostname);
if (!data) return;
if (window.MediaMetadata === undefined) {
alert("Please enable \"Enhanced Media Controls\" in the Plasma Integration extension's Preferences to allow the script to work.");
return;
}
const mediaMetadata = { title: null, artist: null, album: null, artwork: [{src: null}] };
const updateMediaSession = function() {
navigator.mediaSession.metadata = new window.MediaMetadata(mediaMetadata);
Object.keys(data.action).forEach(function(i) {
navigator.mediaSession.setActionHandler(i, data.action[i]);
});
console.log("mediaSession is updated", mediaMetadata);
}
const callback = function(mutationsList, observer) {
if (mediaMetadata.title === data.song.title && mediaMetadata.artist === data.song.artist
&& mediaMetadata.album === data.song.album && mediaMetadata.artwork[0].src === data.song.artwork) {
if (typeof data.updateAgain === "function" && data.updateAgainEnabled) {
data.updateAgain(mutationsList);
}
return;
}
mediaMetadata.title = data.song.title;
mediaMetadata.artist = data.song.artist;
mediaMetadata.album = data.song.album;
mediaMetadata.artwork = [{src: data.song.artwork}];
updateMediaSession();
data.updateAgainEnabled = true;
}
const songObserver = new MutationObserver(callback);
songObserver.observe(document.body, { childList: true, subtree: true });