Giter Site home page Giter Site logo

jcubic / jquery.terminal Goto Github PK

View Code? Open in Web Editor NEW
3.0K 106.0 567.0 32.98 MB

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands

Home Page: https://terminal.jcubic.pl

License: MIT License

CSS 4.59% JavaScript 92.82% Makefile 0.58% HTML 0.03% Shell 0.07% TypeScript 1.91%
json-rpc interpreter shell terminal command-line ansi-art jquery-terminal interpreters parse-commands javascript

jquery.terminal's Introduction

ASCII Art that represent text jQuery Terminal - JavaScript Library for Web Based Terminal Emulators

JavaScript Library for Web Based Terminal Emulators

npm bower Build and test Coverage Status NPM Downloads jsDelivr Downloads Paid Support LICENSE MIT

Summary

jQuery Terminal Emulator is a plugin for creating command line interpreters in your applications. It can automatically call JSON-RPC service when a user types commands or you can provide your own function in which you can parse user commands. It's ideal if you want to provide additional functionality for power users. It can also be used to debug your application.

You can use this JavaScript library to create a web based terminal on any website.

Because with this library you need to code all the commands yourself, you can call it fake terminal emulator. In contrast to library that will give you access to real terminal like online SSH. To have real online SSH I suggest to use xterm.js library.

Features:

  • You can create an interpreter for your JSON-RPC service with one line of code (just use url as first argument).

  • Support for authentication (you can provide functions when users enter login and password or if you use JSON-RPC it can automatically call login function on the server and pass token to all functions).

  • Stack of interpreters - you can create commands that trigger additional interpreters (eg. you can use couple of JSON-RPC service and run them when user type command)

  • Command Tree - you can use nested objects. Each command will invoke a function (own REPL), if the value is an object it will create a new interpreter and use the function from that object as commands. You can use as many nested object/commands as you like. If the value is a string it will create JSON-RPC service.

  • Support for command line history, it uses Local Storage if possible.

  • Support for tab completion.

  • Includes keyboard shortcut from bash like CTRL+A, CTRL+D, CTRL+E etc.

  • Bash reverse history search (CTRL+R / CTRL+G).

  • You can create and overwrite existing keyboard shortcuts.

  • Multiple terminals on one page (every terminal can have different commands, its own authentication function and its own command history).

  • It catches all exceptions and displays error messages in the terminal (you can see errors in your javascript and php code in terminal if they are in the interpreter function).

  • Using extended commands you can change working of the terminal without touching the front-end code (using echo method and terminal formatting like syntax). Read more in docs.

  • Easy way to change the style of the terminal (like color or cursor animation).

  • Chinese and Japanese character support.

  • You can use ASCII forms and collect information from users.

  • Animation (including typing effect and Canvas canvas adapter).

  • Support ANSI escapes codes.

  • Experimental mobile support, see open issues

Demo

You can test current version at this URL:

or if it doesn't use latest version (because of jsDelivr cache) you can force it with this URL:

And development version using:

You can use any version you want, everything what jsDelivr GH API accepts.

Installation

Include jQuery library, you can use cdn from https://jquery.com/download/

or use jsDelivr:

<script src="https://cdn.jsdelivr.net/npm/jquery"></script>

Then include js/jquery.terminal-2.40.3.min.js and css/jquery.terminal-2.40.3.min.css

You can grab the files from CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.40.3/js/jquery.terminal.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.40.3/css/jquery.terminal.min.css" rel="stylesheet"/>

or

<script src="https://cdn.jsdelivr.net/npm/[email protected]/js/jquery.terminal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/jquery.terminal.min.css"/>

If you always want latest version, you can get it from unpkg without specifying version, it will redirect to the latest ones:

<script src="https://unpkg.com/jquery.terminal/js/jquery.terminal.min.js"></script>
<link href="https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet"/>

or jsDelivr that is bit faster:

<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet"/>

Bleeding Edge Version

If you want to test bleeding edge, development version of jQuery Terminal. You can use those files:

<script src="https://cdn.jsdelivr.net/gh/jcubic/jquery.terminal@devel/js/jquery.terminal.min.js"></script>
<link href="https://cdn.jsdelivr.net/gh/jcubic/jquery.terminal@devel/css/jquery.terminal.min.css" rel="stylesheet"/>

