有没有办法用javascript模拟在鼠标点击时按下多个键? [英] Is there a way to simulate pressing multiple keys on mouse click with javascript?

查看:23
本文介绍了有没有办法用javascript模拟在鼠标点击时按下多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 chrome 扩展程序,以使 Netflix 视频播放器打开隐藏的更改质量面板.

I'm working on chrome extension to make Netflix video player opens the hidden changing quality panel.

Netflix 根据您的互联网速度自动更改视频质量.

Netflix Changes the video quality automatically depending on the speed of your Internet.

但是有一种已知的方法可以打开隐藏面板并手动更改质量.

But there is a known way to open the hidden panel and change the quality manually.

这是在 HTML5 Player 中打开隐藏面板的方法.CTRL + SHIFT + ALT + S

This is the method to open the hidden panel in HTML5 Player Only CTRL + SHIFT + ALT + S

用户点击标题时有没有办法,
它模拟键盘按键 CTRL + SHIFT + ALT + S ?

Is there a way when the user clicks on Title,
it simulates the keyboard keys CTRL + SHIFT + ALT + S ?

$('body').on('click', 'div.player-status .player-status-main-title', function () {
    alert('Clicked!');
    //Simulate?
});

推荐答案

试试这个(无法测试,因为我无法访问 Netflix 已测试,确认在 11.11.14 开始工作).

Try this (can't test, since I can't access Netflix tested, confirmed working as of 11.11.14).

function simulateCtrlShiftAltS() {
  // Prepare function for injection into page
  function injected() {
    // Adjust as needed; some events are only processed at certain elements
    var element = document.body;

    function keyEvent(el, ev) {
      var eventObj = document.createEvent("Events");
      eventObj.initEvent(ev, true, true);

      // Edit this to fit
      eventObj.keyCode = 83;
      eventObj.which = 83;
      eventObj.ctrlKey = true;
      eventObj.shiftKey = true;
      eventObj.altKey = true;

      el.dispatchEvent(eventObj);
    }

    // Trigger all 3 just in case
    keyEvent(element, "keydown");
    keyEvent(element, "keypress");
    keyEvent(element, "keyup");
  }

  // Inject the script
  var script = document.createElement('script');
  script.textContent = "(" + injected.toString() + ")();";
  (document.head||document.documentElement).appendChild(script);
  script.parentNode.removeChild(script);
}

此代码改编自对您链接的答案的评论:https://stackoverflow.com/a/10520017/2518069

This code is adapted from comments to the answer you linked: https://stackoverflow.com/a/10520017/2518069

准确地说,从这个例子:http://jsbin.com/awenaq/4

To be precise, from this example: http://jsbin.com/awenaq/4

关于按需调整":

有些页面处理元素上的事件,有些页面会等到它冒泡到诸如 bodydocument 之类的东西.您可以通过转到 Dev Tools、Sources 并启用 Event Listener Breakpoints > Keyboard 来监视它.从那里,您将能够看到哪个事件触发了您需要的更改,以及哪个元素捕获了它 - 当断点触发时,它将在 this 中.

Some pages process events on an element and some wait until it bubbles to something like body or document. You can spy on this by going to Dev Tools, Sources, and enabling Event Listener Breakpoints > Keyboard. From there, you'll be able to see which event triggers the change you need, and which element catches it - it will be in this when the breakpoint triggers.

另外,请注意,如果播放器实际上是插件,则所有这些都可能不起作用.我在 YouTube HTML5 播放器上对此进行了测试:它适用于除全屏以外的所有内容(我认为这是一个安全限制?),并在元素 #movie_player 处进行处理.

Also, note that all of this may not work if the player is actually a plugin. I tested this on YouTube HTML5 player: it works for everything but fullscreen (which I believe to be a security restriction?), and is processed at element #movie_player.

这篇关于有没有办法用javascript模拟在鼠标点击时按下多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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