Skip to content

Instantly share code, notes, and snippets.

@jscher2000
Created November 5, 2017 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jscher2000/586009d1d5078f4e72abe2be466d9144 to your computer and use it in GitHub Desktop.
Save jscher2000/586009d1d5078f4e72abe2be466d9144 to your computer and use it in GitHub Desktop.
Hide the play command for video elements
// ==UserScript==
// @name Hide Media Play Command
// @namespace Violentmonkey Scripts
// @match *://*/*
// @grant none
// ==/UserScript==
/* Supplements but does not replace media.autoplay.enabled = false
* PROBLEM: in testing on YouTube, userStart() reports "pending" and the video doesn't play, when it's the initial page load.
* Videos you load from thumbnails work as expected. */
// Kill play() and create userStart()
function noplay(){
HTMLMediaElement.prototype.userStart = HTMLMediaElement.prototype.play;
HTMLMediaElement.prototype.play = function(){if(this.nodeName == "VIDEO") console.log("play canceled!"); else this.userStart();};
}
var s = document.createElement('script');
s.appendChild(document.createTextNode('('+ noplay +')();'));
document.body.appendChild(s);
// Add button to find and start videos
function startVid(e){
var vids = document.querySelectorAll('video');
console.log('vids.length='+vids.length);
for (var i=0; i<vids.length; i++){
vids[i].scrollIntoView();
if(confirm("Play this one?")){
vids[i].userStart();
break;
}
}
}
var btn = document.createElement('button');
btn.setAttribute('style','width:25px;height:25px;color:#ccc;background-color:#333;position:fixed;bottom:0;right:0;border:none;z-index:5000;');
btn.innerHTML = '&#9654;';
document.body.appendChild(btn);
btn.addEventListener('click', startVid, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment