YouTube - Button Container (Align Tweak) (@require)

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

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/31401/205792/YouTube%20-%20Button%20Container%20%28Align%20Tweak%29%20%28%40require%29.js

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