功能丰富 终端仿真插件 jq控制台

  • 源码大小:201.51KB
  • 所需积分:1积分
  • 源码编号:19JP-3439
  • 浏览次数:548次
  • 最后更新:2023年06月13日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

jq控制台是jQuery的一个简单但功能丰富且高度可定制的终端模拟器,它允许您在浏览器中模拟类似linux/unix的环境。

该插件的目标是创建一个web终端模拟器,让人感觉尽可能接近真实的物理设备。

特征:

  • 自定义样式。
  • 键盘快捷键。
  • 命令提醒。
  • 控制台历史记录。
  • 支持ANSI转义码和SGR(选择图形格式副本)

参见:

  • JavaScript和jQuery中的10个最佳终端模拟器

如何使用它:

1.开始在文档中加载jq控制台插件的文件。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/lib/jqconsole.js"></script>

2.创建一个空的DIV来容纳web终端。

  1. <div id="console"></div>

3.初始化插件。可用参数:

  • 欢迎字符串:欢迎字符串。
  • 提示标签:输入前提示标签
  • continue标签:输入的连续行之前的标签
  • 禁用自动对焦:是否禁用自动对焦
  1. window.jqconsole = $('#console').jqconsole(welcomeString, promptLabel, continueLabel, disableAutoFocus);

4.将您自己的风格应用于网络终端。

  1. #console {
  2. height: 400px;
  3. width: 750px;
  4. position:relative;
  5. background-color: black;
  6. border: 2px solid #CCC;
  7. margin: 0 auto;
  8. margin-top: 50px;
  9. }
  10.  
  11. .jqconsole {
  12. padding: 10px;
  13. padding-bottom: 10px;
  14. }
  15.  
  16. .jqconsole-cursor {
  17. background-color: #999;
  18. }
  19.  
  20. .jqconsole-blurred .jqconsole-cursor {
  21. background-color: #666;
  22. }
  23.  
  24. .jqconsole-prompt {
  25. color: #0d0;
  26. }
  27.  
  28. .jqconsole-old-prompt {
  29. color: #0b0;
  30. font-weight: normal;
  31. }
  32.  
  33. .jqconsole-input {
  34. color: #dd0;
  35. }
  36.  
  37. .jqconsole-old-input {
  38. color: #bb0;
  39. font-weight: normal;
  40. }
  41.  
  42. .jqconsole-composition {
  43. background-color: red;
  44. }
  45.  
  46. .jqconsole-prompt-text {
  47. color: red;
  48. }

5.注册新的快捷方式。所有可用的内置快捷方式:

  • 删去:删除以下字符。
  • Ctrl+删除:删除以下单词。
  • 后退(Backspace):删除前面的字符。
  • Ctrl+Backspace组合键:删除前面的单词。
  • Ctrl+向左:向左移动一个单词。
  • Ctrl+右键:向右移动一个单词。
  • 家庭:移动到当前行的开头。
  • Ctrl+主页:移到第一行的开头。
  • 终止:移动到当前行的末尾。
  • Ctrl+结束:移动到最后一行的末尾。
  • Shift+向上,Ctrl+向上:将光标移动到当前光标上方的行。
  • Shift+向下,Ctrl+向下:将光标移动到当前光标下方的行。
  • 选项卡:缩进。
  • Shift+Tab键:未拒绝。
  • Up:上一个历史记录项目。
  • 向下:下一个历史项目。
  • 进来:完成输入/提示操作。有关详细信息,请参见Input()和Prompt()。
  • Shift+Enter键:新行。
  • 向上翻页:将控制台向上滚动一页。
  • 向下翻页:向下滚动控制台一页。
  1. jqconsole.RegisterShortcut('KEYCODE OR The ASCII code of the first character', function() {
  2. // do something
  3. });

6.启动命令提示符操作。如果当前正在进行另一个输入或提示操作,则新的提示操作将被排队,并将在当前操作和所有先前排队的操作完成时调用。

  • 已启用历史记录:此输入是否应使用历史记录。如果为true,则用户可以从历史记录中选择输入,并且他们的输入也将被添加为新的历史记录项。
  • 结果回调(_C):当用户按下Enter键并完成提示操作时,使用用户输入调用的函数。
  • 多重链接回调:如果指定,当用户按下Enter键检查输入是否应继续到下一行时,会调用此函数
  • 异步多行(_M):是否应将多行回调函数视为异步操作,并向其传递应使用其中一个返回值调用的延续函数。
  1. jqconsole.Prompt(history_enabled, result_callback, multiline_callback, async_multiline);

