YouTube - click to play

Provides click-to-play functionality for YouTube (disables auto-play).

  1. // ==UserScript==
  2. // @name YouTube - click to play
  3. // @namespace monnef.tk
  4. // @include https://www.youtube.com/*
  5. // @version 2
  6. // @grant none
  7. // @require http://code.jquery.com/jquery-2.1.3.min.js
  8. // @run-at document-start
  9. // @description Provides click-to-play functionality for YouTube (disables auto-play).
  10. // ==/UserScript==
  11.  
  12. /*
  13.  
  14. Few Notes
  15. -----------
  16. This script *may* work with other html5 players, you have to test it on your own.
  17. Mutation observer is not used, b/c GreaseMonkey thrashes all events for it...
  18.  
  19. */
  20.  
  21. var debug = false;
  22.  
  23. function log(msg){
  24. if(debug) console.log(msg);
  25. }
  26.  
  27. log("installing video hook");
  28.  
  29. HTMLMediaElement.prototype._oldPlay = HTMLMediaElement.prototype.play;
  30. HTMLMediaElement.prototype.play = function() {
  31. var unlocked = this._playUnlocked !== undefined;
  32. log("intercepted play request, " + unlocked);
  33. if (unlocked) this._oldPlay();
  34. };
  35.  
  36. this.$ = this.jQuery = jQuery.noConflict(true);
  37.  
  38. function insertUnlockers(sc) {
  39. log("inserting unlocker");
  40. var ve = $("video", sc);
  41. log("got " + ve.length + " video element(s).");
  42. ve.each(function() {
  43. var elem = this;
  44. $(this).attr("data-uv-processed", "true");
  45. var unlockerLink = $("<a href='#'>Unlock video and play!</a>").click(function() {
  46. log("unlocking and playing");
  47. elem._playUnlocked = true;
  48. elem.play();
  49. $(this).parent().hide();
  50. return false;
  51. }).css("font-size", "15pt");
  52. $(this).before(unlockerLink);
  53. unlockerLink.wrap("<div></div>");
  54. unlockerLink.parent().css("height", "50px").css("width","220px").css("background-color", "rgba(255, 255, 255, 0.8)").css("color", "black").css("position", "relative").css("z-index", "950").css("margin", "20px").css("padding", "10px").css("border", "1px solid #aaa").append("<p style='margin-top:10px'>Created by <a href='http://monnef.tk'>monnef</a>.</p>");
  55. });
  56. }
  57.  
  58. var inserting = false;
  59.  
  60. function installDomEventListener() {
  61. document.addEventListener("DOMNodeInserted", function(e) {
  62. if(inserting){
  63. log("Preventing recursion.");
  64. return;
  65. }
  66. inserting = true;
  67. var nodeRaw = e.target;
  68. var node = $(nodeRaw);
  69. if (nodeRaw.nodeName.toLowerCase() == "video" || $("video", node).length > 0) {
  70. log("found");
  71. var parentNode = $(node.parent());
  72. if (!node.attr("data-uv-processed") && !parentNode.attr("data-uv-processed")) {
  73. insertUnlockers(parentNode);
  74. }
  75. }
  76. inserting = false;
  77. }, false);
  78. }
  79.  
  80. installDomEventListener();

QingJ © 2025

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