Last active
June 27, 2024 22:39
-
-
Save nemokaul/3c02aa3610c579a747bc36d125a14c00 to your computer and use it in GitHub Desktop.
Automatically pause the video when the mouse leaves the browser window or switches tabs, and automatically play the video when the mouse reactivates the browser window or switches back to the tab. `Ctrl+Alt+;` - AutoVideoPause Enable/Disable.
This file contains hidden or 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 Automatically pause/resume video playback | |
// @namespace nemokaul | |
// @version 1.3 | |
// @description Automatically pause the video when the mouse leaves the browser window or switches tabs, and automatically play the video when the mouse reactivates the browser window or switches back to the tab | |
// @author Nemo Kaul | |
// @match *://*/* | |
// @grant none | |
// @downloadURL https://gist.github.com/nemokaul/3c02aa3610c579a747bc36d125a14c00/raw/70ff8a5f7c710c2195027e34c24106d99c48a904/autopause.user.js | |
// @updateURL https://gist.github.com/nemokaul/3c02aa3610c579a747bc36d125a14c00/raw/70ff8a5f7c710c2195027e34c24106d99c48a904/autopause.user.js | |
// ==/UserScript== | |
(function() { | |
var videoElement = null; | |
var isPaused = false; | |
var isScriptActive = false; | |
// Get all video elements | |
function getVideoElements() { | |
return document.querySelectorAll('video'); | |
} | |
// Pause video playback | |
function pauseVideo() { | |
if (!videoElement.paused) { | |
videoElement.pause(); | |
isPaused = true; | |
} | |
} | |
// Resume video playback | |
function playVideo() { | |
if (isPaused) { | |
videoElement.play(); | |
isPaused = false; | |
} | |
} | |
// Listen for mouse out of window events | |
function handleMouseOut(event) { | |
if (!isScriptActive) return; | |
// Determine whether the mouse has left the entire browser window | |
if (event.toElement === null && event.relatedTarget === null) { | |
var videos = getVideoElements(); | |
if (videos.length > 0) { | |
videoElement = videos[0]; | |
pauseVideo(); | |
} | |
} | |
} | |
// Listen for mouse entering window events | |
function handleMouseOver(event) { | |
if (!isScriptActive) return; | |
// Determine whether the mouse enters the entire browser window | |
if (event.fromElement === null && event.relatedTarget === null) { | |
playVideo(); | |
} | |
} | |
// Toggle script activation | |
function toggleScript() { | |
isScriptActive = !isScriptActive; | |
if (isScriptActive) { | |
showMessage("Video AutoPause is ON", "blue"); | |
window.addEventListener('mouseout', handleMouseOut); | |
window.addEventListener('mouseover', handleMouseOver); | |
} else { | |
showMessage("Video AutoPause is OFF", "blue"); | |
window.removeEventListener('mouseout', handleMouseOut); | |
window.removeEventListener('mouseover', handleMouseOver); | |
} | |
} | |
// Show a message in the top right corner of the page | |
function showMessage(message, color) { | |
var messageElement = document.createElement('div'); | |
messageElement.style.position = 'fixed'; | |
messageElement.style.bottom = '10px'; | |
messageElement.style.left = '20px'; | |
messageElement.style.color = color; | |
messageElement.style.fontSize = '15px'; | |
messageElement.style.fontWeight = 'bold'; | |
messageElement.style.backgroundColor = 'rgba(255, 255, 255, 0.7)'; | |
messageElement.style.padding = '20px'; | |
messageElement.style.border = '1px solid ' + color; | |
messageElement.style.zIndex = '10000'; | |
messageElement.textContent = message; | |
document.body.appendChild(messageElement); | |
setTimeout(() => { | |
document.body.removeChild(messageElement); | |
}, 2000); | |
} | |
// Listen for the specific key combination to toggle the script | |
// Alt+; -- Toggle script on or off | |
document.addEventListener('keydown', function(event) { | |
if (event.altKey && event.key === ';') { | |
toggleScript(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment