单击鼠标左键时使此功能起作用?

Get this function to work when I left mouse click?

我正在创建一个扩展程序并打算使用 tampermonkey 来 运行 它,我希望它在我左键单击并按住它时仍能正常工作 运行。

// @run-at       document-end

(function() {
var amount = 6;
var duration = 50; //ms

var overwriting = function(evt) {
    if (evt.keyCode === 0) { // Left mouse click?
        for (var i = 0; i < amount; ++i) {
            setTimeout(function() {
                window.onkeydown({keyCode: 87}); // KEY_W
                window.onkeyup({keyCode: 87});
            }, i * duration);
        }
    }
};

window.addEventListener('keydown', overwriting);
})();

尝试使用 "mousedown" 代替:

(function() {
var amount = 6;
var duration = 50; //ms

var overwriting = function(evt) {
    for (var i = 0; i < amount; ++i) {
        setTimeout(function() {
            window.onkeydown({keyCode: 87}); // KEY_W
            window.onkeyup({keyCode: 87});
        }, i * duration);
    }
};

window.addEventListener('mousedown', overwriting);
})();

"event.which" ,对于键或鼠标事件,此 属性 表示按下的特定键或按钮。

// @run-at       document-end

(function() {
var amount = 6;
var duration = 50; //ms

var overwriting = function(evt) {
    if (evt.keyCode === 0) { // Left mouse click?
        for (var i = 0; i < amount; ++i) {
            setTimeout(function() {
                window.onkeydown({keyCode: 87}); // KEY_W
                window.onkeyup({keyCode: 87});
            }, i * duration);
        }
    }
};
/*
  1 = Left   mouse button
  2 = Centre mouse button
  3 = Right  mouse button
*/

window.addEventListener('mousedown', function(event) {
  if (event.which === 1) {
/* Left mouse button was clicked! */
    overwriting();
   }
  });
})();