Sunday, January 27, 2013

Run Codes On the Webpage!

Fun little project where..
1. Copy your source code (Live shell script, Python, Html, and Perl) and paste them into the text box.
2.Click the run button corresponding to each language the code belongs to.
3. See the output below the buttons. (No page reload needed, using JQuery)
(I am not going to reveal the url for this webpage, because this opens lots and lots of security holes to my server..,., Just a proof of concept.)
 


It was fun.  Here is the JQuery that
1)gets the code in the text box,
2) sends to the Django web server,
3)Gets the result,
4)show it to the webpage without reloading


$('button').mouseenter(function(event) {
    var text = $('textarea').val();
    var language = event.target.id;

     $.get(
       "ec2-xxxx-xxxxx-xxxxx-xxxxx.compute-1.amazonaws.com/blahblahblah",
       {user: text,lng:language}, 
       function(data) {$('a#friend').html(data);}
             );
       }); 

Also you can deal with this age-old problem about how to insert the tab in the text box by: (Found by googling it)


$("textarea#1").keydown(function(e) {
    if(e.keyCode === 9) { // tab was pressed
        // get caret position/selection
        var start = this.selectionStart;
        var end = this.selectionEnd;

        var $this = $(this);
        var value = $this.val();

        // set textarea value to: text before caret + tab + text after caret
        $this.val(value.substring(0, start)
                    + "\t"
                    + value.substring(end));

        // put caret at right position again (add one for the tab)
        this.selectionStart = this.selectionEnd = start + 1;

        // prevent the focus lose
        e.preventDefault();
    }
});

No comments:

Post a Comment