Custom Image for Youtube Background

Replace the old boring flat background with whatever picture you like

当前为 2024-06-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Custom Image for Youtube Background
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Replace the old boring flat background with whatever picture you like
  6. // @author Hoover
  7. // @match *://*.youtube.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. // Fill the array up with URLs to pictures
  15. const bgSources = [
  16. "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg",
  17. "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg"
  18. ]
  19.  
  20. function getRandomImageUrl(images) {
  21. const randomIndex = Math.floor(Math.random() * images.length);
  22. return images[randomIndex];
  23. }
  24.  
  25. // Select only one image from the array
  26. let bgImage = getRandomImageUrl(bgSources);
  27.  
  28. function applyBackground() {
  29. var primaryElement = document.getElementById('content');
  30. if (primaryElement) {
  31. primaryElement.style.backgroundImage = `url(${bgImage})`;
  32. primaryElement.style.backgroundAttachment = 'fixed';
  33. primaryElement.style.backgroundSize = 'cover';
  34. primaryElement.style.backgroundRepeat = 'no-repeat';
  35. primaryElement.style.backgroundPosition = 'center';
  36. }
  37. }
  38. // Function to add transparency to some divs
  39. function applyTransparency(divName) {
  40. var divElem = document.getElementById(divName);
  41. if (divElem) {
  42. divElem.style.opacity = 0.9;
  43. }
  44. }
  45.  
  46. // Repackaged for quick calling
  47. function applyTransparentElems() {
  48. applyTransparency('guide');
  49. applyTransparency('chips-wrapper');
  50. }
  51.  
  52. // Function to remove elements by class name
  53. function removeElementsByID(id) {
  54. var element = document.getElementById(id);
  55. if (element) {
  56. element.parentNode.removeChild(element);
  57. }
  58. }
  59.  
  60.  
  61. // Must remove cinematic container (the glowing effect when video plays)
  62. removeElementsByID('cinematics-container');
  63.  
  64. applyTransparentElems();
  65.  
  66. // Check for the element every 500ms until it is found
  67. var checkExist = setInterval(function () {
  68. if (document.getElementById('content')) {
  69. applyBackground();
  70. clearInterval(checkExist);
  71. removeElementsByID('cinematics-container');
  72. }
  73. }, 500);
  74.  
  75. // Additionally, listen for page changes and reapply the background
  76. var observer = new MutationObserver(function (mutations) {
  77. mutations.forEach(function (mutation) {
  78. if (mutation.addedNodes.length) {
  79. applyBackground();
  80. applyTransparentElems()
  81. removeElementsByID('cinematics-container');
  82. }
  83. });
  84. });
  85.  
  86. observer.observe(document.body, {
  87. childList: true,
  88. subtree: true
  89. });
  90. })();