in Which browser can I use "dispatchevent" in the background tab?

Viewed 357

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);
2 Answers

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>

Related