Last active
January 15, 2021 02:35
-
-
Save eth-p/dc1745cd0b377e17c72d02c92d01ea3a to your computer and use it in GitHub Desktop.
A quick gist to prevent websites from automatically playing videos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Fuck Off Autoplay | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Videos with audio shouldn't be allowed to play without a user gesture. | |
// @author eth-p | |
// @match http://*/* | |
// @match https://*/* | |
// @grant none | |
// @downloadURL https://gitcdn.xyz/cdn/eth-p/dc1745cd0b377e17c72d02c92d01ea3a/raw/fuck-off-autoplay.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Environment. | |
const DBG = (...args) => console.log(`[FOA] ${args[0]}`, ...args.slice(1)); | |
const SYM = Symbol('Fuck Off Autoplay'); | |
let mouse = {x: null, y: null}; | |
function onInteract(event) { | |
mouse = {x: event.clientX, y: event.clientY}; | |
}; | |
function interacted(target) { | |
for (const rect of target.getClientRects()) { | |
if ( | |
(mouse.x > rect.left && mouse.x < rect.right) && | |
(mouse.y > rect.top && mouse.y < rect.bottom) | |
) { | |
return true; | |
} | |
} | |
return false; | |
} | |
window.addEventListener('mousedown', onInteract); | |
// Hook the play() method. | |
const playVideo = HTMLVideoElement.prototype.play; | |
HTMLVideoElement.prototype.play = function(...args) { | |
if (this[SYM] || interacted(this)) { | |
this[SYM] = true; | |
playVideo.apply(this, args); | |
} else { | |
DBG("Prevented autoplay.", this); | |
} | |
} | |
DBG("Loaded."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment