Custom Image for Youtube Background

Replace the old boring flat background with whatever picture you like

当前为 2024-07-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Custom Image for Youtube Background
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Replace the old boring flat background with whatever picture you like
  6. // @author Hoover
  7. // @match *://*.youtube.com/*
  8. // @grant none
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14. // Only tested on dark mode. If you're using light mode, you're a lunatic.
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. // Fill the array up with URLs to pictures
  20. const bgSources = [
  21. "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg",
  22. "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg"
  23. ]
  24.  
  25. function getRandomImageUrl(images) {
  26. const randomIndex = Math.floor(Math.random() * images.length);
  27. return images[randomIndex];
  28. }
  29.  
  30. // Select only one image from the array
  31. let bgImage = getRandomImageUrl(bgSources);
  32.  
  33. let css = `
  34. background-image: url(${bgImage});
  35. background-attachment: fixed;
  36. background-size: cover;
  37. background-repeat: no-repeat;
  38. background-position: center;
  39. `;
  40.  
  41. // Find the first #content div and apply the style
  42. const firstContentDiv = document.querySelector("#content");
  43. if (firstContentDiv) {
  44. firstContentDiv.style.cssText += css;
  45. }
  46.  
  47. let css2 = `
  48. #guide,#chips-wrapper{
  49. opacity: 0.9;
  50. }
  51. #cinematics-container{
  52. display: none;
  53. }
  54. `;
  55. if (typeof GM_addStyle !== "undefined") {
  56. GM_addStyle(css2);
  57. } else {
  58. let styleNode = document.createElement("style");
  59. styleNode.appendChild(document.createTextNode(css2));
  60. (document.querySelector("head") || document.documentElement).appendChild(styleNode);
  61. }
  62.  
  63. // Live chat transparency
  64. // Have to overrride the whole style block for this to work
  65. const overridenDarkStyle = `
  66. html[dark],
  67. [dark] {
  68. --yt-spec-base-background:#0f0f0fbf;
  69. }
  70. `
  71.  
  72. // Create a new <style> element
  73. const styleElement = document.createElement('style');
  74. styleElement.type = 'text/css';
  75. styleElement.appendChild(document.createTextNode(overridenDarkStyle));
  76.  
  77. // Append the <style> element to the document's <head>
  78. const head = document.head || document.getElementsByTagName('head')[0];
  79. if (head) {
  80. head.appendChild(styleElement);
  81. }
  82. })();