The javascript API is provided by an object implementing JavascriptBridge
.
It is available only to html pages on the filesystem (http and https pages cannot access it).
It provides the following javascript methods:
-
NetLogo.evaluateCommand(commandText, onCompletion, onError)
returns
undefined
.commandText
- The text of the command to be evaluatedonCompletion
- zero-argument function called on successful completiononError
- one-argument function called with a java Exception on error
-
NetLogo.evaluateReporter(reporterText, onResult, onError)
returns
undefined
.reporterText
- The text of the reporter to be evaluatedonResult
- one-argument function called on successful completion with the result of the procedure. This can be any of the following:- A string
- A boolean
- A LogoList
- A number (whose numeric value must be obtained with
doubleValue()
. Seehtml/netlogo.js
for an example.
onError
- one-argument function called with a java Exception on error
-
NetLogo.log(message)
and `NetLogo.errreturns
undefined
.message
- A string which will be written toSystem.out
.
Start a new tab for webview. Will open as the farthest right tab. Optionally accepts a tab name and a document to open. If a tab or frame is open, an exception will be thrown.
Example:
(webview:create-tab) ; creates a tab at far right with name "Webview", open to "html/index.html"
webview:create-tab "Extra Tab Fun!" ; creates a tab with name "Extra Tab Fun!"
webview:create-tab "Test" "test2.html" ; creates a tab with name "Test", open to "html/test2.html"
webview:create-frame "Test" "http://www.example.org" ; creates a tab with title "Test", open to "example.org"
Open a new frame for webview. Optionally accepts a tab name and document to open. If a tab or frame is open, an exception will be thrown.
Example:
(webview:create-frame) ; creates a frame at far right with title "Webview", open to "html/index.html"
webview:create-frame "Extra Frame Fun!" ; creates a frame with title "Extra Frame Fun!"
webview:create-frame "Test" "test2.html" ; creates a frame with title "Test", open to "html/test2.html"
webview:create-frame "Test" "http://www.example.org" ; creates a frame with title "Test", open to "example.org"
Directs the system-default browser to open the specified URL. The browser will launch in a separate process. Consequently, further calls to webview primitives will not effect this browser.
Example:
webview:browse "htttp://www.example.org/" ; direct the operating system to open the default browser to example.org
Points an open webview frame to a URL.
Example:
webview:load "http://www.google.com
Reloads the current page (useful for if the webview gets stuck).
Example:
webview:reload
Closes the tab or frame currently associated with webview. If no tab or frame is currently open, does nothing.
Example:
webview:close
Execute javascript within the context of the document. If webview has not been started before this call, an error will be raised.
Example:
webview:exec "document.body.appendChild(document.createTextNode('hello!'));"
Execute javascript within the context of the document, waiting for a result to be ready.
Example:
webview:eval "2 + 2" ; => 4
webview:eval "[\"abc\", 123, false]" ; => (list "abc" 123 false)
webview:eval "var o = {\"abc\": 123, \"def\": false}; o" ; => (list (list "abc" 123 (list "def" false))
webview:eval "document.getElementById(\"reporter\").value" ; => "foo"
Adds a predefined webview "module" to the current page. Note that the "NetLogo" and "javaWebView" modules are added by default on all pages loaded from the file system. The modules available are:
- NetLogo
- javaWebView
All other values passed to webview:add-module
will raise an error.
WARNING: Modules give the page you are on the ability to execute commands directly into NetLogo. This allows the page the ability to run arbitrary NetLogo code, from which a malicious user could do significant damage. This functionality should be used with great care and only on trusted pages.
Example:
webview:add-module "NetLogo" ; add the NetLogo module into the current page. See above for description of functionality
webview:add-module "javaWebView" ; adds javaWebView module into the current page.
The javaWebView
module is a binding to the JavaFX WebView containing the current page.
Methods can be called on this object just like any other javascript object.
See also the Oracle documentation for javafx.scene.web.WebView
.
The null javascript value.
Example:
webview:null = webview:eval "null" ; => true
webview:null = webview:eval "2 + 2" ; => false
The undefined javascript value.
Example:
webview:undefined = webview:eval "undefined" ; => true
webview:undefined = webview:eval "2 + 2" ; => false
Switch to the currently open webview tab or frame. Does nothing if no tab or frame currently open.
Example:
webview:focus
Switches focus from the currently open webview frame or tab to the NetLogo interface tab.
Example:
webview:unfocus
Checks whether a Webview tab or frame is open.
Example:
webview:is-open? ; => true
The WebView NetLogo Extension is in the public domain. To the extent possible under law, Uri Wilensky has waived all copyright and related or neighboring rights.