WebView
Load a page and read from it
Loads a page and runs JavaScript inside it, which reaches anything a browser can see. Use WebView.read to load a page and pull one value out; the constructor is for asking several questions of one page. It can also show the page. Every call blocks until the page or the script finishes. In-app only: a widget has no view hierarchy to build one in, so check WebView.isAvailable. A web view is heavy; call close() when you are done rather than leaving several open.
Properties
isAvailableBooleanFalse in a widget.
Methods
.loadURL(url, options?)RootlessWebViewLoads a page and blocks until it has finished.
- url String required
The page to load.
- options Any
- timeout Duration
How long to wait. Default 30 seconds.
- timeout Duration
.loadHTML(html, options?)RootlessWebViewLoads markup instead of fetching a page.
- html String required
The markup.
- options Any
- baseURL String
Where relative links and assets resolve from.
- timeout Duration
How long to wait for the load.
- baseURL String
.evaluate(script)AnyRuns JavaScript inside the loaded page and returns its result.
- script String required
The last expression is what comes back.
returns Anything JSON-able. A DOM node or a function does not cross and comes back null.
.present(options?)RootlessWebViewShows the loaded page as a bottom sheet, and returns when it is dismissed.
- options Any
- detents [Any]
The heights it can rest at: "medium", "large", or a fraction between 0 and 1.
- grabber Boolean
Show the drag handle at the top.
- cornerRadius Number
Corner radius of the sheet.
- detents [Any]
.close()Releases the web view. Worth doing explicitly: they are not small.
WebView()RootlessWebViewCreate a web view.
WebView.read(url, script, options?)AnyLoads a page, evaluates the script in it, returns the result, and throws the view away.
- url String required
The page to load.
- script String required
JavaScript evaluated in the page. Its last expression is the result.
- options Any
- timeout Duration
How long to wait for the load and the script. Default 30 seconds.
- baseURL String
Resolves relative URLs when the page is passed as HTML rather than fetched.
- timeout Duration
Examples
Scraping one value
// A site with no API at all
var price = WebView.read(
"https://example.com/product",
"document.querySelector('.price').innerText"
);
Script.setWidget(new Widget({ child: Text(price, { font: Font.TITLE }) }));Several questions, one page
var web = new WebView();
web.loadURL("https://example.com");
var title = web.evaluate("document.title");
var links = web.evaluate(
"Array.from(document.querySelectorAll('a')).slice(0,5).map(a => a.href)"
);
web.close();