-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.js
218 lines (205 loc) · 7.56 KB
/
debug.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
Debugging of Javascript
No jQuery required.
When appending 'd=1' after the URL, a debug button will appear on the website and when clicked on it, debug output is shown as overlay over the webpage. Click again or double click on the debug output to hide it.
*/
var g_d = location.href.match(/\bd\=[1-9]/); // || localStorage.getItem("_jsdebug");
var _jsdebug_callback = function(later) {
var consolelog = console.info; // avoid console.log because of cyclic recursion of console.log !!
var consoletrace = console.trace;
var origconsole = console;
var jsonify=function(o){
var seen=[];
try {
var jso=JSON.stringify(o, function(k,v){
if (typeof v =='object') {
if ( !seen.indexOf(v) ) { return '__cycle__'; }
//console.info(v)
seen.push(v);
} return v;
});
return jso;
} catch (err) {
return o.toString()
}
};
//https://stackoverflow.com/questions/6715571/how-to-get-result-of-console-trace-as-string-in-javascript-with-chrome-or-fire
function getStackTrace () {
let stack = new Error().stack || '';
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
function __translatestack(stacktrace)
{
let h = stacktrace.indexOf('@');
let file = stacktrace;
let func = "[anonymous]";
if (h > 0) {
func = stacktrace.substr(0,h);
file = stacktrace.substr(h);
}
if (navigator.userAgent.indexOf("Safari") != -1) { // Webkit
let f = false;
if (navigator.userAgent.indexOf("Chrome") > 0) { // Chrome / Brave / etc.
f = file.substr(3).match(/^(.+?)\s+(\S+)\?.+?:(\d+)/);
} else { // Apple Safari
// @http://localhost/strato/startpage/ephemeris/safaritest.html?d=1&clear=&clearhistory=&157993143794700141#:228:8
f = file.match(/^((\S+?)\?.+?:(\d+))/);
if (f) {
f[1] = func; // f[5] = function, f[3] = line number
}
}
if (!f) { // occurs in Brave in beginning only
return (file + " " + func + "(): " );
}
file = f[2];
let parts = file.split("/");
file = parts[parts.length-1] + " " + f[3];
func = f[1];
} else if (navigator.userAgent.indexOf("Firefox") != -1) { // Firefox ?
file = file.replace(/^.+?\/([^\/]+?)(\?.*?)?(\d+:\d+)$/, "$1 $3");
file = file.replace(/(.+?)(\d+):\d+$/, "$1 $2");
}
let out = (file + " " + func + "(): " );
return out;
}
function __makehtml(output,raw)
{
if (typeof(output) == "string") {
output = output.replace(/&/g, '&');
output = output.replace(/\x3C/g, '<');
output = output.replace(/>/g, '>');
//output = output.replace(/\n/g, '<br />');
}
if (typeof(raw) == 'undefined' || !raw) {
return jsonify(output) + "\n";
} else {
return jsonify(output) + "\n";
// return output;
}
}
console.log = function() {
// you may do something with side effects here.
// log to a remote server, whatever you want. here
// for example we append the log message to the DOM
// p.innerText = JSON.stringify(args);
// // call the original console.log function
console._log(1, arguments);
}
console.trace = function(title, level)
{
consoletrace.apply(); // call original function
let params = getStackTrace();
let stackline = new Array();
if (typeof level == 'undefined') {
level = 0;
}
for (var i in params) {
if (i>level) {
stackline.push(__translatestack(params[i]));
}
}
var str = "\n" + stackline.join("\n");
if (g_d) {
if (arguments.length < 1) {
title = "";
}
g_d.innerHTML += (`\n** START TRACE ${title}**` + __makehtml(str) + "\n** END TRACE **\n\n");
}
}
console._log = function(level, params)
{
stacktrace = getStackTrace()[level+1];
let out = __translatestack(stacktrace);
let output = params;
//console.info(params);
if (params.length == 1) {
output = params[0];
}
consolelog(out,output);
if (g_d) {
g_d.innerHTML += out + __makehtml(output,1) + "\n";
}
}
window.onerror = function () {
if (g_d) {
g_d.innerHTML += "ERROR: " + jsonify(arguments) + "\n";
}
}
if (g_d) {
var h = document.getElementsByTagName('head').item(0);
var s = document.createElement("style");
s.type = "text/css";
s.appendChild(document.createTextNode("pre#js_debug { \
border: solid black 2px; display:none; position:fixed; top: 20px;\
font-family: Lucida Console, monospace; background-color: white; color: black; font-size: 9pt; height: 400px; overflow:scroll; width:calc(100% - 20px);\
white-space: pre-wrap; word-wrap: break-word; z-index:10000;\
} \
#js_debugclick { \
color:red; font-weight:bold; z-index:10001; position:fixed; left: 30px; padding: 2px; background: rgba(128,128,128, .5); \
} \
"));
h.appendChild(s);
_toggleelement = function (elementid)
{
var u = document.getElementById(elementid);
//console.info(u.style.display);
if (u.style.display != 'none') {
u.style.display = 'none';
} else {
u.style.display = 'block';
}
}
var body = document.getElementsByTagName('body');
_adddebug = function()
{
if (!document.getElementById('js_debug')) {
//console.info(document.getElementsByTagName('body')[0]);
document.getElementsByTagName('body')[0].insertAdjacentHTML("afterbegin",
"<div class=\"js_debug\" id=\"js_debugclick\" onclick=\"_toggleelement('js_debug')\">JS DEBUG</div>\
<pre id='js_debug' style=\"display:none;\" ondblclick=\"_toggleelement('js_debug');\"></pre>\n");
g_d = document.getElementById('js_debug');
}
}
if (typeof(later) != "undefined") {
window.addEventListener('load', (event) => {
_adddebug();
///console.info('page is fully loaded');
});
} else {
_adddebug();
}
}
};
/*
// only when jQuery is loaded (optional !) does not load earlier
if (typeof($) != 'undefined' && typeof($.fn.jquery) != 'undefined') {
$(document).ready(function() {
_jsdebug_callback();
});
}
else */
if (document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
_jsdebug_callback();
} else {
document.addEventListener("DOMContentLoaded", _jsdebug_callback);
}
function p_r()
{
if (!g_d) return;
if (arguments[0] == "__STACK__") {
console.trace("p_r", 1);
return;
}
if (typeof console._log != 'undefined') {
console._log(1, arguments);
} else {
console.log(arguments);
}
}
if (g_d) {
var __moredebug = location.href.match(/\bd\=[2-9]/);
// if (__moredebug) console.log(["which", __iswebkit, __ischrome, __issafari, __isopera, __isfirefox]);
}