7.启动输入操作。如果当前正在进行另一个输入或提示操作,则新的输入操作将被排队,并将在当前操作和所有先前排队的操作完成时调用。

  1. jqconsole.Input(function(input) {
  2. // ...
  3. });

8.更多API方法。

  1. // Writes the given text to the console in a span, with an optional class.
  2. jqconsole.Write(text, class, escape=true);
  3.  
  4. // Sets the number of spaces inserted when indenting.
  5. jqconsole.SetIndentWidth(width);
  6.  
  7. // Returns the number of spaces inserted when indenting.
  8. jqconsole.GetIndentWidth();
  9.  
  10. // Registers character matching settings for a single matching
  11. // open: the openning character
  12. // close: the closing character
  13. // class: the html class to add to the matched characters
  14. jqconsole.RegisterMatching(open, close, class);
  15.  
  16. // Unregisters a character matching.
  17. jqconsole.UnRegisterMatching(open, close);
  18.  
  19. // Adds a dom node, where any text would have been inserted
  20. jqconsole.Append(node);
  21.  
  22. // Aborts the current prompt operation and returns to output mode or the next queued input/prompt operation.
  23. jqconsole.AbortPrompt();
  24.  
  25. // Sets the contents of the prompt.
  26. jqconsole.SetPromptText('text');
  27.  
  28. // Clears the contents of the prompt.
  29. jqconsole.ClearPromptText(clear_label);
  30.  
  31. // Returns the contents of the prompt.
  32. jqconsole.GetPromptText(full);
  33.  
  34. // Replaces the main prompt label.
  35. jqconsole.SetPromptLabel(main_label, continue_label);
  36.  
  37. // Updates the main prompt label.
  38. jqconsole.UpdatePromptLabel();
  39.  
  40. // Handles key presses and potentially override internal keypress handler.
  41. jqconsole.SetKeyPressHandler(handler);
  42.  
  43. // Handles and potentially override control keys (tab, up, down).
  44. jqconsole.SetControlKeyHandler(handler);
  45.  
  46. // Resets the shortcut configuration.
  47. jqconsole.ResetShortcuts();
  48.  
  49. // Sets the history
  50. // history: The history buffer to use.
  51. // e.g. ['a = 3', 'a + 3']
  52. jqconsole.SetHistory(history);
  53.  
  54. // Gets the current history
  55. jqconsole.GetHistory(history);
  56.  
  57. // Resets the history into intitial state.
  58. jqconsole.ResetHistory();
  59.  
  60. // Moves the cursor to the start of the current prompt line.
  61. // all_lines: If true, moves to the beginning of the first prompt line, instead of the beginning of the current.
  62. jqconsole.MoveToStart(all_lines);
  63.  
  64. // Moves the cursor to the end of the current prompt line.
  65. jqconsole.MoveToEnd(all_lines);
  66.  
  67. // Returns the 0-based number of the column on which the cursor currently is.
  68. jqconsole.GetColumn();
  69.  
  70. // Returns the 0-based number of the line on which the cursor currently is.
  71. jqconsole.GetLine();
  72.  
  73. // Gets the current prompt state: input, output, or prompt
  74. jqconsole.GetLine();
  75.  
  76. // Sets focus on the console's hidden input box so input can be read.
  77. jqconsole.Focus();
  78.  
  79. // Enables focus and input on the console.
  80. jqconsole.Enable();
  81.  
  82. // Disables focus and input on the console.
  83. jqconsole.Disable();
  84.  
  85. // Returns true if the console is disabled.
  86. jqconsole.IsDisabled();
  87.  
  88. // Dumps the content of the console before the current prompt.
  89. jqconsole.Dump();
  90.  
  91. // Resets the console to its initial state.
  92. jqconsole.Reset();
  93.  
  94. // Clear the console keeping only the prompt.
  95. jqconsole.Clear();

预览截图