YouTube-右键单击不显示上下文菜单

问题描述 投票:1回答:1

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

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

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

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)
}
javascript youtube greasemonkey youtube-javascript-api greasemonkey-4
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.