Both sides previous revisionPrevious revisionNext revision | Previous revision |
custom:javascript [2018/12/05 01:37] – Added a whole section/table on text output methods, because there are so many digital man | custom:javascript [2023/08/04 19:24] (current) – [Input] A little more clarity on console.getbyte() digital man |
---|
| |
You can learn about the core JavaScript language and object model from the following documents: | You can learn about the core JavaScript language and object model from the following documents: |
* [[http://developer.mozilla.org/en/JavaScript/Guide|Core JavaScript Guide]] ([[http://developer.mozilla.org/docs/Core_JavaScript_1.5_Guide|Historic: 1.5]]) | * [[https://developer.mozilla.org/en-US/docs/Web/JavaScript|Core JavaScript Guide]] ([[http://developer.mozilla.org/docs/Core_JavaScript_1.5_Guide|Historic: 1.5]]) |
* [[http://developer.mozilla.org/en/JavaScript/Reference|Core JavaScript Reference]] ([[http://developer.mozilla.org/docs/Core_JavaScript_1.5_Reference|Historic: 1.5]]) | * [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference|Core JavaScript Reference]] ([[http://developer.mozilla.org/docs/Core_JavaScript_1.5_Reference|Historic: 1.5]]) |
| |
JavaScript is an established, mature scripting language syntactically similar to C++ and Java. | JavaScript is an established, mature scripting language syntactically similar to C++ and Java. |
[[https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_Overview#JavaScript_and_Java|JavaScript is not Java]]. | [[https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_Overview#JavaScript_and_Java|JavaScript is not Java]]. |
| |
The ECMA and ISO standards organizations have standardized the core JavaScript language in [[http://developer.netscape.com/docs/javascript/e262-pdf.pdf|ECMA-262]] (ECMAScript) and ISO-16262. | The ECMA and ISO standards organizations have standardized the core JavaScript language in [[https://262.ecma-international.org/5.1/ECMA-262.pdf|ECMA-262]] (ECMAScript) and ISO-16262. |
| |
===== Baja and JavaScript ===== | ===== Baja and JavaScript ===== |
Modified stock ''.js'' files should be placed in your Synchronet ''[[dir:mods]]'' directory to prevent over-writing by future upgrades. | Modified stock ''.js'' files should be placed in your Synchronet ''[[dir:mods]]'' directory to prevent over-writing by future upgrades. |
| |
===== Integration ===== | ===== Invocation ===== |
| |
JavaScript files can be executed from: | JavaScript files can be executed / invoked from: |
* [[:server:terminal|Terminal Server]] | * [[:server:terminal|Terminal Server]] |
as a timed event, external program (door), login/logon/newuser module, | as a timed event, external program (door), login/logon/newuser module, via the ''EXEC'' sysop command |
basically anywhere a Baja module or executable can be launched | ((basically anywhere a Baja module or native or MS-DOS executable can be launched)) |
* [[:server:web|Web Server]] | * [[:server:web|Web Server]] |
dynamically generates HTML files, | dynamically generates HTTP responses (e.g. HTML/CSS content), |
see ''[[dir:web]]/root/*.ssjs'' | see ''[[dir:web]]/root/*.ssjs'' |
* [[:server:ftp|FTP Server]] | * [[:server:ftp|FTP Server]] |
inbound mail processors may be written in JavaScript, | inbound mail processors may be written in JavaScript, |
see ''[[dir:exec]]/[[module:mailproc_example]].js'' and ''[[dir:ctrl]]/[[config:mailproc.ini]]'' | see ''[[dir:exec]]/[[module:mailproc_example]].js'' and ''[[dir:ctrl]]/[[config:mailproc.ini]]'' |
* [[:util:jsexec|JSexec]] | * [[:util:JSexec]] |
some script files may be executed outside of Synchronet (e.g. as a CGI script or daemon) using jsexec, | some script files may be executed outside of Synchronet (e.g. as a CGI script or daemon) using [[util:JSexec]], |
examples: ''ircd.js'', ''newslink.js'' | examples: ''ircd.js'', ''newslink.js'' |
| |
From the Terminal Server, a JavaScript file is executed on a native command-line by placing a question mark (''?'') at the beginning of the command-line before the JavaScript file name (in [[util:scfg]]). It is not necessary to specify the ''.js'' portion of the file name on the command-line. For example, the command-line to execute the file ''[[dir:exec]]/newslink.js'' would be "''?newslink''" or "''?newslink.js''". | From the Terminal Server, a JavaScript file is executed on a native command-line by placing a question mark (''?'') or asterisk (''*'') at the beginning of the command-line before the JavaScript file name (in [[util:SCFG]]). It is not necessary to specify the ''.js'' portion of the file name on the command-line. For example, the command-line to execute the file ''[[dir:exec]]/newslink.js'' would be "''?newslink''" or "''*newslink''". |
| |
From within a Baja module, a JavaScript file may be executed using the following Baja code: | From within a Baja module, a JavaScript file may be executed using the following Baja code: |
<code baja> | <code baja> |
exec "?modname" # where modname is the JavaScript file. | exec "?modname" # where modname is the JavaScript filename or base filename. |
| </code> |
| or: |
| <code baja> |
| exec "*modname" # where modname is the JavaScript filename or base filename. |
| </code> |
| or: |
| <code baja> |
| exec_bin "modname" # where modname is the JavaScript filename or base filename. |
</code> | </code> |
| |
===== Object Model ===== | ===== Object Model ===== |
| |
The Synchronet JavaScript object model includes a global method: ''load()'' (a closely-related ''require()'' method was added in v3.17). | The Synchronet JavaScript object model includes a global method: ''load()'' (a closely-related ''require()'' method was added in v3.17). |
| |
The ''load'' method is used to compile and execute an external script from within a //parent// script. It is most often used to load numeric constants and object definitions from files in the ''[[dir:exec]]/load'' directory as these definitions (e.g. ''sbbsdefs.js'') and object libraries (e.g. ''graphic.js'') enable code re-use and sharing among modules, saving us JS programmers and lot of typing and redundancy. | The ''load'' method is used to compile and execute an external script from within a //parent// script. It is most often used to load numeric constants and object definitions from files in the ''[[dir:exec]]/[[dir:load]]'' directory as these definitions (e.g. ''sbbsdefs.js'') and object libraries (e.g. ''graphic.js'') enable code re-use and sharing among modules, saving us JS programmers and lot of typing and redundancy. |
| |
There are 3 primary ways to use the ''load'' method: | There are 3 primary ways to use the ''load'' method: |
var queue = load(true, 'myscript.js', 1, 2, 3); | var queue = load(true, 'myscript.js', 1, 2, 3); |
var value = queue.read(1000); | var value = queue.read(1000); |
print(value); | writeln(value); |
</code> | </code> |
| |
lib.do_thing(); | lib.do_thing(); |
if(lib.status_good == true) | if(lib.status_good == true) |
print('good'); | writeln('good'); |
else | else |
alert('bad'); | alert('bad'); |
This usage expressly places all items created by the loaded script into the scope of the passed variable (in this case, a newly created empty //Object//, just for this purpose). This means there should be no naming conflicts between the parent script and the loaded script (e.g. they may have methods or variables defined with the **same name** and there will be [[https://www.youtube.com/watch?v=2CifYWxJXaI|no confrict]]). | This usage expressly places all items created by the loaded script into the scope of the passed variable (in this case, a newly created empty //Object//, just for this purpose). This means there should be no naming conflicts between the parent script and the loaded script (e.g. they may have methods or variables defined with the **same name** and there will be [[https://www.youtube.com/watch?v=2CifYWxJXaI|no confrict]]). |
| |
==== output ==== | ==== Output ==== |
There are many different text output methods supported by the Synchronet JavaScript Object Model and knowing which method (function) to use in what situation can be confusing. The following table should help: | There are many different text output methods supported by the Synchronet JavaScript Object Model and knowing which method (function) to use in what situation can be confusing. The following table should help: |
| |
^ Method ^ BBS((Terminal Server)) ^ JSexec ^ Prints newline ^ Expands/Decodes ^ Multiple Strings((as function arguments)) ^ Notes ^ | ^ Method ^ BBS((Terminal Server)) ^ JSexec ^ Newline ^ Xlat ((Character set translation)) ^ Expands/Decodes ^ Multiple Values((as function arguments)) ^ Notes ^ |
| ''write()'' | Yes | Yes | No | Telnet-IAC, Ctrl-A | Yes | | | | ''write()'' | Yes | Yes | No | Yes | Telnet-IAC, [[custom:ctrl-a_codes|Ctrl-A]] | Yes | If user not online, same as ''log(LOG_INFO, ...)'' | |
| ''write_raw()'' | Yes | No | No | Telnet-IAC | Yes | | | | ''write_raw()'' | Yes | No | No | No | Telnet-IAC | Yes | Value may contain NULs, no charset translation | |
| ''writeln()'' | Yes | Yes | Yes | Telnet-IAC, Ctrl-A | Yes | aka ''print()'' | | | ''writeln()'' | Yes | Yes | Yes | Yes | Telnet-IAC, Ctrl-A | Yes | aka ''print()'' | |
| ''printf()'' | Yes | Yes | No | Telnet-IAC, Ctrl-A | No | ala C ''printf()'' | | | ''printf()'' | Yes | Yes | No | Yes | Telnet-IAC, Ctrl-A | Yes((using C printf format syntax)) | ala C ''printf()'' | |
| ''alert()'' | Yes | Yes | Yes | Telnet-IAC, Ctrl-A | No | | | | ''alert()'' | Yes | Yes | Yes | Yes | Telnet-IAC, Ctrl-A | No | If user not online, same as ''log(LOG_WARNING, ...)'' | |
| ''log()'' | Yes | Yes | N/A | | Yes((following the log-level argument)) | Displayed/logged on the server only | | | ''log()'' | Yes | Yes | N/A | N/A | | Yes((following the log-level argument)) | Displayed/logged on the server only | |
| ''console.print()'' | Yes | No | No | Telnet-IAC, Ctrl-A | Yes | | | | ''console.print()'' | Yes | No | No | Yes | Telnet-IAC, Ctrl-A | Yes | | |
| ''console.write()'' | Yes | No | No | Telnet-IAC | Yes | | | | ''console.write()'' | Yes | No | No | Yes | Telnet-IAC | Yes | No line-counting/auto-pause | |
| ''console.writeln()'' | Yes | No | Yes | Telnet-IAC | Yes | | | | ''console.writeln()'' | Yes | No | Yes | Yes | Telnet-IAC | Yes | No line-counting/auto-pause | |
| ''console.putmsg()'' | Yes | No | No | Telnet-IAC, Ctrl-A, @-Codes | No | | | | ''console.putmsg()'' | Yes | No | No | Yes | Telnet-IAC, Ctrl-A, [[custom:atcodes|@-Codes]], [[custom:colors|Extra Attributes]] | No | Typically used for displaying text/menu files | |
| ''console.center()'' | Yes | No | Yes | Telnet-IAC, Ctrl-A | No | Text centered on screen | | | ''console.center()'' | Yes | No | Yes | Yes | Telnet-IAC, Ctrl-A | No | Text centered on screen | |
| ''console.mnemonics()'' | Yes | No | No | Telnet-IAC, Ctrl-A, @-code, ~ | No | Hotkey highlights | | | ''console.mnemonics()'' | Yes | No | No | Yes | Telnet-IAC, Ctrl-A, @-Codes, ~ | No | Hotkey highlights | |
| ''console.putbyte()'' | Yes | No | No | Telnet-IAC | No | No charset translation | | | ''console.putbyte()'' | Yes | No | No | No | Telnet-IAC | No | No charset translation | |
| ''client.socket.send()'' | Yes | No | No | | No | Avoid using | | | ''client.socket.send()'' | Yes | No | No | No | | No | Avoid using | |
| |
| |
| === Carriage Returns === |
| If you wish to send a traditional "carriage return" character to the client terminal (i.e. move the cursor to the far left column without advancing to the next line) it is recommended that you use an output method/function that supports [[custom:Ctrl-A codes]] and you send the Ctrl-A ''['' sequences //instead of// an ASCII 13 (''\r'') character. This is required for PETSCII terminal compatibility, where an ASCII 13 character performs a complete "newline" sequence (the equivalent of ''\r\n'') on the client terminal. |
| |
| ==== Input ==== |
| |
| ^ Method ^ BBS((Terminal Server)) ^ JSexec ^ Notes ^ |
| | ''read()'' | Yes | Yes | Read up to maxlen (default 128) characters of input | |
| | ''readln()'' | Yes | Yes | Read a string (line of input) from user, up to maxlen chars (default: 128) | |
| | ''console.inkey()'' | Yes | No | Wait for a key-press or an elapsed timeout duration | |
| | ''console.getstr()'' | Yes | No | Get a string of characters from user | |
| | ''console.getkey()'' | Yes | No | Get a single key-press | |
| | ''console.getbyte()'' | Yes | No | Get an unprocessed input byte from remote terminal as a number | |
| | ''console.getnum()'' | Yes | No | Get a number (between 1 and maxnum) from user | |
| | ''console.getkeys()'' | Yes | No | Get a key from a list of valid keys, or a number | |
| | ''console.gettemplate()'' | Yes | No | Get a string conforming to a provided input template | |
| |
| ==== Prompt ==== |
| |
| Often you'll want your script to prompt the user for some input (e.g. a string of text or a single key). A prompt involves both //output// (first, from the BBS) and //input// (from the user): |
| |
| ^ Method ^ BBS((Terminal Server)) ^ JSexec ^ Notes ^ |
| | ''prompt()'' | Yes | Yes | | |
| | ''confirm()'' | Yes | Yes | | |
| | ''deny()'' | Yes | Yes | | |
| | ''console.yesno()'' | Yes | No | | |
| | ''console.noyes()'' | Yes | No | | |
| | ''console.mnemonics()'' | Yes | No | | |
| |
===== See Also ===== | ===== See Also ===== |
* [[http://synchro.net/docs/jsobjs.html|Synchronet JavaScript Object Model]] | * [[http://synchro.net/docs/jsobjs.html|Synchronet JavaScript Object Model]] |
* [[:util:jsexec]] | * [[http://nix.synchro.net/jsobjs.html|Synchronet JavaScript Object Model (preliminary for v3.17a)]] |
* [[:util:baja]] | * [[:util:JSexec]] |
| * [[:util:Baja]] |
| * [[:custom:javascript:lib:|JavaScript Libraries]] |
* [[:custom:index|Customization]] | * [[:custom:index|Customization]] |
| |
{{tag>javascript}} | {{tag>javascript petscii}} |
| |