Last active
May 13, 2024 09:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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.2" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name YouTubeMusicScript | |
// @description YouTubeMusicScript | |
// @include http://music.youtube.com/* | |
// @autho Nicolas Mollet | |
// @version 1.2 | |
// ==/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') { | |
if( mutation.target.className.includes('ytp-title-expanded-subtitle')){ | |
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