How can I create an online JavaScript editor? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm learning JavaScript. However, it's not so convenient to do some experiments. You have to create a template HTML file and then embed your JavaScript code into it, and then use a browser to open the
HTML file, even you just want to see alert("hello world").
I wonder if it's easy to create an online JavaScript editor. The basic idea is: in a HTML text-area, you just type in JavaScript code directly, and it has one button, "Run", below to run the JavaScript code.
If it contains some syntax errors, maybe show the error message out. For example: someone already created one like this: http://www.codehouse.com/webmaster_tools/javascript_editor/
How can I create this? Are there any materials, blog, etc. on how to create this?

Consider http://jsbin.com. It's already equipped with various versions of the most prominent JavaScript frameworks for testing. Furthermore, it stores your code online and creates a short-URL so you can pass examples around, and quickly refer back to previous items of interest. You'll see many of us here using it frequently to help each other.
If you really want to work on your own, you can fork jsbin on GitHub: http://github.com/remy/jsbin

CodeMirror is the syntax highlighting 'engine' that jsbin and jsFiddle use.

Use Firebug.
You can open Firebug in any webpage and then type arbitrary JavaScript at the bottom of the Console tab. It will evaluate the JavaScript and display the result. It's even got basic autocomplete!
However, the real value of Firebug is its DOM tab.
You can look at any JavaScript object and see all of its methods and properties, nested as much as you want. You can inspect the browser's pre-defined objects (window, document, etc), any global variables defined on the page, and even objects you create in the console tab (by clicking an object that you got in the results list).
The DOM tab will show you all information you could possibly want about an object. You can even mouseover the word function in the right side and see the function's body in a tooltip. (And it will be auto-indented, too.)
Once you start writing your own pages, you can use Firebug's JavaScript debugger to help make them work correctly.

I think you can do something like this:
HTML
<textarea id="code"></textarea>
<iframe id="output"></iframe>
<button id="submit-b" onclick="update()">run</button>
JavaScript
function update()
{
var frame = $('#output').get(0);
var frameDoc = frame.contentDocument || frame.contentWindow.document;
var w = document.getElementById("code").value;
document.getElementById('output').contentWindow.document.write(w);
}

jsFiddle [docs] is very powerful and it's the most popular on Stack Overflow.

For interactive explorations I use either this:
http://www.squarefree.com/shell/shell.html
or if I want to type in a whole bunch of code and then run it, I've been using this (from the same site):
http://www.squarefree.com/jsenv/

Related

Add better debug statement to overlay in VS Code

I use the library bignumber.js, which does the job of handling big/small numbers very nicely. However, debugging with VS Code is a pain, as i get the following output upon inspection:
Is there a way for me to edit this? I've looked into prototype editing but I could not get this to work.

How to get source of function when toSource() and toString() return "[native code]" (using mozilla firefox)

I am building a mozilla extension, and one of the tasks is to get the source code of js functions in a school website. I tried using .toSource() and .toString(), which both produced vague answers as shown below.
function click() {
[native code]
}
Is there any work around to decompile the native code into readable javascript code? To be more specific, the function when called eventually does a GET request with information valuable to my project. My goal is to look through the code and access that information from the function without having to have the website change view and page to the GET request. I did look at other stack overflow questions, but I was unable to find any way to make a workaround. Thanks for all advice!