but it's not refreshed as fast as rawgit was, because it's CDN and need to be propagated to different servers.

Keyboard key polyfill

NOTE: From version 1.0.0 if you want to support old browsers then you'll need to use key event property polyfill. You can check the support for it on can I use.

<script src="https://unpkg.com/js-polyfills/keyboard.js"></script>

or

<script src="https://cdn.jsdelivr.net/npm/js-polyfills/keyboard.js"></script>

Command Line

You can also install jQuery Terminal using command line, from bower repository:

bower install jquery.terminal

or npm registry:

npm install jquery.terminal

Example of usage

This is code that uses low level function, that gives you full control of the commands, just pass anything that the user types into a function.

jQuery(function($, undefined) {
    $('#term_demo').terminal(function(command) {
        if (command !== '') {
            var result = window.eval(command);
            if (result != undefined) {
                this.echo(String(result));
            }
        }
    }, {
        greetings: 'Javascript Interpreter',
        name: 'js_demo',
        height: 200,
        width: 450,
        prompt: 'js> '
    });
});

Here is a higher level call, using an object as an interpreter, By default the terminal will parse commands that a user types and replace number like strings with real numbers regex with regexes and process escape characters in double quoted strings.

$('body').terminal({
    cat: function(width = 200, height = 300) {
        return $(`<img src="https://placekitten.com/${width}/${height}">`);
    },
    title: function() {
        return fetch('https://terminal.jcubic.pl')
            .then(r => r.text())
            .then(html => html.match(/<title>([^>]+)<\/title>/)[1]);
    }
}, {
    checkArity: false,
    greetings: 'My Terminal\n'
});

And more advanced example:

jQuery(function($, undefined) {
    $('#term_demo').terminal({
        add: function(a, b) {
            this.echo(a + b);
        },
        re: function(re, str) {
           if (re instanceof RegExp && re.test(str)) {
              this.echo(str + ' [[;green;]match]');
           }
        },
        foo: 'foo.php',
        bar: {
            sub: function(a, b) {
                this.echo(a - b);
            }
        }
    }, {
        height: 200,
        width: 450,
        prompt: 'demo> '
    });
});

command add 2 2 will display 4 (not 22).

Command foo will change prompt to foo> and each new command will execute json-rpc method from foo.php script.

command bar will change the prompt to bar> and if you type sub 10 2 it will display 8. To exit from bar nested command you can type exit or press CTRL+D.

command re /^foo/ foo-bar will echo: "foo-bar match" where "match" will be green.

By default arguments are required but you can disable the check like this:

jQuery(function($, undefined) {
    $('#term_demo').terminal({
        add: function(...args) {
            this.echo(args.reduce((a,b) => a + b));
        }
    }, {
       checkArity: false
    });
});

And add command will accept any number of argments and it will sum them up (if they are numbers).

You can create JSON-RPC interpreter with authentication in just one line:

$('#term_demo').terminal('service.php', {login: true});

The rest of the code can be on the server, so you can write fully working application, without any front-end, that can be tested in browser.

First argument to terminal can also be array with objects strings and functions, with one requirement, that only one function can be used as last fallback for commands that was not found in RPC or in objects.

jQuery(function($, undefined) {
    $('#term_demo').terminal([{
        add: function(...args) {
            this.echo(args.reduce((a,b) => a + b));
        }
    } 'foo.php', function(command) {
       this.echo("You've typed " + command, {formatters: false, exec: false});
    }], {
       checkArity: false
    });
});

More examples here. You can also check Full Documentation or Getting Started Guide on Wiki.

Quick Start Tutorials

If you want to start with jQuery Terminal you can look at those tutorials:

Security

Because of security in version 1.20.0 links with protocols different than ftp or http(s) (it was possible to enter javascript protocol, that could lead to XSS if author of the app echo user input and save it in DB) was turn off by default. To enable it, you need to use anyLinks: true option.

In version 1.21.0 executing terminal methods using extendend commands [[ terminal::clear() ]] was also disabled by default because attacker (depending on your application) could execute terminal::echo with raw option to enter any html and execute any javascript. To enable this feature from this version you need to use invokeMethods: true option.

