YouTube Channel Search Filter

Hide search results from other YouTube channels when searching within a specific channel.

  1. // ==UserScript==
  2. // @name YouTube Channel Search Filter
  3. // @namespace https://greasyfork.org/en/users/1191564-iamcup
  4. // @version 1.0
  5. // @description Hide search results from other YouTube channels when searching within a specific channel.
  6. // @author Tom McGraw
  7. // @match https://www.youtube.com/*/search?query=*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Get the channel name from the URL
  16. const url = window.location.href;
  17. const match = url.match(/@([^/]+)\/search/);
  18. if (!match) return;
  19.  
  20. const searchText = match[1];
  21.  
  22. // Function to hide elements without the specified text
  23. function hideElementsWithoutText() {
  24. const elements = document.querySelectorAll('div#contents ytd-item-section-renderer');
  25.  
  26. elements.forEach(element => {
  27. const text = element.textContent;
  28. if (!text.includes(searchText)) {
  29. element.style.display = 'none';
  30. }
  31. });
  32. }
  33.  
  34. // Call the function when the page loads and when new content is loaded (e.g., when scrolling)
  35. window.addEventListener('load', hideElementsWithoutText);
  36. window.addEventListener('scroll', hideElementsWithoutText);
  37. })();