jQuery触发鼠标中键单击不起作用?

时间:2019-02-26 01:19:09

标签: jquery tampermonkey

以下Tampermonkey js可以正常工作(等待链接显示出来然后单击它):

// ==UserScript==
// @name     converto.io click the download link
// @include    *://*.converto.io/en/download/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @run-at document-end
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */

setTimeout(main, 1000);

function main(){
   waitForKeyElements ("#download-url", clickNode, true);
}

function clickNode (jNode) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

现在,我想将其更改为鼠标中键,我用了很长时间的Google搜索并汇编了以下代码,但它不起作用,并且没有错误提示。 任何想法?感谢任何回应。

setTimeout(main, 1000);

function main(){
  waitForKeyElements ("#download-url", clickNode, true);
}

function clickNode (jNode) {
  if ( window.CustomEvent ) {
      var middleClick = new MouseEvent( "click", { "button": 1, "which": 1 });
      jNode[0].dispatchEvent( middleClick );
  }
}

0 个答案:

没有答案