Classes
The following classes are available globally.
-
A JavaScript expression that executes a function in the current JavaScript
this
. The function variable is referenced by a key path relative to the currentthis
.For instance, to present an alert:
let alert = JSFunction<JSVoid>("window.alert", arguments: "Hello from Swift!") // equivalent to the JS script: `this.window.alert("Hello from Swift!");`
Instances of this class are specialized with the
T
generic parameter. It must be set to the return type of the JavaScript function to execute. Check the documentation of the JavaScript function to know what to set the parameter to.T
must be aDecodable
type. This includes:JSVoid
for functions that do not return a value- Primitive values (Strings, Numbers, Booleans, …)
- Decodable enumerations
- Objects decodable from JSON
- Arrays of primitive values
- Arrays of enumeration cases
- Arrays of objects
- Native dictionaries
Declaration
Swift
public final class JSFunction<T> : JSExpression where T : Decodable
-
A JavaScript expression that executes a user-defined script. This class allows you to evaluate your own custom scripts.
For instance, to return the text of the longest
node in the current document:
let javaScript = """ var longestInnerHTML = ""; var pTags = document.getElementsByTagName("p"); for (var i = 0; i < pTags.length; i++) { var innerHTML = pTags[i].innerHTML; if (innerHTML.length > longestInnerHTML.length) { longestInnerHTML = innerHTML; } } longestInnerHTML; """ let findLongestText = JSScript<String>(javaScript) // this is equivalent to running the script inside a browser's JavaScript console.
Instances of this class are specialized with the
T
generic parameter. It must be set to the type of the last statement in your script. In the above example,findLongestText
has a return type ofString
because its last statement is a String (longestInnerHTML
).T
must be aDecodable
type. This includes:JSVoid
for scripts that do not return a value- Primitive values (Strings, Numbers, Booleans, …)
- Decodable enumerations
- Objects decodable from JSON
- Arrays of primitive values
- Arrays of enumeration cases
- Arrays of objects
- Native dictionaries
Declaration
Swift
public final class JSScript<T> : JSExpression where T : Decodable
-
A JavaScript expression that returns the value of a variable in the current JavaScript
this
. The variable is referenced by a key path relative to the currentthis
.For instance, to get the title of the current document:
let title = JSVariable<String>("document.title") // equivalent to the JS script: `this.document.title;`
Instances of this class are specialized with the
T
generic parameter. It must be set to the type of the JavaScript variable to query. Check the documentation of the JavaScript variable to know what to set the parameter to.T
must be aDecodable
type. This includes:- Primitive values (Strings, Numbers, Booleans, …)
- Decodable enumerations
- Objects decodable from JSON
- Arrays of primitive values
- Arrays of enumeration cases
- Arrays of objects
- Native dictionaries
Declaration
Swift
public final class JSVariable<T> : JSExpression where T : Decodable