NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Yandex Music & Radio MediaApi Support
// @version 0.1.1
// @author Syleront
// @include /(music|radio)\.yandex\.ru/
// @run-at document-start
// @license MIT
// @copyright 2018, Syleront
// ==/UserScript==
(() => {
const { navigator } = unsafeWindow;
if ("mediaSession" in navigator) {
document.addEventListener("DOMContentLoaded", () => {
const { externalAPI } = unsafeWindow;
const {
EVENT_TRACK,
getCurrentTrack,
togglePause,
prev,
next
} = externalAPI;
externalAPI.on(EVENT_TRACK, () => {
const current = getCurrentTrack();
if (current !== null) {
const { title, artists, cover } = current;
const artist = artists.map((e) => e.title).join(", ");
const album = current.album.title;
const artwork = [{
src: "https://" + cover.replace(/%%$/, "200x200"),
sizes: "200x200",
type: "image/jpeg"
}];
navigator.mediaSession.metadata = new MediaMetadata({ title, artist, album, artwork });
}
});
navigator.mediaSession.setActionHandler("play", () => togglePause());
navigator.mediaSession.setActionHandler("pause", () => togglePause());
navigator.mediaSession.setActionHandler("nexttrack", () => next());
if (location.hostname !== "radio.yandex.ru") { // radio changes tracks only to the next
navigator.mediaSession.setActionHandler("previoustrack", () => prev());
}
});
}
})();