Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Simulate Ctrl+Mouse Click & Keypress Events

edited January 2017 in Development Firefox

Below is code that simulate mouse click but without ctrl button. How do I simulate it ctrl+mouse click.

Also I am unable to get keypress to work, if anyone knows how I can get it to work, appreciate it.

var event = new MouseEvent('click', {
                    'view': window,
                    'bubbles': true,
                    'cancelable': true
                });

                link.dispatchEvent(event);

Related pages:
https://developer.mozilla.org/en-US/docs/Web/API/Event

Comments

  • <h4 href=# id=H style="cursor:pointer">AUTO CLICK</h4>
    <script>
    function ctrlClick(link){
    var event = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': true,
                        'ctrlKey': true
                    });
      return link.dispatchEvent(event);
    }   
    var link = document.getElementById("H");
    link.addEventListener("click",
     function(e){alert('I am clicked with'+(e.ctrlKey?'':'out')+' CTRL')}
    );
    ctrlClick(link);
    </script>
    

    seems working as expected

  • edited February 2017 Firefox

    Thank you.

    I already tried that on links but I cannot get it to work. Works on buttons but not links. My main goal is to open a tab without focusing on it. FYI: I am using FireFox.

    var l = document.createElement("a");
    l.href = "http://www.example.com";
    document.body.insertBefore(l, document.body.firstElementChild);    
    ctrlClick(document.querySelector("a"));
    
  • edited February 2017 Firefox

    Update: It brings up the message but it does not open the link in a new tab. Could be a security feature.
    I always just tried to see if it opens a tab, never placed a message in there before.

  • It brings up the message but it does not open the link in a new tab.

    GM_openInTab
  • Yes, it is a security feature implemented by at least Chrome and Firefox that I know of.
  • That makes sense.

    It brings up the message but it does not open the link in a new tab.

    GM_openInTab
    Thanks, I raised a suggestion for it. It never cross my mind it already exists. :smile:
Sign In or Register to comment.