html5 videos: Click To Play

Make "video" elements click to play

  1. // ==UserScript==
  2. // @name html5 videos: Click To Play
  3. // @namespace ChoGGi.org
  4. // @description Make "video" elements click to play
  5. // @include http://*
  6. // @include https://*
  7. // @version 0.3
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. //skip these domains (allow autoplay)
  15. function checkDomain() {
  16. let d = (window.location != window.parent.location) ? document.referrer : document.location.href;
  17. if (d.includes("example.com") === false &&
  18. //d.includes("youtube.com") === false &&
  19. //d.includes("gfycat.com") === false &&
  20. //d.includes("escapistmagazine.com") === false &&
  21. d.includes("example.net") === false)
  22. return true;
  23. }
  24.  
  25. //listen for newly added videos
  26. let MutationObserver = window.MutationObserver;
  27. let vidObserver = new MutationObserver(mutationHandler);
  28. let observerConfig = {
  29. childList:true,
  30. subtree:true
  31. };
  32.  
  33. //activated on new elements
  34. function mutationHandler(mutationRecords) {
  35. mutationRecords.forEach(function(mutation) {
  36. if (mutation.type == "childList" &&
  37. typeof mutation.addedNodes == "object" &&
  38. mutation.addedNodes.length) {
  39. for (let i = 0, length = mutation.addedNodes.length; i < length; ++i) {
  40. let node = mutation.addedNodes[i];
  41. //check if it's a video node
  42. if (node.nodeType === 1 && node.nodeName === "VIDEO") {
  43. //if it's a video we added; remove the function below
  44. if (node.ChoGGi_autoplayVideo === 1) {
  45. node.onplaying = null;
  46. } else {
  47. //have to do this for some annoying sites (twitch/youtube) else we get audio playing with no video
  48. node.onplaying = function() {
  49. convertVideo(node);
  50. };
  51. }
  52. }
  53. }
  54. }
  55. });
  56. }
  57.  
  58. function iterateVideos() {
  59. let vidList = document.getElementsByTagName("video");
  60. for (let i = 0; i < vidList.length; i++) {
  61. convertVideo(vidList[i]);
  62. }
  63. }
  64.  
  65. //switch the video out with a button
  66. function convertVideo(video) {
  67. //get position/size of video element
  68. let style = window.getComputedStyle(video, null);
  69. //build a button to replace the video
  70. let clicker = document.createElement("button");
  71. clicker.style = "z-index: 99999; font-size: 15px; cursor: pointer; position: " + style.position + "; top: " + style.top + "; left: " + style.left +"; width: " + style.width + "; height: " + style.height + ";";
  72. clicker.innerHTML = "Play Video";
  73. clicker.className = "ChoGGi_autoplayToggle";
  74. //store the video as part of the button
  75. clicker.ChoGGi_videoObject = video;
  76. //fire this function when we click the button
  77. clicker.onclick = function() {
  78. let video = this.ChoGGi_videoObject;
  79. //we don't want the vidObserver to catch this video
  80. video.ChoGGi_autoplayVideo = 1;
  81. //add default controls to video
  82. //video.setAttribute("controls","");
  83. //add video back to webpage
  84. this.parentNode.appendChild(video);
  85. //remove any extra buttons added (thanks youtube)
  86. let childList = video.parentNode.children;
  87. for (let i = 0; i < childList.length; i++) {
  88. if (childList[i].className === "ChoGGi_autoplayToggle")
  89. this.parentNode.removeChild(childList[i]);
  90. }
  91. //video re-added so remove the button (it was probably removed by the above)
  92. try {
  93. this.parentNode.removeChild(this);
  94. }catch(e){}
  95. //if we're clicking the button we want to watch the video
  96. video.play();
  97. };
  98. //add button to page
  99. video.parentNode.appendChild(clicker);
  100. //remove video from page
  101. video.parentNode.removeChild(video);
  102. }
  103.  
  104. //are we skipping this page?
  105. if (checkDomain()) {
  106. //wait before checking for video elements
  107. window.setTimeout(function() {
  108. iterateVideos();
  109. }, 250);
  110. //add listener to check for js added vids
  111. vidObserver.observe(document,observerConfig);
  112. }
  113.  
  114. })();

QingJ © 2025

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