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

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

简介

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

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

特征:

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

参见:

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

如何使用它:

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.初始化插件。可用参数:

  • 欢迎字符串:欢迎字符串。
  • 提示标签:输入前提示标签
  • continue标签:输入的连续行之前的标签
  • 禁用自动对焦:是否禁用自动对焦
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.注册新的快捷方式。所有可用的内置快捷方式:

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

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

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

预览截图