jq控制台是jQuery的一个简单但功能丰富且高度可定制的终端模拟器,它允许您在浏览器中模拟类似linux/unix的环境。
该插件的目标是创建一个web终端模拟器,让人感觉尽可能接近真实的物理设备。
1.开始在文档中加载jq控制台插件的文件。
- <script src="/path/to/cdn/jquery.min.js"></script>
- <script src="/path/to/lib/jqconsole.js"></script>
2.创建一个空的DIV来容纳web终端。
- <div id="console"></div>
3.初始化插件。可用参数:
- window.jqconsole = $('#console').jqconsole(welcomeString, promptLabel, continueLabel, disableAutoFocus);
4.将您自己的风格应用于网络终端。
- #console {
- height: 400px;
- width: 750px;
- position:relative;
- background-color: black;
- border: 2px solid #CCC;
- margin: 0 auto;
- margin-top: 50px;
- }
- .jqconsole {
- padding: 10px;
- padding-bottom: 10px;
- }
- .jqconsole-cursor {
- background-color: #999;
- }
- .jqconsole-blurred .jqconsole-cursor {
- background-color: #666;
- }
- .jqconsole-prompt {
- color: #0d0;
- }
- .jqconsole-old-prompt {
- color: #0b0;
- font-weight: normal;
- }
- .jqconsole-input {
- color: #dd0;
- }
- .jqconsole-old-input {
- color: #bb0;
- font-weight: normal;
- }
- .jqconsole-composition {
- background-color: red;
- }
- .jqconsole-prompt-text {
- color: red;
- }
5.注册新的快捷方式。所有可用的内置快捷方式:
- jqconsole.RegisterShortcut('KEYCODE OR The ASCII code of the first character', function() {
- // do something
- });
6.启动命令提示符操作。如果当前正在进行另一个输入或提示操作,则新的提示操作将被排队,并将在当前操作和所有先前排队的操作完成时调用。
- jqconsole.Prompt(history_enabled, result_callback, multiline_callback, async_multiline);
7.启动输入操作。如果当前正在进行另一个输入或提示操作,则新的输入操作将被排队,并将在当前操作和所有先前排队的操作完成时调用。
- jqconsole.Input(function(input) {
- // ...
- });
8.更多API方法。
- // Writes the given text to the console in a span, with an optional class.
- jqconsole.Write(text, class, escape=true);
- // Sets the number of spaces inserted when indenting.
- jqconsole.SetIndentWidth(width);
- // Returns the number of spaces inserted when indenting.
- jqconsole.GetIndentWidth();
- // Registers character matching settings for a single matching
- // open: the openning character
- // close: the closing character
- // class: the html class to add to the matched characters
- jqconsole.RegisterMatching(open, close, class);
- // Unregisters a character matching.
- jqconsole.UnRegisterMatching(open, close);
- // Adds a dom node, where any text would have been inserted
- jqconsole.Append(node);
- // Aborts the current prompt operation and returns to output mode or the next queued input/prompt operation.
- jqconsole.AbortPrompt();
- // Sets the contents of the prompt.
- jqconsole.SetPromptText('text');
- // Clears the contents of the prompt.
- jqconsole.ClearPromptText(clear_label);
- // Returns the contents of the prompt.
- jqconsole.GetPromptText(full);
- // Replaces the main prompt label.
- jqconsole.SetPromptLabel(main_label, continue_label);
- // Updates the main prompt label.
- jqconsole.UpdatePromptLabel();
- // Handles key presses and potentially override internal keypress handler.
- jqconsole.SetKeyPressHandler(handler);
- // Handles and potentially override control keys (tab, up, down).
- jqconsole.SetControlKeyHandler(handler);
- // Resets the shortcut configuration.
- jqconsole.ResetShortcuts();
- // Sets the history
- // history: The history buffer to use.
- // e.g. ['a = 3', 'a + 3']
- jqconsole.SetHistory(history);
- // Gets the current history
- jqconsole.GetHistory(history);
- // Resets the history into intitial state.
- jqconsole.ResetHistory();
- // Moves the cursor to the start of the current prompt line.
- // all_lines: If true, moves to the beginning of the first prompt line, instead of the beginning of the current.
- jqconsole.MoveToStart(all_lines);
- // Moves the cursor to the end of the current prompt line.
- jqconsole.MoveToEnd(all_lines);
- // Returns the 0-based number of the column on which the cursor currently is.
- jqconsole.GetColumn();
- // Returns the 0-based number of the line on which the cursor currently is.
- jqconsole.GetLine();
- // Gets the current prompt state: input, output, or prompt
- jqconsole.GetLine();
- // Sets focus on the console's hidden input box so input can be read.
- jqconsole.Focus();
- // Enables focus and input on the console.
- jqconsole.Enable();
- // Disables focus and input on the console.
- jqconsole.Disable();
- // Returns true if the console is disabled.
- jqconsole.IsDisabled();
- // Dumps the content of the console before the current prompt.
- jqconsole.Dump();
- // Resets the console to its initial state.
- jqconsole.Reset();
- // Clear the console keeping only the prompt.
- jqconsole.Clear();