3

I tried dispatchEvent in the background in chrome, but it didn't work. When i try it in firefox, It worked delayed.

I'm trying in the same code firefox slow running chrome also does not work, I think this is browser-related.

How do I revoke these restrictions on browsers? or can you recommend a browser without restrictions?

.

coordX = 100 // Moving from the left side of the screen
 coordY = 100 // Moving in the center


    // Create new mouse event
     ev = new MouseEvent("mousemove", {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: coordX,
        clientY: coordY
    });
window.document.querySelector('#canvas').dispatchEvent(ev);
Efe Zeybek
  • 83
  • 1
  • 2
  • 14

2 Answers2

0

You have not bind event Listener so you not able to see function dispatch call

for more info you can check mozilla network [https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events][1]

coordX = 100 // Moving from the left side of the screen
     coordY = 100 // Moving in the center


    // Create new mouse event
     ev = new MouseEvent("click", {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: coordX,
        clientY: coordY
    });
var domEl=window.document.querySelector('#canvas')
domEl.addEventListener('click', function (e) { 
      setTimeout(() => {
        alert('success :'+stringifyEvent(ev));
       }, 5000);
  }, false);

function dispatchcall(){
   
       setTimeout(() => {
           domEl.dispatchEvent(ev);
    }, 5000);
   

}



//**for  stringify event object** 

function stringifyEvent(e) {
  const obj = {};
  for (let k in e) {
    obj[k] = e[k];
  }
  return JSON.stringify(obj, (k, v) => {
    if (v instanceof Node) return 'Node';
    if (v instanceof Window) return 'Window';
    return v;
  }, ' ');
}
document.getElementById("myBtn").addEventListener("click", dispatchcall);
<h2 id="canvas">canvas mouse click here after clicking on dispatch event Then wait 5 sec, if you wish you can switch tab come back after 5 sec</h2>

<button id="myBtn">dispatch event</button>
Jadli
  • 858
  • 1
  • 9
  • 17
  • My problem is that it doesn't work or delay on the background tab. The cause of this problem maybe settimeout. but it doesn't work in chrome, but it works late in firefox. Is there a library that redefines the settimeout function to run in the background? https://stackoverflow.com/q/6032429/10004713 – Efe Zeybek Nov 14 '19 at 13:30
  • I made some change on code, now i am calling dispatch event using set-timeout, if you still facing issue then please share some more working code.. – Jadli Nov 14 '19 at 15:25
  • Perhaps this will help: https://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background/11310496#11310496 – Jadli Nov 14 '19 at 15:36
0

To find out which browsers support different APIs, you can use the site caniuse.com

https://caniuse.com/#feat=dispatchevent

David Bradshaw
  • 11,859
  • 3
  • 41
  • 70