Further Ideas
The code for this command line is very shortonly about 100 lines. This is because it's pretty bare bones. Here are some ideas for making this code more useful.
Unix-Style Options
This command line syntax lets you use a sequence of words, such as "g i", to implement a specific command. Expressing this as "g -i" would be more Unix-like, so this might be a useful modification. You can implement this in a couple of ways:
- Strip the dashes from the options and call the g_i() function.
- Have a single function, g(), that is responsible for interpreting the options.
Function Disambiguation
I named the 'alert' command al(). If you wanted the command to be the full word 'alert', then you would have to implement a handler called alert(), but this isn't allowed. JavaScript already has a function called alert(), which you shouldn't try to redefine.
One solution to this problem is to use a prefix for handler names: bcl_g(), bcl_g_i(), and so on. This could be made mandatory, or it could be optional. If it's optional, the code would have to check for the existence of bcl_g(). If it didn't exist, the code could then check for g().
Security
You can use the browser command line to call any JavaScript function, not just your specially defined handlers. A handler is just a regular function, after all. Thus, you can run the command alert hello, which will show an alert window displaying the word "hello." However, putting a script like this in a place where anyone can access it is asking for trouble.
The solution to the disambiguation problem is also a solution to this security issue: require handlers to start with "bcl_", and specify that only handlers can be called. Another solution is to enclose all the handler functions inside an array or object, and allow the system to look for handlers only in that array/object. Neither of these solutions are perfect, however, because browser bugs and other vulnerabilities may allow a hacker to create JavaScript code to define functions starting with "bcl_", or to modify the object containing your handlers.
Commands in Separate Files
If you like using the browser command line, you will probably accumulate many useful command handlers. If you keep all of them in the same file with the command line code, it could get messy. Keeping the handlers in separate files would be useful, perhaps organized by functionality. Loading JavaScript code can be tricky, though, because of asynchronous loading issues. So you may want to consider using AJAX techniques or even an AJAX framework for loading code.
Better Parsing
The parser is very simple, separating the command into words by breaking it at whitespace. But consider the following command:
% al "hello world!"
This doesn't work right. It should display an alert box with the words "hello world!", but in fact it displays only the world "hello" (see Figure 4). The parser breaks the command up into the following words:
al
"hello
world!"
Figure 4. Parser Breaks Up the "hello world!" Command |
Although both arguments are passed to al(), the al() function recognizes only the first argument, so that's what it displays.
This problem can be solved using a more sophisticated parserone that recognizes quotation marks. But this in turn requires a syntax for escaping quotation marks. A full command line parser has many elementsfeel free to add what you need.
A Scripting Language for a Scripting Language
Command lines are useful for a number of reasons. It's much easier to run new code by calling it from a handler than it is to create a GUI to call it. A command line is a great way to try out new code before you go to the trouble of implementing a real interface. Command lines are also useful for impatient people, who don't like to use the mouse any more than necessary.
This command line implementation is also pedagogically useful: it demonstrates some basic DHTML/JavaScript techniques, as well as a JavaScript program's ability to inspect its own code and call functions dynamically.
The browser command line is, in a sense, a scripting language for a scripting language.