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.

Since 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 is 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 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

Static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer WebView.defaultMaxListeners;

Member attributes

visible

Boolean, query and set whether the window is displayed

1
Boolean WebView.visible;

onload

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

1
Function WebView.onload;

onmove

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

1
Function WebView.onmove;

The following example outputs 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, 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;

Member function

setHtml

Set the page html of webview

1
WebView.setHtml(String html) async;

Call parameters:

  • html : String, set html

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

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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependListener

Bind an event handler to the object start

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 , returns the event object itself, easy to chain call

Bind an event handler to the object start

1
Object WebView.prependListener(Object map);

Call parameters:

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

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependOnceListener

Bind an event handler to the object start

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 , returns the event object itself, easy to chain call

Bind an event handler to the object start

1
Object WebView.prependOnceListener(Object map);

Call parameters:

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

Return result:

  • Object , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to 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 , returns the event object itself, easy to chain call

removeAllListeners

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

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

Call parameters:

  • evs : Array, specify the name of the event

Return result:

  • Object , returns the event object itself, easy to 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 , returns 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

eventNames

Query listener event name

1
Array WebView.eventNames();

Return result:

  • Array , returns an array of event names

emit

Actively trigger an event

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 , returns the event trigger status, returns true if there is a response event, otherwise returns 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