YouTube - Button Container (@require)

Creates a button container, under the video, onto which buttons can be added

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/2104/5493/YouTube%20-%20Button%20Container%20%28%40require%29.js

  1. // ==UserScript==
  2. // @name YouTube - Button Container (@require)
  3. // @namespace http://userscripts.org/users/23652
  4. // @description Creates a button container, under the video, onto which buttons can be added
  5. // @include http://*.youtube.com/*
  6. // @include http://youtube.com/*
  7. // @include https://*.youtube.com/*
  8. // @include https://youtube.com/*
  9. // @copyright JoeSimmons
  10. // @version 1.0.2
  11. // @license LGPL version 3 or any later version; http://www.gnu.org/copyleft/lgpl.html
  12. // @grant GM_addStyle
  13. // ==/UserScript==
  14.  
  15. /* CHANGELOG
  16.  
  17. 1.0.2 (1/15/2014)
  18. - changed license to LGPL
  19. - updated style
  20. - added a test mode, so I can work on visual changes more easily
  21. - modified the main function to return the created button
  22.  
  23. 1.0.1 (12/13/2013)
  24. - slight tweak for more flexible styles
  25. buttons now have a special class which you can modify.
  26. you can even put buttons outside of the container with
  27. that class, and they will have that style applied
  28.  
  29. 1.0.0 (12/13/2013)
  30. - created
  31.  
  32. */
  33.  
  34. var addButtonToContainer = (function () {
  35. 'use strict';
  36.  
  37. var rYoutubeWhitelistedUrls = /^https?:\/\/([^\.]+\.)?youtube\.com\/(watch|user\/|channel\/)/;
  38.  
  39. var TEST_MODE = true; // if true, will replace the current style with this one
  40.  
  41. // helper functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  42. function addStyle(contents) {
  43. var head = document.head || query('html > head'),
  44. style = document.createElement('style');
  45.  
  46. style.id = 'underMovieDivStyle';
  47. style.type = 'text/css';
  48. style.appendChild( document.createTextNode(contents) );
  49.  
  50. if (head) {
  51. head.appendChild(style);
  52. }
  53. }
  54. function id(name) {
  55. return document.getElementById(name);
  56. }
  57. function query(name) {
  58. return document.querySelector(name);
  59. }
  60. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  61.  
  62. function waitForHeadReady() {
  63. if ( document.head || query('html > head') ) {
  64. window.setTimeout(handleStyleCreation, TEST_MODE === true ? 3000 : 0);
  65. } else {
  66. window.setTimeout(waitForHeadReady, 200);
  67. }
  68. }
  69.  
  70. function handleStyleCreation() {
  71. var umdv = id('underMovieDivStyle'),
  72. obs = new MutationObserver(function (mutations) {
  73. mutations.forEach(function (mutation) {
  74. if (mutation.target.id === 'underMovieDivStyle') {
  75. handleStyleCreation();
  76. }
  77. });
  78. });
  79.  
  80. if (TEST_MODE === true || !umdv) {
  81. if (umdv) {
  82. umdv.parentNode.removeChild(umdv);
  83. }
  84.  
  85. addStyle('' +
  86. '#under-movie-div { ' +
  87. 'background: transparent !important; ' +
  88. 'display: block; ' +
  89. 'padding-top: 4px; ' +
  90. 'padding-bottom: 8px; ' +
  91. 'text-align: left; ' +
  92. 'z-index: 999999; ' +
  93. '}\n\n' +
  94. '.under-movie-div-button { ' +
  95. 'background-image: linear-gradient(to top, #F6F6F6 0px, #FCFCFC 100%) !important; ' +
  96. 'border: 1px solid #CCCCCC; ' +
  97. 'border-radius: 2px; ' +
  98. 'color: #666666 !important; ' +
  99. 'font-size: 12px !important; ' +
  100. 'font-family: arial, sans-serif !important; ' +
  101. 'font-weight: 400 !important; ' +
  102. 'height: auto !important; ' +
  103. 'line-height: 20px !important; ' +
  104. 'margin-right: 6px; ' +
  105. 'padding: 2px 10px !important; ' +
  106. '}\n\n' +
  107. // - - - - - - - - - - - - - - - - - - -
  108. '#watch7-headline { ' +
  109. 'padding: 4px 0 !important; ' +
  110. '}\n\n' +
  111. '#watch7-headline h1 { ' +
  112. 'text-align: center; ' +
  113. 'width: 100%; ' +
  114. '}\n\n' +
  115. '#upsell-video { ' +
  116. 'overflow: visible !important; ' + // allow the button container to show on non-video pages
  117. '}' +
  118. '');
  119.  
  120. // observe the style for changes if test mode enabled
  121. if (TEST_MODE === true) {
  122. obs.observe(id('underMovieDivStyle'), {
  123. childList : true,
  124. attributes : true,
  125. characterData : true
  126. });
  127. }
  128. }
  129. }
  130.  
  131. function handleContainerCreation() {
  132. var holder = query('#watch7-headline, #gh-overviewtab div.c4-spotlight-module-component'),
  133. container = id('under-movie-div');
  134.  
  135. if (container == null) {
  136. container = document.createElement('div');
  137. container.id = 'under-movie-div';
  138.  
  139. if (holder) {
  140. holder.appendChild(container);
  141. }
  142. }
  143.  
  144. return container;
  145. }
  146.  
  147. function add(text, onclick, ID) {
  148. var upsellVideo = id('upsell-video'),
  149. container = handleContainerCreation(),
  150. button = document.createElement('button'),
  151. span = document.createElement('span');
  152. ID = ID || text.replace(/[^a-zA-Z0-9-_]/, '');
  153.  
  154. if ( !location.href.match(rYoutubeWhitelistedUrls) || id(ID) ) { return; }
  155. if (typeof text !== 'string' || typeof onclick !== 'function') { return; }
  156.  
  157. span.setAttribute('class', 'yt-uix-button-content');
  158. span.appendChild( document.createTextNode(text) );
  159.  
  160. button.id = ID;
  161. button.addEventListener('click', function () {
  162. window.setTimeout(onclick, 0);
  163. }, false);
  164.  
  165. button.type = 'button';
  166. button.setAttribute('class', 'under-movie-div-button yt-uix-button yt-uix-button-text yt-uix-tooltip');
  167. button.appendChild(span);
  168.  
  169. return container.appendChild(button);
  170. }
  171.  
  172. waitForHeadReady();
  173.  
  174. return add;
  175. }());
  176.  
  177. /* EXAMPLE BUTTON
  178. addButtonToContainer('A Test Button', function () { alert('Test.'); }, 'test-button');
  179. */
  180.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址