-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconsole.js
More file actions
143 lines (121 loc) · 3.76 KB
/
Copy pathconsole.js
File metadata and controls
143 lines (121 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
if(typeof jQuery === "undefined") {
var script = document.createElement("script");
script.type = "text/javascript";
//script.src = "http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js";
script.src = "../jquery-2.0.1.min.js";
document.getElementsByTagName("head")[0].appendChild(script);
}
window.addEventListener("load", function() {
var logger = new Logger();
displayCodeFromScriptBlocks();
wireUpRunButtons(logger);
/*
$(".sample").keypress(function(e) {
var selection = window.getSelection();
if(e.keyCode == 13 && typeof window.getSelection != 'undefined' && selection.isCollapsed) {
var range = selection.getRangeAt(0);
var script = $(this).text();
var beginningOfLine = script.lastIndexOf('\n', range.startOffset - 1);
var indentation = calculateIndentation(script, beginningOfLine);
var indentationNode = document.createTextNode("xxxx")
range.insertNode(indentationNode);
//range.setStart(this, range.startOffset + indentation);
range.setStart(this, 1);
}
}); */
});
function calculateIndentation(value, startOffset) {
var leadingSpaces = /^\s+/.exec(value.substring(startOffset));
if(leadingSpaces && leadingSpaces.length > 0) {
return leadingSpaces[0].length;
}
return 0;
}
function displayCodeFromScriptBlocks() {
var scripts = $("script:not([src])");
var body = $("body");
for(var x=0; x<scripts.length; x++) {
var script = $(scripts[x]).html();
script = cleanScriptText(script);
body.append("<div class='sample' id='sample" + x + "' contenteditable='true'></div>");
$("#sample" + x).text(script);
body.append("<input type='button' value='run' class='run-button' sample='" + x + "' />")
}
var style = " \
.sample { \
background-color: #F6F6F6; \
font-family: monospace; \
white-space:pre; \
padding: 7px; \
/* font-size: 2em; */ \
} \
";
body.append("<style type='text/css'>" + style + "</style>");
}
function cleanScriptText(script)
{
script = script.replace(/^\n/, ""); // trim first line
script = script.replace('\t', " "); // tabs tp spaces
var indentation = calculateIndentation(script);
if(indentation > 0)
script = script.replace(new RegExp("^( ){" + indentation +"}", "gm"), "");
return script;
}
function wireUpRunButtons(logger) {
$(".run-button").click(function() {
logger.clear();
var sampleId = "#sample" + $(this).attr('sample');
var sample = $(sampleId).text();
eval(sample);
});
}
function Logger() {
var logElement;
createOutputLog();
rewriteLogMethod();
captureErrors();
this.clear = function() {
logElement.html("");
}
function createOutputLog() {
var body = $("body");
body.after("<div id='output'></div>");
logElement = $("#output");
var style = " \
.sample { \
background-color: #F6F6F6; \
font-family: monospace; \
} \
#output { \
color: white; \
background-color: gray; \
font-family: monospace; \
height: 200px; \
overflow: scroll; \
padding: 7px; \
margin: 10px; \
white-space:pre; \
/* font-size: 2em; */ \
} \
";
body.append("<style type='text/css'>" + style + "</style>");
}
function log() {
logElement.append(Array.prototype.join.call(arguments, " ") + "\n");
}
function error() {
log("<span style='color:LightPink'>" + Array.prototype.join.call(arguments, " ") + "</span>\n");
}
function rewriteLogMethod() {
console.originalLog = console.log;
console.log = function() {
log.apply(this, arguments);
console.originalLog.apply(console, arguments);
}
}
function captureErrors() {
window.onerror = function(message, file, line) {
error(message);
};
}
}