Skip to content

Instantly share code, notes, and snippets.

@benlacey57
Created April 22, 2025 11:13
Show Gist options
  • Select an option

  • Save benlacey57/b4645e44b9d1d61253b33e6295b956b8 to your computer and use it in GitHub Desktop.

Select an option

Save benlacey57/b4645e44b9d1d61253b33e6295b956b8 to your computer and use it in GitHub Desktop.
JavaScript video download and screenshot prevention
document.addEventListener('DOMContentLoaded', function() {
const video = document.getElementById('protected-video');
// Prevent right-click menu (basic download blocking)
video.addEventListener('contextmenu', function(e) {
e.preventDefault();
alert("Video downloading is not allowed");
});
// Disable controls that might allow download
video.controls = false;
// Create custom video controls
const customControls = document.createElement('div');
customControls.className = 'custom-video-controls';
customControls.innerHTML = `
<button id="play-pause">Play</button>
<input type="range" id="progress" min="0" max="100" value="0">
<button id="mute">Mute</button>
<input type="range" id="volume" min="0" max="1" step="0.1" value="1">`;
video.parentNode.insertBefore(customControls, video.nextSibling);
// Screenshot prevention - blank video during captures
document.addEventListener('keydown', function(e) {
// Common screenshot key combinations
if ((e.key === 'PrintScreen') ||
(e.ctrlKey && e.key === 'p') ||
(e.metaKey && e.key === 'p')) {
e.preventDefault();
// Temporarily hide video content
const originalDisplay = video.style.display;
video.style.display = 'none';
// Restore after a short delay
setTimeout(function() {
video.style.display = originalDisplay;
}, 200);
alert("Screenshots are not allowed");
}
});
// Basic screen capture detection (not foolproof)
const detectScreenCapture = function() {
if (document.pictureInPictureElement) {
document.exitPictureInPicture();
alert("Picture-in-Picture mode is disabled");
}
};
video.addEventListener('enterpictureinpicture', detectScreenCapture);
// Apply CSS to prevent selection
video.style.userSelect = 'none';
video.style.webkitUserSelect = 'none';
// Implement custom control handlers
// Play/pause button
const playPauseBtn = document.getElementById('play-pause');
playPauseBtn.addEventListener('click', function() {
if (video.paused) {
video.play();
playPauseBtn.textContent = 'Pause';
} else {
video.pause();
playPauseBtn.textContent = 'Play';
}
});
// And other video control handlers...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment