3

When we right click on a YouTube video, it displays this context menu. I need a JavaScript code to disable this context menu when it's right clicked.

enter image description here

I've found similar question on this topic but that's also unanswered. YouTube - don't display context menu on right click

1 Answers1

-1

update: To detect youtube page change and inject the script

document.body.addEventListener("yt-navigate-finish", function (event) {
  console.log('Youtube page Change')
  $('div video').addEventListener('contextmenu', e => e.preventDefault());
  $('body').insertAdjacentHTML('afterbegin', '<style>.ytp-contextmenu{visibility:hidden;}</style>');
});

you can try this

// disable HTML5 Video contextmenu
$('div video').addEventListener('contextmenu', e=>e.preventDefault())
// hide element contextmenu by appending custom <style> (CSS)
// cannot use inline CSS
// because the element created after user right click
$('body').insertAdjacentHTML('afterbegin', '<style>.ytp-contextmenu{visibility:hidden;}</style>');

$() is not jQuery function, change it with document.querySelector() if you have trouble

uingtea
  • 6,002
  • 2
  • 26
  • 40
  • try paste the code in browser console, it will not working if you navigate from other page, you need to watch for URL or DOMElement change. – uingtea Feb 24 '21 at 08:11
  • @unigtea I want to execute the code every time a page loads. So I'm using tampermonkey to run this script. This script works in browser console but doesn't works in tampermonkey. How to make this work? – Mahdi Hasan Feb 26 '21 at 14:19