Skip to content

Instantly share code, notes, and snippets.

@nicomollet
Last active October 18, 2023 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomollet/944064e3c36b8eca1fa9bc1067a8c967 to your computer and use it in GitHub Desktop.
Save nicomollet/944064e3c36b8eca1fa9bc1067a8c967 to your computer and use it in GitHub Desktop.
YouTube Music userscript: moves the Save to library button in the player bar
{
"manifest_version": 2,
"content_scripts": [
{
"exclude_globs": [],
"include_globs": [
"*"
],
"js": [
"YoutubeMusicScript.user.js"
],
"matches": [
"https://music.youtube.com/*"
],
"run_at": "document_end"
}
],
"converted_from_user_script": true,
"description": "Youtube Music button 'Save to library' moved in player bar",
"name": "Youtube Music save to library in player",
"author": "Nicolas Mollet",
"version": "1.01"
}
// ==UserScript==
// @name YouTubeMusicScript
// @description YouTubeMusicScript
// @include http://music.youtube.com/*
// @autho Nicolas Mollet
// @version 1.0
// ==/UserScript==
// Observe play-button clicks.
const play_buttons = document.querySelectorAll('#play-button');
play_buttons.forEach(play_button=>{
play_button.onclick = function () {
moveSaveLibraryToPlayerBar();
}
});
// Configure Observer of mutations to catch song change.
const targetNode = document.body;
const config = { childList: true, subtree: true };
const checkMutations = function(mutationsList, observer) {
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
// Detect song has changed
if( mutation.target.className === 'title style-scope ytmusic-player-bar'){
moveSaveLibraryToPlayerBar();
}
// Detect player bad appeared.
if( mutation.target.className === 'menu style-scope ytmusic-player-bar'){
moveSaveLibraryToPlayerBar();
}
}
}
};
const observer = new MutationObserver(checkMutations);
observer.observe(targetNode, config);
// Function that moves the "Library button" to the player bar.
const moveSaveLibraryToPlayerBar = function() {
const playerbar_middle_control_buttons = document.querySelector('.middle-controls-buttons.ytmusic-player-bar');
if(playerbar_middle_control_buttons.lastElementChild.textContent.includes('Remove from library') || playerbar_middle_control_buttons.lastElementChild.textContent.includes('Save to library')){
playerbar_middle_control_buttons.removeChild(playerbar_middle_control_buttons.lastElementChild);
}
// Browse all buttons.
const buttons = document.querySelectorAll('ytmusic-player-bar .ytmusic-menu-renderer button');
buttons.forEach(el=>{
// Open the song menu.
el.click();
setTimeout(() => {
// Find all menu items.
const menu_items = document.querySelectorAll('ytmusic-toggle-menu-service-item-renderer');
menu_items.forEach( menu_item=> {
// Find all menu items elements.
const menu_item_elements = menu_item.querySelectorAll('yt-formatted-string');
menu_item_elements.forEach(menu_item_element=>{
if(menu_item_element.textContent === 'Remove from library' || menu_item_element.textContent === 'Save to library'){
playerbar_middle_control_buttons.append(menu_item_element.parentNode);
}
});
})
}, 300);
// Close the song menu.
el.click();
})
}
// On load actions.
window.addEventListener ("load", function()
{
setTimeout(() => {
moveSaveLibraryToPlayerBar()
}, 300);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment