MochaJSWebScriptingObject is intended to be an simple way for Mocha CocoaScript scripts to expose functions to WebView.
Originally written for use in Sketch 3+.
The following example will create a WebView and expose a function to it:
@import'MochaJSWebScriptingObject.js'// Create a WebViewvarwebView=WebView.new();// Expose functionsvarwebScriptingObject=MochaJSWebScriptingObject({demo: function(){varapp=[NSApplicationsharedApplication];[appdisplayDialog:"This is being called from a WebView! Cool!"withTitle:"Alert"];}});varwindowScriptObject=webView.windowScriptObject();windowScriptObject.setValue_forKey_(webScriptingObject,"exported");windowScriptObject.evaluateWebScript_("exported.demo();");MochaJSWebScriptingObject leverages Mocha's methods for manipulating the Objective-C runtime. It creates an NSObject complying with the WebScripting protocol and manages function exposure between CocoaScript -> Objective-C -> WebView.