Is building big portions of page in javascript bad? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
On an existing project I am working on, I noticed many of the developers are building big portions of pages with javascript. For example:
$( targetdiv ).append("<div>");
$( targetdiv ).append(" <div class='info'>");
$( targetdiv ).append(" <div id='modes'>");
$( targetdiv ).append(" <table cellspacing='0'>");
$( targetdiv ).append(" <tbody>");
// much more...
I understand using javascript to build certain elements on page. Sometimes the content is dynamic and it is now know when the page first loads (ajax stuff).
However, most of the code (not all is shown) is not dynamic and will be build the same way every time. No 'if' statements or loops
Is there any reason why one would build large parts of pages using javascript vs just having the html be part of the html doc? I would think one would want to minimize javascript html generation because its more confusing and harder to write. Also, javascript html generation has to hurt the performance (Does it?)
I am a "newer" javascript dev so maybe I am missing something. I want to say something but I am "newer" so maybe i don't get "it"
Thanks
That is quite possible the most horrible way I have ever seen a kitten getting killed. Now, that's not just a kitten you killed: each line like this kills a thousands of kittens. And puppies.
Don't do this. It's wrong. It's bad. It's horrible. It's terrible. It's hell. It's anything but good.
On a more serious note...
The code doesn't even work as intended. See the comments on your question.
The HTML is the content, the JavaScript is the behavior. You're mixing both for absolutely no compelling reason.
Every time you call the DOM, you have time to go grab a coffee. You're calling append so many times that just seeing it hurts my eyes.
From a philosophical standpoint, I would have to say this is a bad practice. html belongs in the html and that's it.
That said there are several different ways of adding html to the page via javascript.
<div id="template">
<div class="mycontent">
<!-- stuff -->
</div>
</div>
Then
$('#target').append($('#template').html());
will give you the same results without having html code in your javascript.
But if you must (and sometimes you do) the most performant way is to create dom elements in native js and operate on them:
var template = document.createElement('div');
template.className = "mycontent";
// do more stuff to template
document.getElementById('target').appendChild(template);
The native js method while offering the best performance is hard on you as a developer. So if you wish to work with it as an html string, doing the append once would be best:
var template = "<div class='mycontent'>";
template += // add more string to build the template
template += "</div>";
$('#target').append(template);
While ajax has a performance hit, as some suggested it is also an excellent way of managing your html code, allowing you to put the template in its own file. jQuery also has a shortcut to accomplish this:
$('#target').load('/template.html');
or if you wish to operate on the template:
$.get('/template.html', function(template){
//do stuff to html
$('#target').append(template);
}, 'html');
multiple append statements are with out a doubt the worst possible way to go, of all the less than great ways to go. Personally the first option I provided is my preference, and can easily be paired with libs like http://handlebarsjs.com/
Good luck!
Well, since you're new, it's a good thing you went to confirm your suspicions before taking action. That said, I'm pretty sure this is a case of lazy coding. In fact, there are actually even shorter ways of writing the code you posted, and those shorter ways won't delay the browser immensely.
For one thing, my favored way of writing large portions of the page is with a templating system - you put flat HTML files in your web directories, or in some sort of undisplayed portion of your page, and then import them when you need them to Javascript. A number of libraries can help with this.
But for goodness' sakes, even if you're too lazy to do it that way, do NOT do this with multiple append functions. That means the browser is figuring out new HTML elements and unclosed tags on each call of the function, often rewriting its own work. At the very least, append the HTML strings together bit by bit before calling jQuery.append ONCE.
var newHtml = "<div" +
" <div class='info'>" + ...
$(targetDiv).append(newHtml);

Are there any static Call-Graph and/or Control-Flow-Graph API for JavaScript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Are there any Call-Graph and/or Control-Flow-Graph generators for JavaScript?
Call Graph - http://en.wikipedia.org/wiki/Call_graph
Control Flow Graph - http://en.wikipedia.org/wiki/Control_flow_graph
EDIT: I am looking specifically for a static tool that let me access the graph using some API/code
To do this, you need:
parsing,
name resolution (handling scoping)
type analysis (while JavaScript is arguably "dynamically typed", there are all kinds of typed constants including function constants that are of specific interest here)
control flow analysis (to build up the structure of the control flow graphs within methods)
data flow analysis (to track where those types are generated/used)
what amounts to global points-to analysis (to track function constants passed between functions as values to a point of application).
How to do it is pretty well documented in the compiler literature. However, implementing this matter of considerable sweat, so answers of the form of "you can use a parser result to get what you want" rather miss the point.
If you could apply all this machinery, what you'll get as a practical result is a conservative answer, e.g., "A may call B". This is all you know anyway, consider
void A(int x,y) { if (x>y) foo.B(); }
Because a tool sometime simply can't reason about complex logic, you may get "A may call B" even when the application designer knows it isn't possible:
void A(int x) // programmer asserts x<4
{ if (x>5) foo.B(); }
eval makes the problem worse, because you need to track string value results that arrive at eval commands and parse them to get some kind of clue as to what code is being evaled, and which functions that eval'd code might call. Things get really nasty if somebody passes "eval" in a string to eval :-{ You also likely need to model the program execution context; I suspect there are lots of browser APIs that include callbacks.
If somebody offers you tool that has all the necessary machinery completely configured to solve your problem out of the box, that would obviously be great. My suspicion is you won't get such an offer, because such a tool doesn't exist. The reason is all that infrastructure needed; its hard to build and hardly anybody can justify it for just one tool. Even an "optimizing JavaScript compiler" if you can find one likely won't have all this machinery, especially the global analysis, and what it does have is unlikely to be packaged in a form designed for easy consumption for your purpose.
I've been beating my head on this problem since I started programming in 1969 (some of my programs back then were compilers and I wanted all this stuff). The only way to get this is to amortize the cost of all this machinery across lots of tools.
My company offers the DMS Software Reengineering Toolkit, a package of generic compiler analysis and transformation machinery, with a variety of industrial strength computer langauge front-ends (including C, C++, COBOL and yes, JavaScript). DMS offers APIs to enable custom tools to be constructed on its generic foundations.
The generic machinery listed at the top of the message is all present in DMS, including control flow graph and data flow analysis available through a clean documented API. That flow analysis has to be tied to specific language front ends. That takes some work too, and so we haven't done it for all languages yet. We have done this for C [tested on systems of 18,000 compilation units as a monolith, including computing the call graph for the 250,000 functions present, including indirect function calls!], COBOL and Java and we're working on C++.
DMS has the same "JavaScript" parser answer as other answers in this thread, and viewed from just that perspective DMS isn't any better than the other answers that say "build it on top of a parser". The distinction should be clear: the machinery is already present in DMS, so the work is not one of implement the machinery and tying to the parser; it is just tying it to the parser. This is still a bit of work, but a hell of a lot less than if you just start with a parser.
In general it isn't possible to do this. The reason is that functions are first-class and dynamically typed, so for example:
var xs = some_function();
var each = another_function();
xs.map(each);
There are two unknowns. One is the version of 'map' that is called (since Javascript polymorphism can't be resolved statically in the general case), and the other is the value assigned to 'each', which also can't be statically resolved. The only static properties this code has are that some 'map' method is called on some function we got from 'another_function'.
If, however, that is enough information, there are two resources that might be helpful. One is a general-purpose Javascript parser, especially built using parser combinators (Chris Double's jsparse is a good one). This will let you annotate the parse tree as it is being constructed, and you can add a custom rule to invocation nodes to record graph edges.
The other tool that you might find useful (shameless plug) is a Javascript-to-Javascript compiler I wrote called Caterwaul. It lets you do pattern-matching against syntax trees and knows how to walk over them, which might be useful in your case. It could also help if you wanted to build a dynamic trace from short-term execution (probably your best bet if you want an accurate and detailed result).
WALA is an open-source program analysis framework that can build static call graphs and control-flow graphs for JavaScript:
http://wala.sourceforge.net/wiki/index.php/Main_Page
One caveat is that the call graphs may be missing some edges in the presence of eval, with, and other hard-to-analyze constructs. Also, we're still working on scalability; WALA can't yet analyze jquery in a reasonable amount of time, but some other frameworks can be analyzed. Also, our documentation for building JavaScript call graphs isn't great at the moment (improving it is on my TODO list).
We're actively working on this code, so if you try it and run into issues, you can email the WALA mailing list (https://lists.sourceforge.net/lists/listinfo/wala-wala) or contact me.
I think http://doctorjs.org/ may fit your needs. It has a nice JSON API, is available on github, backed up by mozilla. It's written in JS itself and generally does stuff pretty well (including dealing with polymorphism etc).
Here are a few solutions I can see:
Use Aptana Call Graph view
Aptana is an IDE based on Eclipse that permit you edit and debugging Javascript code.
Use Dynatrace
Dynatrace is a useful tool that let you live trace your code and see the call graph and hot spots.
Use Firebug
The famous developer addon on Firefox
Most of the call graphs generated here will be dynamic, ie you'll see the call graph for a given set of actions. If you're looking for static call graphs check Aptana first. Static call graphs may not let you see dynamic calls (code running through eval()).
For a js approach, check out arguments.callee.caller. It gives you the function that called the function you are in and you can recurse up the call stack. There is an example in this thread http://bytes.com/topic/javascript/answers/470251-recursive-functions-arguments-callee-caller.
Be aware that this may not work in all browsers and you may run in to some unexpected things when you get to the top of the "call stack" or native functions so use at your own risk.
My own example works in IE9 and Chrome 10 (didn't test any other browsers).
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body onload="Function1()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
<script type="text/javascript">
function Function1()
{
Function2();
}
function Function2()
{
Function3();
}
function Function3()
{
Function4();
}
function Function4()
{
var caller = arguments.callee.caller;
var stack = [];
while (caller != null)
{
stack.push(caller);//this is the text of the function. You can probably write some code to parse out the name and parameters.
var args = caller.arguments; //this is the arguments for that function. You can get the actual values here and do something with them if you want.
caller = caller.caller;
}
alert(stack);
}
</script>
</html>
The closest thing you can get to a Call Graph is manipulating a full Javascript AST. This is possible with Rhino, take a look at this article: http://tagneto.blogspot.com/2010/03/requirejs-kicking-some-ast.html
Example from the post:
//Set up shortcut to long Java package name,
//and create a Compiler instance.
var jscomp = Packages.com.google.javascript.jscomp,
compiler = new jscomp.Compiler(),
//The parse method returns an AST.
//astRoot is a kind of Node for the AST.
//Comments are not present as nodes in the AST.
astRoot = compiler.parse(jsSourceFile),
node = astRoot.getChildAtIndex(0);
//Use Node methods to get child nodes, and their types.
if (node.getChildAtIndex(1).getFirstChild().getType() === CALL) {
//Convert this call node and its children to JS source.
//This generated source does not have comments and
//may not be space-formatted exactly the same as the input
//source
var codeBuilder = new jscomp.Compiler.CodeBuilder();
compiler.toSource(codeBuilder, 1, node);
//Return the JavaScript source.
//Need to use String() to convert the Java String
//to a JavaScript String.
return String(codeBuilder.toString());
}
In either Javascript or Java, you could walk the AST to build whatever type of call graph or dependency chain you'd like.
Not related directly to NodeJS, but generally to JavaScript, SAP has released a Web IDE related to HANA (but also accessible freely from the HANA Cloud - see more details here http://scn.sap.com/community/developer-center/cloud-platform/blog/2014/04/15/sap-hana-web-ide-online-tutorial).
In this Web IDE, there is a REST-based service that analyzes JavaScript content with primary focus (but not only) on creating a Call Graph. There are many consumers of that service, like Code Navigation.
Some more information here (see the Function Flow part):
http://scn.sap.com/community/developer-center/hana/blog/2014/12/02/sap-hana-sps-09-new-developer-features-sap-hana-web-based-development-workbench
Note: I am the main developer of this service.

console-like interface on a web page using javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I very like MySQLs mysql cli tool and I don't like phpMyAdmin.
[IMHO]It's a nice thing for a Windows user, but its not so good when you've used to console.[/IMHO].
What I want is to build a web page containing element with console-like input (for example something like this) which should get input from user, send it to PHP script on back-end and show back-end response.
Back-end script is done (it was the easiest part), but I can't find any library for JavaScript implementing console-like input.
I've tried to examine and modify for my needs example I've provided, but it's too bloated (because doesn't use any libraries) and implements specific thing. Also I would like this element to provide some auto-completion for input.
Any ideas on such JS library?
I think you are looking for this: jQueryTerminal
there is shellinabox - javascript terminal.
EDIT:
There is also library xterm.js that's real terminal emulator.
EDIT 2:
My jQuery Terminal library is useful when you need custom behavior and you can write your code in JS or as backend code, but backend need to be simple input -> output, if you want to run for instance interactive backend commands, like vi or emacs, you need proper tty, for this use xterm.js (or implement that in JavaScript) for any other usage jQuery Terminal is better. It have lot of features and you don't need to run process on the server (listen on a port) which usually is forbidden on shared hostings or GitHub pages.
instead of using console.log() use document.write()
It will write text on the webpage just like console.log would in the console
I've made a console library called Simple Console (I'll probably rename it because simple-console is taken on npm)
It handles command history and such for you, and you can use it to implement any kind of console.
var handleCommand = (command)=> {
var req = new XMLHttpRequest();
req.addEventListener("load", ()=> {
con.log(req.responseText);
// TODO: use con.error for errors and con.warn for warnings
// TODO: maybe log a table element to display rows of data
});
// TODO: actually pass the command to the server
req.open("GET", "mysql.php");
req.send();
};
var con = new SimpleConsole({
handleCommand,
placeholder: "Enter MySQL queries",
storageID: "mysql-console"
});
document.body.appendChild(con.element);
Check out the documentation on GitHub for more information.
hmm firebug console ?
http://getfirebug.com/commandline

Categories

Resources