想法:通过使用右键在视频播放器上拖放来搜索 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/