Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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