Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created April 11, 2012 17:05
Show Gist options
  • Save jbclements/2360584 to your computer and use it in GitHub Desktop.
Save jbclements/2360584 to your computer and use it in GitHub Desktop.
Tam
<html>
<head>
<title> JavaScript Variable Name Parser</title>
<style type="text/css">
.inputtext { width: 512px; height: 512px; }
</style>
<script language="JavaScript">
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
Components.utils.import("resource://gre/modules/reflect.jsm");
var listOfVars = new Array();
var counter = 0;
function parseJS() {
try {
var ast = Reflect.parse(document.getElementById("JSForm").value);
js_traverse(ast);
listOfVars.sort(compare);
printList();
} catch (e) {alert(e);}
}
function js_traverse(node) {
var type = typeof node;
if(type == "object")
{
for (var key in node) {
if(key == "declarations"){
var temp = contains(node[key][0].id.name);
if(temp == -1){
listOfVars[counter] = {id:node[key][0].id.name, value:'1'};
counter++;
} else {
listOfVars[temp].value++;
}
}
js_traverse(node[key]);
}
}
}
function contains(varName)
{
for(var item in listOfVars)
if(listOfVars[item].id == varName)
return item;
return -1;
}
function printList()
{
document.write('<center><table width="512" border="1">');
document.write('<tr><td><center><b>Name</b></center></td>');
document.write('<td><center><b>Count</b></center></td></tr>');
for(var item in listOfVars) {
document.write('<tr><td><center>' + listOfVars[item].id + '</center></td>');
document.write('<td><center>' + listOfVars[item].value + '</center></td></tr>');
}
}
function compare(a, b) {
if (a.value < b.value)
return 1;
if(a.value > b.value)
return -1;
return 0;
}
</script>
</head>
<body>
<textarea id="JSForm" rows="50" cols="100">Enter JavaScript Code Here . . .</textarea>
<form>
<input type="button" value="Parse The Code!" onClick="parseJS()">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment