浏览器中的Bash shell模拟

时间:2014-04-22 07:33:26

标签: javascript jquery

有没有办法在浏览器中使用Javascript / JQuery 模拟一个Bash shell?我想在我们的网站上有一个演示,它使用我们的新数据库系统从Bash shell模拟某人。最好看起来有人在shell命令中输入,输出会像典型的Bash shell一样逐行列出。我一直没有找到任何东西,所以我应该如何实现它 - 使用什么JQuery插件来使我的工作更轻松。

3 个答案:

答案 0 :(得分:2)

看看Asciinema

  

Asciinema是一种免费的开源解决方案,用于记录终端会话并在网络上分享。

答案 1 :(得分:1)

如果它仅用于演示目的,那么从头开始编码就不那么困难了。设置页面使其看起来像命令行界面

HTML:

<div id='window'></div>
<div id='command-line'>
    [user@computer folder]$ <input type='text' id='command-input/>'
</div>

CSS:

body{
    background-color:black;
    color:green;
}
#window{
    position:absolute;
    top:0;
    left:0;
    width:100%:
    height:95%;
}
#command-line{
    position:absolute;
    top:95%;
    left:0;
    width:100%;
    height:5%;
}

然后在JavaScript / jQuery中实现虚假操作,这些操作将在客户端加载页面时启动。例如

function appendOutput(message){
    var window = $('#window');
    window.append("<div>"+message+"</div>");
}    

这似乎是来自服务器的响应,或者例如

function typeMessage(message){
    var input = $('#command-input');
    for(var i=0; i<message.length; i++){
        setTimeout(function(){
            var formerValue = input.val();
            input.val(formerValue + message[i]);
        });
    }
}

输入命令等。我确定你可以玩这个以使它看起来更好,而且功能还没有经过测试 - 它只是为了帮助你获得一个可能的解决方案的想法。希望它有所帮助!

答案 2 :(得分:1)

我根据example made by Kos制作了解决方案,您可以看到完整正常的演示here

<强> HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/q.js/1.0.0/q.js"></script>
<div id="wnd"></div>
<div id="log"></div>

<textarea rows="11" cols="50">
% cat example.c
#include <stdio.h>
int main()
{
  printf("Goodbye Cruel World!\n");
  return 0;
}
% make example.c -o example
% ./example
Goodbye Cruel World!
</textarea>

<强> CSS

body {
  background: black;
}
#wnd {
  background: #232;
  border-radius: .2em;
  white-space: pre-wrap;
  padding: .5em;
  font-family: "Consolas", "Ubuntu Mono", "Monaco", monospace;
  color: white;
}
.prompt {
  color: #99aaee;
}
.cmd {
  color: #9e9e9C;
}

<强> JQuery的

var prompt;

function setPrompt(usr, domain)
{
  prompt = usr + '@' + domain + ' %';
}

function addOutput(s) {
  $('<div>').text(s).appendTo(wnd);
  return Q.delay(100);
//  return addPrompt();
}

function addInput(s) {
  var l = $('.prompt:last');
var e = $('<span>').addClass('cmd').appendTo(l);

  return addLettersRecursive(e, s);
}

function addPrompt() {
  var l = $('<div>').text(prompt).addClass('prompt').appendTo(wnd);
  return Q.delay(900);
}

function addLettersRecursive(container, s) {
  container.append(s.charAt(0)); // dangerous :(
  var row_complete = Q.defer();
  Q.delay(100).then(function() {
    if (s.length <= 1) {
      row_complete.resolve();
    }
    addLettersRecursive(container, s.substr(1)).then(function() {
      row_complete.resolve();
    })
  });
  return row_complete.promise;
}

$( document ).ready(function() {
  $('textarea').hide();

  setPrompt('inge', 'sparkledb.net');

  var lines = $('textarea').val().split('\n');

  var promise = new Q();

  promise = promise.then(function() 
  {  
    lines.forEach( function(item) {
      if (item[0] == '%')
      {
        promise = promise.then(function() 
        { return addPrompt(); })
        promise = promise.then(function() 
        { return addInput(item.substr(1)); })
      }
      else
      {
        promise = promise.then(function() 
        { return addOutput(item); })
      }
    })
  })
  promise.done();

});
相关问题