YouTube-右键单击时不显示上下文菜单 [英] YouTube - don't display context menu on right click

查看:92
本文介绍了YouTube-右键单击时不显示上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想法:通过使用鼠标右键(例如,每2%的屏幕宽度为1秒)拖放视频播放器来查找YouTube视频.因此,在1920x1080屏幕上,如果我按鼠标右键,将其向左拖动384px(20%),然后释放它,则视频应快退10秒.

The idea: seek YouTube video by dragging and dropping on the video player with right button (e.g. 1 second for every 2% of screen width). So on a 1920x1080 screen, if I press the right mouse button, drag it some 384px (20%) to the left and then release it, the video should rewind by 10 seconds.

我有一个GreaseMonkey脚本,该脚本几乎可以完成我想要的操作,但是当我松开按钮时,上下文菜单仍然会弹出.这不是默认的上下文菜单,而是YouTube的自定义上下文菜单,该菜单可能以某种方式绑定到mouseup事件.我想摆脱此菜单,并且我也想阻止默认上下文菜单打开.

I have a GreaseMonkey script which does pretty much what I want, but the context menu still pops up when I release the button. This is not the default context menu, but YouTube's custom context menu, which is presumably somewhere somehow bound to the mouseup event. I want to get rid of this menu, and I also want to prevent the default context menu from opening.

有没有一种方法可以更改鼠标事件的默认操作?我想保留所有其他操作(单击鼠标左键,键盘操作等).我还没有找到一种方法来删除特定事件元素上的事件处理程序.

Is there a way I can change default actions on mouse events? I want to keep all other actions (left click, keyboard actions, etc.). I have not found a way to remove event handlers on an element for a specific event.

if (window.top === window.self) {
  // YT video cannot be manipulated from the scope in which GM is running
  // so we add a <script> element in the document to make it work
  addJsNode(installListeners)
}

function installListeners() {

  const FACTOR = screen.width / 70

  const mp = document.getElementById('movie_player')

  let startpos

  mp.onmousedown = (e) => {
    // only using FF so cross-browser compatibility is not required
    if (e.button == 2) { startpos = e.clientX }
  }

  mp.onmouseup = (e) => {
    if (e.button == 2) {
        //===> somehow prevent YT from displaying context menu
        const diff = e.clientX - startpos
        mp.seekBy(diff / FACTOR)
    }
  }

}

function addJsNode(func) {
  var scriptNode = document.createElement('script')
  scriptNode.type = 'text/javascript'
    scriptNode.textContent = '('+func.toString()+')()'

  var target = document.getElementsByTagName ('head')[0] ||
                        document.body || document.documentElement

  target.appendChild(scriptNode)
}

推荐答案

您可以使用remove()方法删除该元素.您将需要元素的类名,可以在检查工具的帮助下找到该元素或查看页面源代码

you could remove the element with remove() method. you would need class name for the element which you can find with help of inspect facility or view page source

这篇关于YouTube-右键单击时不显示上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