%META:TOPICINFO{author="TWikiContributor" date="1495793747" format="1.1" version="$Rev$"}%
---+!! !EmbeddedJSPlugin API

This page lists all the API functions for [[EmbeddedJSPlugin]]. See also [[EmbeddedJSPluginTableAPI][EJS Table API List]].

<!--
   * Set EJS = off
   * Set SAVE_ACTION_POLICIES = See also [[EmbeddedJSPlugin#Save_Action_Policies][Save Action Policies]].
   * Set DATE_TIME_VALUES = See also [[EmbeddedJSPlugin#Date_Time_Values][Date/Time Values]].
-->

%TOC%

---++ print
<verbatim>
print("text"[, "text" ...]);
</verbatim>

Appends output texts.
Multiple arguments are appended altogether.
If non-string objects are given, [[#JSON][JSON.stringify()]] is automatically used to serialize the data.

---++ println
<verbatim>
println("text"[, "text" ...]);
</verbatim>

Appends output texts in the same way as [[#print][print()]], followed by a line break at the end.

---++ requireTopic
<verbatim>
requireTopic("[Web.]Topic");
requireTopic({web: "Web", topic: "Topic"});
</verbatim>

Executes the topic content as EJS. A topic is loaded only once even when used multiple times.
It should usually be used to load !JavaScript libraries.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ findWebs
<verbatim>
var webNames = findWebs("Web*");
var webNames = findWebs({web: "Web*"});
var formattedNames = findWebs("Web*", function (webName, loop) {});
var formattedNames = findWebs({web: "Web*", callback: function (webName, loop) {}});
</verbatim>

Returns an array of web names that are matched with a pattern with wildcards.

---++ findTopics
<verbatim>
var topicNames = findTopics("[Web*.]Topic*");
var topicNames = findTopics({web: "Web*", topic: "Topic*"});
var formattedNames = findTopics("[Web*.]Topic*", function (topicName, loop) {});
var formattedNames = findTopics({web: "Web*", topic: "Topic*", callback: function (topicName, loop) {}});
</verbatim>

Returns an array of topic names that are matched with a pattern with wildcards. The web name can also include wildcards.

If a web name is specified, the returned topic names will contain the web name prefix.

For example:

<verbatim>
findTopics('TestTopic*'); // returns ['TestTopic1', 'TestTopic2']
findTopics('WebA.TestTopic*'); // returns ['WebA.TestTopic1', 'WebA.TestTopic2']
findTopics('Web*.TestTopic*'); // returns ['WebA.TestTopic1', 'WebA.TestTopic2', 'WebB.TestTopic1', 'WebB.TestTopic2']
</verbatim>

---++ webExists
<verbatim>
var exists = webExists("Web");
var exists = webExists({web: "Web"});
</verbatim>

Determines if the web exists.

---++ topicExists
<verbatim>
var exists = topicExists("[Web.]Topic");
var exists = topicExists({web: "Web", topic: "Topic"});
</verbatim>

Determines if the topic exists.

---++ createWeb
<verbatim>
createWeb("%WEB%/SubWeb");
createWeb("%WEB%/SubWeb", {baseWeb: "TemplateWeb"});
createWeb({web: "%WEB%/SubWeb", baseWeb: "TemplateWeb"});
</verbatim>

Creates a new web or subweb. Because of the default same-web policy, this API is usually used to create a subweb rather than a top-level web.

If the =baseWeb= parameter is given, the new web is copied from the baseWeb.
Otherwise, the baseWeb defaults to the configured value (=$TWiki::cfg{EmbeddedJSPlugin}{DefaultBaseWeb}=).

An exception is thrown if the web does not exist or if the CREATEWEB permission is denied.

%SAVE_ACTION_POLICIES%

---++ moveWeb
<verbatim>
moveWeb("%WEB%/SubWeb1", "%WEB%/SubWeb2");
moveWeb({web: "%WEB%/SubWeb1", toWeb: "%WEB%/SubWeb2"});
</verbatim>

Renames a web or subweb, or moves it to another web or subweb.

An exception is thrown if the web does not exist or if the RENAMEWEB permission is denied.

Note: Links are *not* automatically updated.

%SAVE_ACTION_POLICIES%

---++ removeWeb
<verbatim>
removeWeb("%WEB%/SubWeb");
removeWeb("%WEB%/SubWeb", {toWeb: "%TRASHWEB%/SubWeb"});
removeWeb({web: "%WEB%/SubWeb", toWeb: "%TRASHWEB%/SubWeb"});
</verbatim>

Removes a web or subweb. The target web will be moved to the trash web, rather than permanently deleted.

An exception is thrown if the web does not exist or if the RENAMEWEB permission is denied.

%SAVE_ACTION_POLICIES%

---++ readTopic
<verbatim>
var topicText = readTopic("[Web.]Topic");
var topicText = readTopic("[Web.]Topic", {rev: revisionNumber});
var topicText = readTopic({web: "Web", topic: "Topic", rev: revisionNumber});
</verbatim>

Returns the topic content.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ saveTopic
<verbatim>
saveTopic("[Web.]Topic", "TopicText");
saveTopic("[Web.]Topic", "TopicText", {forceNewRevision: true, minor: true, breakLock: true, parentTopic: "ParenTopic"});
saveTopic({web: "Web", topic: "Topic", text: "TopicText", forceNewRevision: true, minor: true, breakLock: true, parentTopic: "ParentTopic"});
</verbatim>

Saves the topic content. If the topic does not exist, a new topic is created.

An exception is thrown if the specified web does not exist or if the CHANGE permission is denied.

Additional parameter(s) can be specified as below:

| *Parameter* | *Value* |
| =forceNewRevision= | Force the revision to be incremented, even when the revision number should otherwise stay the same. |
| =minor= | Suppress notification (similarly to =checkpoint= or =quietsave=). |
| =breakLock= | Ignore the edit lock acquired by another user. |
| =parentTopic= | Set the parent topic name. If the value is either =undefined=, =null=, =""=, or ="none"=, then the parent topic will be unset. |
| =formName= | Set the form name (same as [[#setFormName][setFormName()]]) |
| _FormFieldName_ | Form field name/value pairs can be specified to update the values (same as [[#setFormField][setFormField()]]) |

%SAVE_ACTION_POLICIES%

---++ moveTopic
<verbatim>
moveTopic("[FromWeb.]FromTopic", "[ToWeb.]ToTopic");
moveTopic("[FromWeb.]FromTopic", "[ToWeb.]ToTopic", {breakLock: true});
moveTopic({web: "FromWeb", topic: "FromTopic", toWeb: "ToWeb", toTopic: "ToTopic", breakLock: true});
</verbatim>

Renames the topic or moves it to another web.

An exception is thrown if the topic does not exist or if the CHANGE permission is denied.

Additional parameter(s) can be specified as below:

| *Parameter* | *Value* |
| =breakLock= | Ignore the edit lock acquired by another user. |

%SAVE_ACTION_POLICIES%

---++ removeTopic
<verbatim>
removeTopic("[Web.]Topic");
removeTopic("[Web.]Topic", {toTopic: "ToTopic"});
removeTopic({web: "Web", topic: "Topic", toWeb: "%TRASHWEB%", toTopic: "ToTopic"});
</verbatim>

Removes a topic. The target topic will be moved to the trash web, rather than permanently deleted.

An exception is thrown if the topic does not exist or if the CHANGE permission is denied.

%SAVE_ACTION_POLICIES%

---++ getFormName
<verbatim>
var formName = getFormName("[Web.]Topic");
var formName = getFormName("[Web.]Topic", {rev: revisionNumber});
var formName = getFormName({web: "Web", topic: "Topic", rev: revisionNumber});
</verbatim>

Retrieves the form name of the topic.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ setFormName
<verbatim>
setFormName("[Web.]Topic", "ExampleForm");
setFormName({web: "Web", topic: "Topic", name: "ExampleForm"});
</verbatim>

Saves the topic with the new form name.

An exception is thrown if the topic does not exist or if the CHANGE permission is denied.

The call of this function will eventually invoke the same logic as [[#saveTopic][saveTopic()]] so the same parameters are accepted.

%SAVE_ACTION_POLICIES%

---++ getFormField
<verbatim>
var fieldValue = getFormField("[Web.]Topic", "FieldName");
var fieldValue = getFormField("[Web.]Topic", "FieldName", {rev: revisionNumber});
var fieldValue = getFormField({web: "Web", topic: "Topic", name: "FieldName", rev: revisionNumber});
</verbatim>

Retrieves the form field value of the topic.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ setFormField
<verbatim>
setFormField(["Web.]Topic", "FieldName", "FieldValue");
setFormField({web: "Web", topic: "Topic", name: "FieldName", value: "FieldValue"});
</verbatim>

Saves the topic with the new form field name/value pair(s).

An exception is thrown if the topic does not exist or if the CHANGE permission is denied.

The call of this function will eventually invoke the same logic as [[#saveTopic][saveTopic()]] so the same parameters are accepted.
If multiple fields are to be set, it is more efficient to use [[#saveTopic][saveTopic()]] to set all the fields at the same time.

%SAVE_ACTION_POLICIES%

---++ getFormFields
<verbatim>
var fields = getFormFields("[Web.]Topic");
var fields = getFormFields("[Web.]Topic", {rev: revisionNumber});
var fields = getFormFields({web: "Web", topic: "Topic", rev: revisionNumber});
</verbatim>

Retrieves all the form fields of the topic. The return value is an object of field/value pairs.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ checkPermission
<verbatim>
var allowed = checkPermission("[Web.]Topic", "VIEW"); // or "CHANGE"
var allowed = checkPermission({web: "Web", topic: "Topic", type: "VIEW"}); // or "CHANGE"
</verbatim>

Determines if the specified permission type is allowed for the topic.

An exception is thrown if the topic does not exist.

---++ getRevisionInfo
<verbatim>
var revisionInfo = getRevisionInfo("[Web.]Topic");
var revisionInfo = getRevisionInfo("[Web.]Topic", {rev: revisionNumber});
var revisionInfo = getRevisionInfo({web: "Web", topic: "Topic", rev: revisionNumber});
</verbatim>

Retrieves the revision information.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

| *Return Value* | *Description* |
| revisionInfo.rev      | Revision number, such as 1, 2, 3, ... |
| revisionInfo.time     | Unix timestamp |
| revisionInfo.user     | User name stored in the revision management system |
| revisionInfo.userName | Converted user name, retrieved by [[#getUserName][getUserName()]] |

%DATE_TIME_VALUES%

---++ getRevisionAtTime
<verbatim>
var revisionNumber = getRevisionAtTime("[Web.]Topic", unixTimestamp);
var revisionNumber = getRevisionAtTime({web: "Web", topic: "Topic", time: unixTimestamp});
</verbatim>

Retrieves the revision number at the specified Unix timestamp.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

%DATE_TIME_VALUES%

---++ getEditLock
<verbatim>
var editLock = getEditLock("[Web.]Topic");
var editLock = getEditLock({web: "Web", topic: "Topic"});
</verbatim>

Retrieves the edit lock (a.k.a. lease file) of the topic.
Returns =undefined= if there are no lease files.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

| *Return Value* | *Description* |
| editLock.user     | The user name stored in the lease file |
| editLock.userName | Converted user name, retrieved by [[#getUserName][getUserName()]] |
| editLock.conflict | A string indicating a lock conflict ("lease_active" or "lease_old"); or an empty string if no conflicts |
| editLock.taken    | Unix timestamp at which the lock was taken |
| editLock.expires  | Unix timestamp at which the lock is to expire |

%DATE_TIME_VALUES%

---++ acquireEditLock
<verbatim>
acquireEditLock("[Web.]Topic");
acquireEditLock({web: "Web", topic: "Topic"});
</verbatim>

Acquires the edit lock (a.k.a. lease file) to prevent other users from editing the same topic.

An exception is thrown if there is a lease conflict with another user, if the topic does not exist or if the CHANGE permission is denied.

---++ releaseEditLock
<verbatim>
releaseEditLock("[Web.]Topic");
releaseEditLock({web: "Web", topic: "Topic"});
</verbatim>

Releases the edit lock acquired by the current user if any. If there is an edit lock acquired by another user, the lease file is left unchanged.

An exception is thrown if the topic does not exist.

---++ getAttachmentList
<verbatim>
var fileNames = getAttachmentList("[Web.]Topic");
var fileNames = getAttachmentList({web: "Web", topic: "Topic"});
</verbatim>

Returns an array of attachment names of the topic.

An exception is thrown if the topic does not exist or if the VIEW permission is denied.

---++ attachmentExists
<verbatim>
var exists = attachmentExists("[Web.]Topic", "FileName.txt");
var exists = attachmentExists({web: "Web", topic: "Topic", file: "FileName.txt"});
</verbatim>

Determines if the attachment exists.

---++ readAttachment
<verbatim>
var contentText = readAttachment("[Web.]Topic", "FileName.txt");
var contentText = readAttachment("[Web.]Topic", "FileName.txt", {rev: fileRevisionNumber});
var contentText = readAttachment({web: "Web", topic: "Topic", file: "FileName.txt", rev: fileRevisionNumber});
</verbatim>

Retrieves the attachment file content.

An exception is thrown if the attachment does not exist or if the VIEW permission is denied.

---++ saveAttachment
<verbatim>
saveAttachment("[Web.]Topic", "FileName.txt", "CataText");
saveAttachment({web: "Web", topic: "Topic", file: "FileName.txt", data: "CataText"});
</verbatim>

Saves the attachment file content. If the attachment does not exist, a new attachment is created.

An exceptiton is thrown if the topic does not exist or if the CHANGE permission is denied.

%SAVE_ACTION_POLICIES%

---++ moveAttachment
<verbatim>
moveAttachment("[FromWeb.]FromTopic", "FromFileName.txt", "[ToWeb.]ToTopic", "ToFileName.txt");
moveAttachment({web: "FromWeb", topic: "FromTopic", file: "FromFileName.txt", toWeb: "ToWeb", toTopic: "ToTopic", toFile: "ToFileName.txt"});
</verbatim>

Renames the attachment or moves it to another topic.

An exception is thrown if the attachment does not exist or if the CHANGE permission is denied.

%SAVE_ACTION_POLICIES%

---++ removeAttachment
<verbatim>
removeTopic("[Web.]Topic", "FileName.txt");
removeTopic({web: "Web", topic: "Topic", file: "FileName.txt", toWeb: "%TRASHWEB%", toTopic: "ToTopic", toFile: "ToFileName.txt"});
</verbatim>

Removes an attachment. The target attachment will be moved to the =TrashAttachment= topic in the trash web, rather than permanently deleted.

An exception is thrown if the attachment does not exist or if the CHANGE permission is denied.

%SAVE_ACTION_POLICIES%

---++ getMethod
<verbatim>
var method = getMethod(); // "GET" or "POST"
</verbatim>

Retrieves the current HTTP request method.

---++ isPost
<verbatim>
var condition = isPost();
</verbatim>

Determines if the current HTTP request method is POST.

---++ getParam
<verbatim>
var paramValue = getParam("ParamName");
var paramValue = getParam("ParamName", "DefaultValue");
var paramValue = getParam({name: "ParamName", default: "DefaultValue"});
</verbatim>

Retrieves the URL parameter value (GET) or form-data value (POST).

---++ setParam
<verbatim>
setParam("ParamName", "ParamValue");
setParam({name: "ParamName", value: "ParamValue"});
</verbatim>

Sets the URL parameter value (GET) or form-data value (POST) for later use in the session.
It affects the actual usage of =%<nop>URLPARAM{...}%= in the TWiki markup after EJS preprocessing is done.

---++ getUserName
<verbatim>
var userName = getUserName();
var userName = getUserName("someUserName");
var userName = getUserName({user: "someUserName"});
</verbatim>

Retrieves the current user name if there are no arguments.

If the argument is given, it is converted to the canonical or login user name.

---++ getWikiName
<verbatim>
var wikiName = getWikiName();
var wikiName = getWikiName("someUserName");
var wikiName = getWikiName({user: "someUserName"});
</verbatim>

Retrieves the current user's wiki name (such as =UserFoobar=) if there are no arguments.

If the argument is given, it is converted to the user's wiki name.

---++ getWikiUserName
<verbatim>
var wikiUserName = getWikiUserName();
var wikiUserName = getWikiUserName("someUserName");
var wikiUserName = getWikiUserName({user: "someUserName"});
</verbatim>

Retrieves the current user's wiki user name (such as =%USERSWEB%.UserFoobar=) if there are no arguments.

If the argument is given, it is converted to the user's wiki user name.

---++ getVariable
<verbatim>
var variableValue = getVariable("VARIABLE_NAME"[, {param: value, ...}]);
</verbatim>

Retrieves the value of the TWiki variable (=%<nop>VARIABLE%=).

Optionally, parameters can be specified as key/value pairs (=%<nop>VARIABLE{param="value"}%=).
The default parameter is passed as =_DEFAULT= key (=%<nop>VARIABLE{"The default value"}%=).

---++ setVariable
<verbatim>
setVariable("VARIABLE_NAME", "VariableValue");
</verbatim>

Sets the value of the TWiki variable (=%<nop>VARIABLE%=) for later use.
It affects the actual usage of =%<nop>VARIABLE%= in the TWiki markup after EJS preprocessing is done.

---++ expandVariables
<verbatim>
var expandedText = expandVariables("%FOO% %BAR% %BAZ{...}%");
</verbatim>

Expands all the occurrences of the TWiki variables (=%<nop>VARIABLE%=).

---++ expandAutoInc
<verbatim>
var newTopicName = expandAutoInc("TopicNameAUTOINC001");
</verbatim>

Replaces the =AUTOINC= pattern in a topic name with the next number to make a new (non-existing) topic name.

---++ formatText
<verbatim>
var formatedText = formatText("$foo $bar $baz", {foo: "Foo", bar: "Bar", baz: "Baz"});
</verbatim>

Converts the variable names in the =$variable= format, where the values are replaced by the given key/value pairs.
The standard escape variables (such as =$percnt= and =$dollar=) are also expanded.
Non-existing variable names are ignored, and the =$variable= will appear in the result string.

---++ isTrue
<verbatim>
var condition = isTrue("on/off");
var condition = isTrue("on/off", "default");
</verbatim>

Determines if the given text is a true value.
If it is either "off", "false" or "no", then the value is regarded as false.
If is it =undefined= or =null=, the default value is used instead.

---++ normalizeWebTopicName
<verbatim>
var nameInfo = normalizeWebTopicName('[Web.]Topic');
var nameInfo = normalizeWebTopicName('Web', 'Topic');
var nameInfo = normalizeWebTopicName({web: 'Web', topic: 'Topic'});
</verbatim>

Normalizes the web name and topic name.

| *Return Value* | *Description* |
| =nameInfo.web= | Normalized web name |
| =nameInfo.topic= | Normalized topic name |

---++ escapeHTML
<verbatim>
var escapedText = escapeHTML("< unescaped >");
</verbatim>

Converts special HTML characters (such as =&lt;= and =&gt;=) into HTML entity strings (such as =&amp;lt;= and =&amp;gt;=).

---++ unescapeHTML
<verbatim>
var unescapedText = unescapeHTML("&lt; escaped &gt;");
</verbatim>

Converts HTML entity strings (such as =&amp;lt;= and =&amp;gt;=) into the original HTML characters (such as =&lt;= and =&gt;=).

---++ escapeQuote
<verbatim>
var escapedText = escapeQuote("\"unescaped\" text\n");
</verbatim>

Escapes special characters so that the text can be embedded in the JSON or !JavaScript string notation.

---++ unescapeQuote
<verbatim>
var unescapedText = unescapeQuote("\\\"escaped\\\" text\\n");
</verbatim>

Unescapes special characters that are escaped by [[#escapeQuote][escapeQuote()]].

---++ escapeTable
<verbatim>
var escapedText = escapeTable("unescaped || text");
</verbatim>

Escapes special characters so that the text can be embedded in the !TWiki table notation.
Special HTML characters are also converted to HTML entity strings.

---++ unescapeTable
<verbatim>
var unescapedText = unescapeTable("escaped %VBAR%%VBAR% text");
</verbatim>

Unescapes special characters that are escaped by [[#escapeTable][escapeTable()]].
HTML entity strings are also converted to the original HTML characters.

---++ parseTable
<verbatim>
var table = parseTable("| *A* | *B* |\n" + "| 1 | 2 |\n" + "| 3 | 4 |\n");
var table = parseTable("| 1 | 2 |\n" + "| 3 | 4 |\n", {header: false});
var table = parseTable("| 1 | 2 |\n" + "| 3 | 4 |\n", {header: false, fields: ['Field1', 'Field2']});
var table = parseTable(readTopic("ManyTables"), {target: 2});
var table = parseTable({text: "| *A* | *B* |\n" + "| 1 | 2 |\n" + "| 3 | 4 |\n"});
var table = parseTable(..., function (row, loop) {});
var table = parseTable({text: "...", header: false, fields: [...], target: 2, callback: function (row, loop) {}});
</verbatim>

Parses a TWiki table notation.

| *Argument Parameter* | *Default Value* | *Description* |
| =header= | =true= | Indicates the parsed table has a header row. |
| =fields= | (none) | Specifies an array of field names. If there is already a header row in the parsed table, this parameter will override the field names used in the return value. |
| =target= | =1= | Select a table by number (starting from =1=) out of multiple tables in the input text. |
| =callback= | (none) | Callback function per row. Each =row= argument can be an object of field/value pairs or a simple array, depending on the context. |

The return value is an object containing =fields= and =rows= if the =fields= are detected from the input or specified as an argument.
If there are no available =fields=, the return value is simply an array of arrays.

| *Return Value* | *Description* |
| =table.fields= | Array of field names |
| =table.rows=   | Array of rows, each of which is an object of field/value pairs |

By default, the first row is assumed to be a header, and the values are detected as =fields=.
If the =header= parameter is set to =false=, the first row is not parsed as a header.

If the =fields= parameter is given, the fields used as the keys of each row object are overridden regardless of the first row.

If a =callback= function is given, each =row= can be altered by returning an updated row object, or excluded by returning nothing (=undefined= or =null=).

When the input text contains multiple tables, the first table is parsed by default. If the =target= parameter is given, the table at the specified number is detected (starting from =1=).

All the values are converted by [[#unescapeTable][unescapeTable()]].

See also [[EmbeddedJSPluginTableAPI#parseTable][Table API]] for example code.

---++ formatTable
<verbatim>
var formatedText = formatTable(table);
var formatedText = formatTable(table, {header: false});
var formatedText = formatTable(table, function (row, loop) {});
var formatedText = formatTable({fields: [...], rows: [...], header: false, callback: function (row, loop) {}});
</verbatim>

Builds a TWiki table notation from an object representing a table.

| *Argument Parameter* | *Default Value* | *Description* |
| =fields=   | (none) | Array of field names |
| =rows=     | (none) | Array of rows, each of which is an object of field/value pairs |
| =header=   | =true= | Indicates the header row is prepended to the result table. |
| =callback= | (none) | Callback function per row. Each =row= argument can be an object of field/value pairs or a simple array, depending on the context. |

The structure of the =table= parameter (first argument) roughly corresponds to the return value of [[#parseTable][parseTable()]], either an object with =fields= and =rows= or an array of arrays.

By default, the first row is prepended as a header based on the =fields= values.
If the =header= parameter is set to =false= or if there are no =fields=, the header row is not prepended.

If a =callback= function is given, each =row= can be altered by returning an updated row object, or excluded by returning nothing (=undefined= or =null=).

All the values are converted by [[#escapeTable][escapeTable()]].

Because of how this API works, it can also be used to generate a single row of a table:

<verbatim>
formatTable([['Foo', 'Bar', 'Baz']]);
// returns "| Foo | Bar | Baz |"

formatTable({fields: ['Foo', 'Bar', 'Baz']});
// returns "| *Foo* | *Bar* | *Baz* |"
</verbatim>

See also [[EmbeddedJSPluginTableAPI#formatTable][Table API]] for example code.

---++ parseCSV
<verbatim>
var table = parseCSV("A,B,C\n1,2,3\n4,5,6");
var table = parseCSV("1,2,3\n4,5,6", {header: false});
var table = parseCSV("1,2,3\n4,5,6", {header: false, fields: ['Field1', 'Field2', 'Field3']});
var table = parseCSV({text: "A,B,C\n1,2,3\n4,5,6"});
var table = parseCSV(..., function (row, loop) {});
var table = parseCSV({text: "...", header: false, fields: [...], callback: function (row, loop) {}});
</verbatim>

Parses a CSV format. The input parameters and the return value are similar to [[#parseTable][parseTable()]].

See also [[EmbeddedJSPluginTableAPI#parseTable][Table API]] for example code.

---++ formatCSV
<verbatim>
var formatedText = formatCSV(table);
var formatedText = formatCSV(table, {header: false});
var formatedText = formatCSV(table, function (row, loop) {});
var formatedText = formatCSV({fields: [...], rows: [...], header: false, callback: function (row, loop) {}});
</verbatim>

Builds a CSV text from an object representing a table. The input object and the parameters are similar to [[#formatTable][formatTable()]].

See also [[EmbeddedJSPluginTableAPI#parseTable][Table API]] for example code.

---++ getExternalResource
<verbatim>
var response = getExternalResource('http://...');
var response = getExternalResource('http://...', {headers: [], method: ...});
var response = getExternalResource('http://...', {headers: {}, method: ...);
</verbatim>

Sends an HTTP GET request to the specified URL and receives the response.

| *Argument Parameter* | *Description* |
| =headers=            | HTTP request headers |
| =method=             | Overrides HTTP request method (Default: "GET"/"POST") |
| =timeout=            | Sets timeout in seconds |
| =maxRedirect=        | Limits the number of HTTP redirect |
| =insecureSSL=        | Indicates whether to ignore SSL hostname verification (Default: =false=) |

The request header format can be in either array or object. The header names are case-insensitive.

   * Array: =['Header-1', 'Value 1', 'Header-2', 'Value 2', ...]=
   * Object: ={'Header-1': 'Value 1', 'Header-2': 'Value 2', ...}=

The return value is an HTTP response object containing the following values:

| *Return Value*        | *Description* |
| =response.code=       | HTTP response code (e.g. 200) |
| =response.message=    | HTTP response status message (e.g. "OK") |
| =response.headers=    | HTTP response header object |
| =response.content=    | Response body content |
| =response.isSuccess=  | Indicates if the response is a success (2xx) |
| =response.isRedirect= | Indicates if the response is an HTTP redirect (3xx) |
| =response.isError=    | Indicates if the response is an error (4xx or greater) |

The response header object is the name/value pairs, where the case is normalized to how RFC describes (e.g. "Content-Type") and the value is either a single string or an array if there are two or more occurrences in the HTTP response.

---++ postExternalResource
<verbatim>
var response = postExternalResource('http://...', 'Request Body Content');
var response = postExternalResource('http://...', 'Request Body Content', {headers: [], method: ...});
var response = postExternalResource('http://...', 'Request Body Content', {headers: [], method: ...});
</verbatim>

Sends an HTTP POST request with a request body content to the specified URL and receives the response.

The argument values and the return value are similar to [[#getExternalResource][getExternalResource()]], except that the request method defaults to "POST".

---++ captureOutput
<verbatim>
var captured = captureOutput(function () {
    print("Captured Text");
});
var captured = captureOutput(callbackWithArgs, arg1, arg2, ...);
</verbatim>

Stores all the outputs in the callback function as a result of [[#print][print()]], instead of actually printing the texts.

Optionally, if additional arguments are given, the callback function will be invoked with those arguments.

This is useful when suppressing the output of a function that would otherwise print something. It can also be used to modify the output before actually printing it.

Since texts outside of the EJS tags (=<%...%>=) are also printed, inline texts can be captured as a string as below:

<verbatim>
<%
var text = captureOutput(function () {
%>
| *Foo* | *Bar* |
| 100 | 200 |
<%
});
var table = parseTable(text);
%>
</verbatim>

It is also possible to nest the call within another callback.

<verbatim>
var outer = captureOutput(function () {
    print("Foo");
    var inner = captureOutput(function () {
        print("Bar");
    });
    print("Baz");
});
// outer == "FooBaz"
// inner == "Bar"
</verbatim>

---++ getJavaScriptEngine
<verbatim>
var engineName = getJavaScriptEngine();
</verbatim>

Retrieves the underlying !JavaScript engine name, such as =JE= and =JavaScript::V8=.

---++ getEJSConfig
<verbatim>
var config = getEJSConfig();
</verbatim>

Retrieves the [[EmbeddedJSPlugin]] configuration under the current execution context.

| *Return Value*             | *Description* |
| =config.namespace=         | Namespace for EmbeddedJSPlugin API functions. Default: =undefined= |
| =config.postMethodPolicy=  | POST method policy |
| =config.sameWebPolicy=     | Same-web policy |
| =config.timeout=           | Timeout in seconds |
| =config.defaultBaseWeb=    | Default template web name used for [[#createWeb][createWeb()]] API  |

The context may be affected by the system configurations (=$TWiki::cfg=), the preference variables (such as =EJS_NAMESPACE=), and the parameters passed to =%<nop>EJS_INCLUDE{...}%=.

---++ executeEJS
<verbatim>
executeEJS('Text containing EJS tags');
executeEJS('Text containing EJS tags', {var1: "Value 1", var2: "Value 2", ...});
</verbatim>

Executes the given text as EJS in the inline context just like the !JavaScript =eval()= function.

Additional variables can be passed via the second argument.

---++ console
<verbatim>
console.log("message");
console.debug("message");
console.info("message");
console.warn("message");
console.error("message");
</verbatim>

These methods are equivalent to invoking the following code, where the outputs are inserted into the browser-side console.

<verbatim>
<script>console.log(...);</script>
</verbatim>

The argument is serialized by =JSON.stringify()=, so non-string objects can also be examined in the console.

---++ JSON
<verbatim>
var object = JSON.parse("{\"key\": \"value\"}");
var jsonText = JSON.stringify({key: "value"});
</verbatim>

Converts a JSON string into a !JavaScript value, and vice versa.
