Turn this into a fully functional Tampermonkey script:…
- User
Turn this into a fully functional Tampermonkey script:
ytd-rich-item-renderer.style-scope:has(a[href*="&start_radio=1"]) { display: none !important; }
- Assistant
Turn this into a fully functional Tampermonkey script:
ytd-rich-item-renderer.style-scope:has(a[href*="&start_radio=1"]) { display: none !important; }
Certainly! Below is the fully functional Tampermonkey script based on the CSS code you provided. This script will hide all YouTube ytd-rich-item-renderer elements that contain an a tag with an href attribute that includes &start_radio=1. This effectively removes elements that start a radio station from the YouTube interface.
Please follow the instructions after the script to install and use it.
// ==UserScript==
// @name Hide YouTube Radio Elements
// @namespace https://tampermonkey.net/
// @version 1.0
// @description Hide YouTube elements that link to start radio stations.
// @author
// @match https://www.youtube.com/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// Function to hide radio elements
function hideRadioElements() {
// Select all ytd-rich-item-renderer elements
let items = document.querySelectorAll('ytd-rich-item-renderer');
items.forEach(function(item){
// Find any 'a' elements inside with href containing '&start_radio=1'
let a = item.querySelector('a[href*="&start_radio=1"]');
if(a){
// Hide the item
item.style.display = 'none';
}
});
}
// Initial execution to hide existing elements
hideRadioElements();
// MutationObserver callback to handle dynamically loaded content
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) { // Element node
// Check if the node is a ytd-rich-item-renderer
if (node.matches('ytd-rich-item-renderer')) {
let a = node.querySelector('a[href*="&start_radio=1"]');
if(a){
node.style.display = 'none';
}
} else if (node.querySelectorAll) {
// Check for any ytd-rich-item-renderer elements within the node
node.querySelectorAll('ytd-rich-item-renderer').forEach(function(item){
let a = item.querySelector('a[href*="&start_radio=1"]');
if(a){
item.style.display = 'none';
}
});
}
}
});
});
});
// Start observing the document body for adding new elements
observer.observe(document.body || document.documentElement, { childList: true, subtree: true });
})();
Install Tampermonkey: If you haven't already, install the Tampermonkey extension for your browser:
Create a New Script:
Paste the Script:
Save the Script:
Ctrl+S (Windows) or Cmd+S (Mac).Refresh YouTube:
Initial Execution: Upon loading the YouTube page, the script runs the hideRadioElements function, which searches for and hides any existing ytd-rich-item-renderer elements containing links with &start_radio=1 in their href.
Dynamic Content Handling: YouTube often loads content dynamically as you scroll or interact with the page. The script uses a MutationObserver to watch for changes in the DOM and applies the hiding logic to any new elements that are added.
Selectors Used:
ytd-rich-item-renderer: Represents individual items in the YouTube feed.a[href*="&start_radio=1"]: Selects anchor tags where the href attribute contains the string &start_radio=1.Customization: If you wish to adjust which elements are hidden, you can modify the selectors within the script accordingly.
Performance: The script is designed to have minimal impact on performance. The MutationObserver efficiently watches for changes without the need for continuous polling.
Compatibility: The script should work in all modern browsers that support Tampermonkey. Ensure your browser and Tampermonkey extension are up to date.
Script Not Working: If the radio elements are not being hidden:
Ctrl+Shift+J in Chrome).@match pattern in the script header (https://www.youtube.com/*).Interference with Other Extensions: Other browser extensions or userscripts may interfere with this script. Try disabling them temporarily to see if there's a conflict.
This script modifies the appearance of YouTube pages by hiding certain elements. Use it responsibly and be aware that changes to YouTube's site structure may cause the script to stop working in the future. In such cases, the script may need to be updated to accommodate any changes.
Enjoy a cleaner YouTube experience!
Not yet included in a published release.