简体   繁体   English

使用 TamperMonkey 脚本暂停本机 Chrome HTML5 播放器上的视频

[英]Pause video on native Chrome HTML5 Player with TamperMonkey Script

I'm trying to set a TamperMonkey script to make a keyboard key pause the native Chrome video player without the need to click on it or use the Space Bar我正在尝试设置一个TamperMonkey脚本来使键盘键暂停原生 Chrome 视频播放器,而无需单击它或使用Space Bar

To achieve this, I tried to set a script that would trigger the click action in an x, y coordinate where the video is displayed on the page.为了实现这一点,我尝试设置一个脚本,该脚本会在视频在页面上显示的x, y coordinate中触发点击操作。

So far, it didn't work.到目前为止,它没有奏效。

// PAUSE VIDEO - ASSIGNED TO *Q* KEY
    (function(){
    document.addEventListener('keydown', function(e) {
    if (e.keyCode == 81 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
    document.elementFromPoint(448, 540).click(); // this should trigger the click on video to pause event
    }
    }, false);
    })();

Can anyone enlighten me on how to make this video pause with the key Q ?谁能启发我如何使用Q键暂停此视频?

Native HTMLVideoElement 's don't play/pause when clicked on, so you'll need to use the .play() and .pause() methods, determining whether to play or pause based off of that .paused readonly boolean.单击时原生HTMLVideoElement不会播放/暂停,因此您需要使用.play().pause()方法,根据该.paused readonly 布尔值确定是播放还是暂停。 It would be great if there was just a .togglePlayback() method, but there isn't.如果只有一个.togglePlayback()方法会很棒,但没有。

(BTW, using KeyboardEvent.keyCode is depricated in favor of KeyboardEvent.key . It still works in major web browsers for backwards compatibility, but it's been officially deprecated in the standards) (顺便说一句, 使用KeyboardEvent.keyCode是depricated赞成KeyboardEvent.key 。它仍然工作在主流浏览器的向后兼容性,但它在标准中被正式弃用)

On StackOverflow, make sure that you click anywhere inside the code snippet (the white box) once you click the blue button.在 StackOverflow 上,单击蓝色按钮后,请确保单击代码片段(白框)内的任意位置。

 (function () { document.addEventListener('keydown', function (e) { if (e.key == 'q' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) { const video = document.querySelector('video'); // or = document.elementFromPoint(448, 540); if that's where it will always be if (video.paused) video.play(); else video.pause(); } }, ); })();
 <!-- Simple video example --> <!-- 'Big Buck Bunny' licensed under CC 3.0 by the Blender foundation. Hosted by archive.org --> <!-- Poster from peach.blender.org --> <video controls src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217" width="300"> </video>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM