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

标签 javascript youtube greasemonkey youtube-javascript-api greasemonkey-4

想法:通过使用右键在视频播放器上拖放来搜索 YouTube 视频(例如,每 2% 的屏幕宽度拖放 1 秒)。因此,在 1920x1080 屏幕上,如果我按下鼠标右键,将其向左拖动 384 像素 (20%),然后松开,视频应该倒回 10 秒。

我有一个 GreaseMonkey 脚本,它几乎可以做我想做的事,但是当我释放按钮时上下文菜单仍然弹出。这不是默认的上下文菜单,而是 YouTube 的自定义上下文菜单,它可能在某处以某种方式绑定(bind)到 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)
}

最佳答案

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

关于javascript - YouTube - 右键单击​​不显示上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664330/

相关文章:

javascript - num2persian 无法压缩此文件中的代码 : ./node_modules/num2persian

javascript - 在 jquery ajax 中可以设置的最大超时是多少?

javascript - ajax 请求在 knockout js 中发送第二次点击

c# - Youtube Data API C# - 无需请求用户凭据即可使用

javascript - chrome扩展程序应在youtube标签不清晰时暂停视频

javascript - 有没有办法在浏览器之外将用户脚本部署到 Grease/Tampermonkey?

javascript - 为什么 waitForKeyElements 对于 Google 搜索结果需要另一个超时?

javascript - Greasemonkey 1.0 中的 jQuery 与使用 jQuery 的网站发生冲突

javascript - 延迟对象链可以用于事件处理程序吗?

javascript - YouTube javascript API(带有 ASP)功能无法正常工作