Skip to content

WebContentsConsoleMessageEvent ​

WebContentsConsoleMessageEvent represents a log message the web page wrote to console.

The console-message event of WebContents is this class. It carries the log level, text, and location. Uncaught exceptions in the page are reported here too.

Inheritance: Event → WebContentsConsoleMessageEvent

Constructor ​

WebContentsConsoleMessageEvent()

Creates a WebContentsConsoleMessageEvent. The runtime creates one itself when it fires the events below; app code can use it to build an event object and fire it with dispatchEvent() to try out a listener.

Instance properties ​

Also inherits the properties of Event; the most commonly used are type and target. The properties below are read-only; assigning to them throws TypeError.

PropertyDescription
level read-onlyThe log level
message read-onlyThe log text
lineNumber read-onlyThe line where it was called
sourceId read-onlyThe file where it was called

These properties live on the prototype, not on the event object itself: Object.keys(event) and { ...event } don't include them, so read them one by one. See Events with fields.

Events that use this class ​

ObjectEvents
WebContentsconsole-message

It can't be canceled: cancelable is false, and preventDefault() has no effect.

Examples ​

js
const contents = new WebView().webContents;
contents.addEventListener('console-message', (e) => {
  console.log(e instanceof WebContentsConsoleMessageEvent, '[网页 ' + e.level + '] ' + e.message);
});
contents.loadURL('data:text/html;charset=utf-8,' +
  encodeURIComponent('<script>console.warn("w");</script>'));

Effect: prints true [网页 warning] w.

Corresponding iOS API ​

There is no corresponding system API. The event name and field names follow the event of the same name on Electron's webContents.

See also ​