Cloning and re-dispatching `TouchEvent` in Android system WebView

Viewed 133

I am trying to handle a TouchEvent by re-dispatching it to another DOM element. Everything works perfectly on iOS and desktop browsers with touch simulation using the following code:

onTouched(event: TouchEvent) {
  this.targetElement.getNativeElement()
    .dispatchEvent(new TouchEvent(event.type, event));
}

However, I can't get this working on Android device. I tried with a mobile phone and simulator. The problem lies in the code constructing a TouchEvent.

Here is what I tried:

  1. new TouchEvent(event.type, event) gives Illegal constructor error

  2. new event.constructor(event.type, event) gives Illegal constructor error

  3. I tried to strip event from all other fields other than touches to fit Typescript's TouchEventInit interface:

    let touchArr: Touch[] = [];
    for (var _i = 0; _i < event.touches.length; _i++) {
      touchArr[_i] = event.touches[_i];      
    }
    
    let newEvent = new TouchEvent(event.type, <TouchEventInit>{
      touches: touchArr      
    });
    

    but it still gives Illegal constructor exception.

  4. In the act of desperation, I tried to use a deprecated document.createEvent:

    let newEvent: any = document.createEvent('TouchEvent');
    newEvent.initTouchEvent(event);
    

    However, the initTouchEvent does not set the type for some reason (probably because of this one) and it shouldn't be used anyway as it's going to be removed.

I am simply out of ideas. I tried with the newest Android System WebView (60.0.3112.116). I could understand this to happen on a desktop browser, but it's a mobile device...

0 Answers
Related