The features are safe to enable, if you don't save user input in DB and don't echo it back to different users (like with chat application). It's also safe if you escape formatting before you echo stuff.

If you don't save user input in DB but allow to echo back what user types and have enabled execHash options, you may have reflected XSS vulnerability if you enable this features. If you escape formatting this options are also safe.

NOTE: To disable exec if you have execHash (or echo stuff from users with invokeMethods: true), you can also set option {exec: false} to your echo call and use it only when you get values from server (not from DB indireclty from users). If you do this you will be able to echo stuff from users and execute terminal methods from server (this feature is mostly done just for that).

Contributors

If you want to contribute read CONTRIBUTING.md first. Here are project contributors:


Jakub T. Jankiewicz

commits

Jean-Michel Carrel

commits

kid1412z

commits

Marcel Link

commits

Sébastien Warin

commits

Christopher John Ryan

commits

Johan

commits

Snyk bot

commits

Florian Schäfer

commits

Riccardo Mura

commits

Tomasz Ducin

commits

Qijia Liu

commits

Ishan Ratnapala

commits

David Refoua

commits

Antoine

commits

youurayy

commits

Steve Kirkegard

commits

stereobooster

commits

Dev Kumar Gupta

commits

dependabot[bot]

commits

coderaiser

commits

Yutong Luo

commits

Steve Phillips

commits

Robert W

commits

exit1

commits

Mateusz Paprocki

commits

Martin v. Löwis

commits

KiddoV

commits

Jon Steinich

commits

John Jarvis

commits

Jarry Shaw

commits

jpaye

commits

Hraban

commits

Hasan

commits

finlob

commits

Ezinne Anne Emilia

commits

Anton Vasilev

commits

Abdelrahman Omran

commits

7twin

commits

jQuery Terminal Website contributors:


Jakub T. Jankiewicz

commits

Ezinne Anne Emilia

commits

Marc Laporte

commits

Rich Morin

commits

4s3ti

commits

DInesh51297

commits

Logan Rosen

commits

Acknowledge

Projects include with the source code:

Other code used inside the project or inspired by:

Personal thanks:

Also thanks to:

BrowserStack

for cross-device testing opportunity.

Paid Support

You can request paid support, you can find details at support.jcubic.pl.

License

Licensed under MIT license

Copyright (c) 2011-2023 Jakub T. Jankiewicz

jquery.terminal's People

Contributors

antoineol avatar chrisjohnryan avatar coderaiser avatar cowuake avatar dependabot[bot] avatar drsdavidsoft avatar ducin avatar eagleoflqj avatar elimisteve avatar exit1 avatar fschaefer avatar ishanratnapala avatar jarv avatar jcubic avatar johanjordaan avatar jsteinich avatar kid1412z avatar kiddov avatar loewis avatar mattpap avatar ml1nk avatar mrkaiser avatar neyxo avatar rbw avatar sebastienwarin avatar snyk-bot avatar stereobooster avatar stevekirks avatar youurayy avatar yutongluo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.terminal's Issues

Add more events

Will add more events like onFocus, onBlur, onPop and onPush onLogin, onLogout

Text formatting not working when text contain line breaks

If you create long line with formatting like

[[u;#f00;]This is long line This is long line This is long line]

and you resize terminal or browser (if you have full screen terminal) that this line is splited into two lines, then you lose formatting.

Parameters

Hello,
is it possible to get the parameters of a command or do I have to extract them manually?

Unable to edit last output line

I want to create a progress bar for the terminal, but the only way I could do this is to directly edit the last div in the terminal-output. But whenever the window is resized, I lose everything. There should be a way to clear just the last line, not just the entire console.

Key events in nested interepters

so you can push different keybindings like:

$('#node').terminal(function(cmd, term) {
   if (cmd == 'hex') {
      term.push($.noop, {
          name: 'hex',
          prompt: 'hex$ ',
          keydown: function(e) {
             if (e.which === 68 && e.ctrlKey) {
                 term.pop();
             } else {
                 term.append(e.which.toString(16));
             }
             return false;
          }
      });
   }
});

You can't type characters on android

When you enter a site with fullscreen terminal on android the keyboard is popup but you can't enter characters

Fixing this bug will probably fix issue on iPhone and iPad.

unable to input '@' symbol on german keywords

on german keyboards, the @ symbol is input using the following key combination:

Right Alt + Q

but it doesn't seem to be working on the terminal! any pointers on how / where i could fix that in the code?

Allow to add CSS classes to terminal output

echo result of each command can have class from that command. And formatting can have class option

[[biug;<COLOR>;<BACKGROUND>;<CLASS NAME>]asd]

;<CLASS NAME> can be opional, so other code will not break

Paste should scroll terminal to bottom

If you paste text larger the one line it's not scrolled to bottom of terminal, there will be need to put setTimeout to scroll after content is inserted into command line.

the char ( is not handle

Hello,

On my config azerty keyboard on ubuntu firefox, i can't type the char (. I have looked in the code and find that this char is suppressed on the line 1315 of the jquery.terminal.js

TryPython Dependants

Would you put the html file for the trypython in, as well as a readme file with the list of dependants it needs. I haven't been able to figure out how to get passed this Apache2 error: "ImportError: cannot import name handle_cgi", which is from the python json module.

Keydown event shoudn't prevent default browser bahavior for keys that are not use by terminal

Right now CTRL+R don't work (it don't reload the page) but it's not use by terminal.

All unused shortcuts should invoke default browser actions and also keydown event handler passed as argument should force terminal to invoke that action even if it call different action for it (like in CTRL+A) for those keys that should act as browser default handler can return true.

$(...).terminal(function(cmd){}, { keydown(e){ if (e.which == 82 && e.ctrlKey){ return true; }}});

Multiline Input

Admittedly this is a niche case, but it would be a nice configurable feature for the plugin to count the number of opening braces/parens/brackets and continue to accept input until the count is zero for each.

get_num_chars() is needed outside the constructor

It would be really useful if I could tell how wide my terminal is. This would let me escape having my lines broken automatically. Automatic line breaking does not consider words, so the result is ugly.

One option would be to add this line just before the return statement of get_num_chars:
self.width = result

Thanks for your great plugin.

Prompt y or n

How to create command that asks yes or no before executing?

API for setting command_list for tab-completion

When I supply an interpreter function to .terminal(), I found no way to provide a command list for tab-completion. An API to provide it would be nice to have. Also, API to update the tab-completion list at runtime. Something like this perhaps?

I can create a pull request if you think these changes look reasonable.

scrolling with jQuery 1.6

jQuery 1.6 introduced a new .prop() method, which should be used for extracting properties of DOM elements, so all the .attr("scrollHeight") calls now return undefined and scrolling simply doesn't work.

History should not be wrap

Simple, history should not use BCycle but list or simple array without abstraction. Bash command line history do not wrap around.

json-rpc server without login

Hello Jakub,
I'm trying to attach your terminal on a json-rpc server in the following way:

$(document).ready(function() {
    $('#console').css("background-color", "black").terminal("/rpc/json", {
        login: false
    });
});

It load correctly, but if I try to run a command it replies:

[USER]: undefined: Property 'token' of object #<Object> is not a function

I investigated a bit and I found this condition in line 2011:

if (!settings.login || method == 'help') {

But at this point the property settings.login has been overwritten by a login method set at line 2328:

settings.login = (function(method) {

So please, can you help me to understand how to reach my achievement?

Regards,
Valerio

Api function that return level of interpreters

So users will be able to know how deep they are and how many pop they'll need to execute to be on top level so they will be able to go to top level without accidentally exit interpreter (if they provide login function - true is not the case since rpc function have only one level).

Blank lines removed from output when term.echo() gets a longer string

Try this in the demo terminal at http://terminal.jcubic.pl/#demo

js> term.echo("aaaaaaaaaaaaaaaaaaa\n\nbbbbbbbbbbbbbbbbb\n\nccccccccccccccccccccccc\n\nd\n\ne")
aaaaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbb

ccccccccccccccccccccccc

d

e
[object Object]

and

js> term.echo("aaaaaaaaaaaaaaaaaaa\n\nbbbbbbbbbbbbbbbbb\n\nccccccccccccccccccccccc\n\ndd\n\ne")
ccccccccccccc\n\ndd\n\ne")
aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbb
ccccccccccccccccccccccc
dd
e
[object Object]

Suddenly all the the blanks disappear when the string echoed becomes one character longer?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.