Skip to content

Instantly share code, notes, and snippets.

@uhwot
Last active April 7, 2024 00:15
Show Gist options
  • Save uhwot/1b97f5b806fdf1424377ddb86446d912 to your computer and use it in GitHub Desktop.
Save uhwot/1b97f5b806fdf1424377ddb86446d912 to your computer and use it in GitHub Desktop.
Apple Music Formats Userscript
// ==UserScript==
// @name Apple Music Formats
// @namespace io.github.uhwot.amusicformats
// @match https://music.apple.com/*
// @grant none
// @run-at document-start
// @version 1.0.4
// @author uh wot
// @description Shows formats available on Apple Music albums. Based on https://github.com/bunnykek/AppleMusic-Formats-Extension
// @icon https://music.apple.com/assets/favicon/favicon-180-f10a76334177ea08c0b3b35b0269fe16.png
// @homepageURL https://gist.github.com/uhwot/1b97f5b806fdf1424377ddb86446d912
// @downloadURL https://gist.github.com/uhwot/1b97f5b806fdf1424377ddb86446d912/raw/amusic_formats.user.js
// ==/UserScript==
const QUERY = '.headings__metadata-bottom'
let audioTraits
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function insert_html() {
let str = '<div class="headings__metadata-bottom svelte-1ug8xq0">AVAILABLE IN '
str += audioTraits.join(" | ");
str += '</p>';
let selector = document.querySelector(QUERY);
selector.insertAdjacentHTML("afterend", str);
}
window.fetch = (function (fetch) {
return async function (url, init) {
let resp = await fetch(url, init)
if (url.startsWith('/')) {
return new Response(JSON.stringify(json), resp)
}
url = new URL(url)
if (url.hostname === 'amp-api.music.apple.com' && /^\/v1\/catalog\/[a-z]{2}\/albums\/\d+/g.test(url.pathname)) {
let json = await resp.json()
if (json.resources === undefined) {
return new Response(JSON.stringify(json), resp)
}
audioTraits = json.resources.albums[json.data[0].id].attributes.audioTraits
waitForElm(QUERY).then((_) => {
console.log('Element is ready');
insert_html();
})
resp = new Response(JSON.stringify(json), resp)
}
return resp
};
})(window.fetch);
@bunnykek
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment