Object built-in object

Object WebView

Browser window object

WebView is a window component embedded in the browser, currently only supports windows, macOS(10.10+). Use IE/Edge kernel in windows, use WKWebView in macOS.

Because the JavaScript program in WebView and fibjs are not in the same engine, if you need to communicate with the host program, you need to use messages.

The object used by WebView for communication is external, which supports one method postMessage and two events onmessage and onclose.

A simple communication example code is as follows:

1 2 3 4 5 6 7 8 9
// index.js var gui = require('gui'); var webview = gui.open('fs://index.html'); webview.onmessage = msg => console.log(msg); webview.onload = evt => webview.postMessage("hello from fibjs"); webview.wait();

The content of index.html is as follows:

1 2 3 4 5 6 7 8
<script> external.onclose = function() { } external.onmessage = function(msg){ external.postMessage("send back: " + msg); }; </script>

Before the user window is closed, the external.onclose event will be triggered, and external.onclose can decide whether to close. If external.onclose returns false, the operation is cancelled, otherwise the window will be closed.

The following example will wait 5 seconds after the user clicks to close and then close the window.

1 2 3 4 5 6 7 8 9 10 11 12
<script lang="JavaScript"> var bClose = false; external.onclose = function () { if (!bClose) { setTimeout(function () { bClose = true; window.close(); }, 5000); return false; } } </script>

In the above code, because window.close itself also triggers the onclose event, a switch variable needs to be added to identify whether the event needs to be processed.

Inheritance

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<class>EventEmitter|new EventEmitter()|EventEmitter|defaultMaxListeners|on();addListener();prependListener();once();prependOnceListener();off();removeListener();removeAllListeners();setMaxListeners();getMaxListeners();listeners();listenerCount();eventNames();emit()] [<this>WebView|type;dev;onopen;onload;onaddress;ontitle;onmove;onresize;onclosed;onmessage;ondownload|loadUrl();getUrl();setHtml();reload();goBack();goForward();print();printToPDF();executeJavaScript();executeDevToolsMethod();close();postMessage()] [object] <:- [EventEmitter] [EventEmitter] <:- [WebView] EventEmitter new EventEmitter() EventEmitter defaultMaxListeners on() addListener() prependListener() once() prependOnceListener() off() removeListener() removeAllListeners() setMaxListeners() getMaxListeners() listeners() listenerCount() eventNames() emit() WebView type dev onopen onload onaddress ontitle onmove onresize onclosed onmessage ondownload loadUrl() getUrl() setHtml() reload() goBack() goForward() print() printToPDF() executeJavaScript() executeDevToolsMethod() close() postMessage() object toString() toJSON()

Static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer WebView.defaultMaxListeners;

Member attributes

type

String, the WebView engine of the current window

1
readonly String WebView.type;

dev

Value, DevTools access object, refer to the calling interface: https://chromedevtools.github.io/devtools-protocol/

1
readonly Value WebView.dev;

onopen

Function, query and bind loading success event, equivalent to on("open", func);

1
Function WebView.onopen;

onload

Function, query and bind loading success event, equivalent to on("load", func);

1
Function WebView.onload;

onaddress

Function, query and bind page address change event, equivalent to on("address", func);

1
Function WebView.onaddress;

ontitle

Function, query and bind the page title change event, equivalent to on("title", func);

1
Function WebView.ontitle;

onmove

Function, query and bind window movement events, equivalent to on("move", func);

1
Function WebView.onmove;

The following example will output the coordinates of the upper left corner of the window when the window is moved:

1 2 3 4
var gui = require('gui'); var webview = gui.open('fs://index.html'); webview.onmove = evt => console.log(evt.left, evt.top);

onresize

Function, query and bind window size change event, equivalent to on("size", func);

1
Function WebView.onresize;

The following example will output the size of the window when the window is resized:

1 2 3 4
var gui = require('gui'); var webview = gui.open('fs://index.html'); webview.onresize = evt => console.log(evt.width, evt.height);

onclosed

Function, query and bind window close event, this time will be triggered after WebView is closed, which is equivalent to on("closed", func);

1
Function WebView.onclosed;

onmessage

Function, query and bind to accept postMessage event in webview, equivalent to on("message", func);

1
Function WebView.onmessage;

ondownload

Function, query and bind to accept the download transaction status change event in webview, which is equivalent to on("download", func);

1
Function WebView.ondownload;

Member function

loadUrl

Load specified url Page of

1
WebView.loadUrl(String url) async;

Call parameters:

  • url: String, specified url

getUrl

Query the current page url

1
String WebView.getUrl() async;

Return result:

  • String, Return to the current page url

setHtml

Set the page html of webview

1
WebView.setHtml(String html) async;

Call parameters:

  • html: String, set html

reload

Refresh the current page

1
WebView.reload() async;

goBack

Go back to the previous page

1
WebView.goBack() async;

goForward

Advance to the next page

1
WebView.goForward() async;

print

Print the current window document

1
WebView.print(Integer mode = 1) async;

Call parameters:

  • mode: Integer, print parameter, 0: fast print; 1: standard print; 2: print preview. Default is 1

printToPDF

Print the current window document to a PDF file

1
WebView.printToPDF(String file) async;

Call parameters:

  • file: String, specify the pdf path

executeJavaScript

Run a piece of JavaScript code in the current window

1
WebView.executeJavaScript(String code) async;

Call parameters:

  • code: String, specify the JavaScript code to be executed

executeDevToolsMethod

Execute the DevTools command in the current window and return the result

1 2
Variant WebView.executeDevToolsMethod(String method, Object params = {}) async;

Call parameters:

  • method: String, specify the DevTools command to be executed
  • params: Object, specify the parameters of the command

Return result:

  • Variant, Return the result of execution

close

Close the current window

1
WebView.close() async;

postMessage

Send a message to the webview

1
WebView.postMessage(String msg) async;

Call parameters:

  • msg: String, the message to be sent

    postMessage needs to send a message after the window is loaded, messages sent before that will be lost. Therefore, it is recommended to call this method after the onload event is triggered.


on

Bind an event handler to the object

1 2
Object WebView.on(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Bind an event handler to the object

1
Object WebView.on(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

addListener

Bind an event handler to the object

1 2
Object WebView.addListener(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Bind an event handler to the object

1
Object WebView.addListener(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

prependListener

Bind an event handler to the start of the object

1 2
Object WebView.prependListener(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Bind an event handler to the start of the object

1
Object WebView.prependListener(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

once

Bind a one-time event processing function to the object, the one-time processing function will only be triggered once

1 2
Object WebView.once(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Bind a one-time event processing function to the object, the one-time processing function will only be triggered once

1
Object WebView.once(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

prependOnceListener

Bind an event handler to the start of the object

1 2
Object WebView.prependOnceListener(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Bind an event handler to the start of the object

1
Object WebView.prependOnceListener(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

off

Cancel the specified function from the object processing queue

1 2
Object WebView.off(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Cancel all functions in the object processing queue

1
Object WebView.off(String ev);

Call parameters:

  • ev: String, specify the name of the event

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Cancel the specified function from the object processing queue

1
Object WebView.off(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

removeListener

Cancel the specified function from the object processing queue

1 2
Object WebView.removeListener(String ev, Function func);

Call parameters:

  • ev: String, specify the name of the event
  • func: Function, specify event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Cancel all functions in the object processing queue

1
Object WebView.removeListener(String ev);

Call parameters:

  • ev: String, specify the name of the event

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Cancel the specified function from the object processing queue

1
Object WebView.removeListener(Object map);

Call parameters:

  • map: Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function

Return result:

  • Object, Return the event object itself, which is convenient for chain call

removeAllListeners

Cancel all the listeners of all events from the object processing queue. If an event is specified, remove all the listeners of the specified event.

1
Object WebView.removeAllListeners(String ev);

Call parameters:

  • ev: String, specify the name of the event

Return result:

  • Object, Return the event object itself, which is convenient for chain call

Cancel all the listeners of all events from the object processing queue. If an event is specified, remove all the listeners of the specified event.

1
Object WebView.removeAllListeners(Array evs = []);

Call parameters:

  • evs: Array, specify the name of the event

Return result:

  • Object, Return the event object itself, which is convenient for chain call

setMaxListeners

The default limit of the number of listeners, only for compatibility

1
WebView.setMaxListeners(Integer n);

Call parameters:

  • n: Integer, specify the number of events

getMaxListeners

Get the default limit of the number of listeners, only for compatibility

1
Integer WebView.getMaxListeners();

Return result:

  • Integer, Return to the default limit number

listeners

Array of listeners for the specified event of the query object

1
Array WebView.listeners(String ev);

Call parameters:

  • ev: String, specify the name of the event

Return result:

  • Array, Returns an array of listeners for the specified event

listenerCount

The number of listeners for the specified event of the query object

1
Integer WebView.listenerCount(String ev);

Call parameters:

  • ev: String, specify the name of the event

Return result:

  • Integer, Returns the number of listeners for the specified event

The number of listeners for the specified event of the query object

1 2
Integer WebView.listenerCount(Value o, String ev);

Call parameters:

  • o: Value, specify the object of the query
  • ev: String, specify the name of the event

Return result:

  • Integer, Returns the number of listeners for the specified event

eventNames

Query the event name of the listener

1
Array WebView.eventNames();

Return result:

  • Array, Returns an array of event names

emit

Trigger an event actively

1 2
Boolean WebView.emit(String ev, ...args);

Call parameters:

  • ev: String, event name
  • args: ..., event parameters, which will be passed to the event handler

Return result:

  • Boolean, Return to the event trigger state, return true if there is a response event, otherwise return false

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String WebView.toString();

Return result:

  • String, Returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value WebView.toJSON(String key = "");

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable