[{InsertPage page='SecurityRisk'}]

This [Contributed Plugin] allows you to execute JavaScript code on the server. Useful for creating pages with dynamic content. This plugin is experimental at this stage; clearly better wiki integration and server safety issues need to be addressed.

See also:
* [Groovy Plugin]
* [Tcl Plugin]

The plugin's JavaScript implementation is Mozilla's [Rhino|]. The public discussion for Mozilla's JavaScript implementations is at [netscape.public.mozilla.jseng|].

For more information please contact [Andrew Gilmartin] at andrewgilmartin at

!! Building
[{SET JWV='2.4.24'}]
[{SET JSV='1.6R2'}]
[{SET XBV='2.20'}]

To built you will need to add the following libraries to the project's lib directory:
* JSPWiki.jar %%(background:yellow;) (version [{$JWV}] or better);
* servlet-api.jar from your Tomcat distribution;
* js.jar from Rhino %%(background:yellow;) [{$JSV}] %% at [];
* xbean.jar from XMLBeans %%(background:yellow;) [{$XBV}]  %% at []. 

Then run {{ant}}.

The code does not yet conform to the [JSPWiki Coding Standard|My Coding Standard].

!! Installing

Copy the file {{build/jspwiki-js-plugin.jar}}, {{lib/js.jar}}, and {{lib/xbean.jar}} to JSPWiki's {{WEB-INF/lib}} directory.

!! Using

The plugin is called using

%%( font-size: 80%; margin: 2ex; )
[{com.ingenta.jspwiki.plugin.js.JSPlugin scriptPage='wikiPage' scriptResult='text|xml|wiki' }]

[{com.ingenta.jspwiki.plugin.js.JSPlugin script='javascript' scriptResult='text|xml|wiki' }]

[{com.ingenta.jspwiki.plugin.js.JSPlugin scriptResult='text|xml|wiki'

.. javascript body content ..

[{scriptPage scriptResult='text|xml|wiki'

.. data body content ..

For most installations you need only refer to the plugin as ''JSPlugin'' as the installation will have added {{com.ingenta.jspwiki.plugin.js}} to the ''jspwiki.plugin.searchPath'' property.

The ''script'' and ''scriptPage'' parameter or body content selects the JavaScript code to execute. The ''scriptResult'' parameter determines how the script's result is further processed; ''xml'' result output is sent as is to the browser; ''text'' result output is send to the browser after escaping &, <, >, and " characters; ''wiki'' result output is send to the browser after first converting to HTML. The default scriptResult value is ''wiki''.

Using the ''scriptPage'' as the name of the plugin only works for JSPWiki implementations using the modified ''com.ecyrd.jspwiki.plugin.PluginManager'' supporting the ''jspwiki.plugin.unknownPlugin'' property. For more information see [ScriptedPlugin|].

During execution the following global variables are available to the script:

| wikiInfo | An instance of class JSWikiInfo
| wikiPage | An instance of class JSWikiPage for the page containing the plugin
| pluginArgs | An associative array containing the arguments to the plugin. For example {{format = pluginArgs[[ "scriptResult" ];}}.
| pluginBody | The content of the plugin body. This is only set if the body does not contain the script.

See [JSPluginScripts] for examples of scripts.

Note that the current interpreter is not a safe one. Thus all event, file, socket, and use of Java classes is enabled.

!! Class and method detail

! class ~JSWikiPage
| | The name of the page.
| | The author name of the most recient version.
| o.created | The creation date of the most recient version.
| o.modified | The last modified date of the most recient version.
| o.versionNumber | The version number of the most recient version.
| o.versions | Returns a list of page's versions. See JSWikiPageVersionInfo
| o.attachments | Returns a list of the page's attachments. See JSWikiPageAttachmentInfo
| o.content | Returns the content of the page.
| o.setContent( pageContent ) | Sets the content of the page (and creates a new version)

! class ~JSWikiPageVersionInfo
| | The name of the author.
| o.modified | The last modified date.
| o.versionNumber | The version number.

! class ~JSWikePageAttachmentInfo

|| method || description
| o.fileName | The attachment's file name.
| o.fileSize | The attachment's file size in bytes.

! class ~JSWikiInfo
|| method || description
| o.pluginVersion | The version number of this plugin.
| o.pages | Returns a list of the pages.
| o.hasPage( pageName ) | Returns true if the named page exists.
| o.getPage( pageName ) | Returns the named page. It is an error if the page does not exist.
| o.createPage( pageName, pageContent ) | Creates a new page with the given name and content. It is an error if the page already exists.
| o.deletePage( pageName ) | Deletes the named page. It is an error if the page does not exist.


! Comments

Q: I can't get the JSPlugin compiled yet. Is it possible to post the .jar on this page.\\
My compiler complains about the call to {{wikiEngine.saveText(...)}} in
-- [DF|DirkFrederickx]

A: Done. Note that this version works with JSPWIki 2.0.39. -- [Andrew Gilmartin]

Q: Help of how to use this plugin is not good enough. e.g. where do we neet to use scriptPage and why? in which cases is it suitable to use it and all things should be given properly.
-- [Kiran thakkar]

A: There are a number of examples on the [JSPluginScripts] page. Perhaps these will answer your questions. Otherwise, please do send me email with some context of how you want to use the plugin and perhaps with all this information I can help. -- [Andrew Gilmartin]

Attached the source code and compiled jar file for a version od JSPlugin that was fixed to work with the latest versions of JSPWiki, Rhino and XMLBeans. \\
-- [Nascif Abousalh-Neto], 26-Jul-2006