nw.js
nw.js copied to clipboard
CustomEvent not work in latest nw.js build 0.73.0
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
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.
My guess is the TypeError has to do with your IDE.
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.
Apologies! I must skipped over the attached zip file. Currently afk but will try it out soon.
Was able to reproduce. This only happens in Mixed Content:
Let me investigate if this is a bug or intended behaviour.
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?
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.
What was the last version this worked?
Appears to have broken in v0.70.1.
Works for me in 0.75 Mac - will test on windows
CustomEvent still doesn't work on 0.84 linux
Also not working for me in 0.84 Windows, does not happen when not using --mixed-context
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
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
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.
An interim solution, writing a polyfill:
class CustomEvent extends Event {
constructor(type, options = {}) {
super(type, options);
this.detail = options?.detail ?? null;
}
}