nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

CustomEvent not work in latest nw.js build 0.73.0

Open omenus opened this issue 1 year ago • 14 comments

Issue Type

Before opening an issue, please search and see if it has already been raised.

  • [ ] Bug Report

  • [ ] Uncaught TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'. at index.js:9:8

  • [ x] Successfully reproduced against the latest version of NW.js? yes 0.73.0

Please use our mailing list or Gitter chatroom to ask questions. The issue tracker is only for bugs and feature requests, in English only. Please note that issues without a repro or code snippet are less likely to be resolved.

Current/Missing Behavior

CustomEvent

Expected/Proposed Behavior

class CustomEvent extends Event {} (when this is added to code, then its work, but not always can be added in libraries scope like uPlot

there is error: Uncaught TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'. at index.js:9:8

There is no such issue in older version like 0.52.x but in 0.70.x there is was this event

Additional Info

  • Operating System: Windows 10
  • NW.js Version: 0.73.0
  • Repro Link:
  • Code snippet:

window.addEventListener("testevent", (e) => { console.log(e); });

let ev = new CustomEvent("testevent", { bubbles: true, cancelable: false }); window.dispatchEvent(ev);

  • Crash report:
  • Uncaught TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'. at index.js:9:8

nwapp.zip

omenus avatar Mar 02 '23 19:03 omenus

I made an automated test using Chromedriver first and the test passed for me.

I also manually ran your code snippet and it worked for me. image

My guess is the TypeError has to do with your IDE.

ayushmanchhabra avatar Mar 02 '23 21:03 ayushmanchhabra

Please try download latest 0.73.0 sdk version (Windows), unzip and add my zipped mini app test, and run: npm run dev or ..\nwjs-sdk-v0.73.0-win-x64\nw.exe . -as configured in package.json. There is no IDE, just simple run (npm -v 8.19.2, node v18.12.1 - but this should be not important)

Please don't close this issue as it is valid, test this with final sdk version.

omenus avatar Mar 03 '23 06:03 omenus

Apologies! I must skipped over the attached zip file. Currently afk but will try it out soon.

ayushmanchhabra avatar Mar 03 '23 06:03 ayushmanchhabra

Was able to reproduce. This only happens in Mixed Content:

image

Let me investigate if this is a bug or intended behaviour.

ayushmanchhabra avatar Mar 15 '23 20:03 ayushmanchhabra

This might be a limitation of Mixed Context.

While differences of contexts are generally benefitial, sometimes they may constitute a problem in your (or some other person’s) code, and a need for a workaround arises.

For example, in different browser contexts, the global objects are not identical and some type checking tricks will fail with multiple contexts.

I don't know enough about the internals and maybe @rogerwang can weight in here?

ayushmanchhabra avatar Mar 15 '23 20:03 ayushmanchhabra

Observing the same breaking change, tested in NWv75. This has always worked in previous NW versions running in "mixed-context" mode. "CustomEvent" inherits from "Event" (which does work). This is certainly a bug. @rogerwang -- please advise.

alex-aloia avatar Apr 12 '23 16:04 alex-aloia

What was the last version this worked?

ayushmanchhabra avatar Apr 17 '23 21:04 ayushmanchhabra

Appears to have broken in v0.70.1.

v0 70 1

v0 70 0

alex-aloia avatar Apr 18 '23 16:04 alex-aloia

Works for me in 0.75 Mac - will test on windows

Screenshot 2023-05-19 at 10 30 24

nebular avatar May 19 '23 08:05 nebular

image

CustomEvent still doesn't work on 0.84 linux

iqrok avatar Feb 22 '24 08:02 iqrok

Also not working for me in 0.84 Windows, does not happen when not using --mixed-context

image

xenobytezero avatar Feb 22 '24 12:02 xenobytezero

I was able to make CustomEvent works in mixed context by building from source and modified this function to do nothing https://github.com/nwjs/node/blob/nw87/lib/internal/process/pre_execution.js#L397-L404

image

It seems like node's CustomEvent is the cause. It replaces browser CustomEvent in mixed context and perhaps enabled by default since node v19.

I think that's why this error started to appear in nw 0.70.1, but not in 0.70.0

iqrok avatar May 16 '24 16:05 iqrok

A work around:

let ev = document.createEvent("customevent"); ev.initCustomEvent('dcs-update', true, true, { name: 'Harry' }); document.dispatchEvent(ev);

This is supposed to be deprecated but works in the newest NWjs. Of course a class could be created to extend out the CustomEvent from the Event class with the above code in the constructor, since ev instanceof Event is true.

harrychiling avatar Jun 20 '24 14:06 harrychiling

An interim solution, writing a polyfill:

class CustomEvent extends Event {
  constructor(type, options = {}) {
    super(type, options);
    this.detail = options?.detail ?? null;
  }
}

ssnangua avatar Jul 09 '24 16:07 ssnangua