Skip to content

Commit

Permalink
use new API for debug-console plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
le-jeu committed Jan 30, 2024
1 parent dd60209 commit c4f18b7
Showing 1 changed file with 65 additions and 95 deletions.
160 changes: 65 additions & 95 deletions plugins/debug-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,46 @@ var debugTab = {};
// DEBUGGING TOOLS ///////////////////////////////////////////////////
// meant to be used from browser debugger tools and the like.


debugTab.renderDetails = function() {
debugTab.console.log('portals: ' + Object.keys(window.portals).length);
debugTab.console.log('links: ' + Object.keys(window.links).length);
debugTab.console.log('fields: ' + Object.keys(window.fields).length);
};

debugTab.printStackTrace = function() {
var e = new Error('dummy');
debugTab.console.error(e.stack);
return e.stack;
};

debugTab.console = {};
debugTab.console.show = function() {
$('#chat, #chatinput').show();
$('#chatinput mark').css('cssText', 'color: #bbb !important').text('debug:');
$('#chat > div').hide();
$('#debugconsole').show();
$('#chatcontrols .active').removeClass('active');
$("#chatcontrols a:contains('debug')").addClass('active');
debugTab.create = function () {
window.chat.addChannel({
id: 'debug',
name: 'Debug',
inputPrompt: 'debug:',
inputClass: 'debug',
sendMessage: function (_, msg) {
var result;
try {
result = eval(msg);
} catch (e) {
if (e.stack) {
console.error(e.stack);
}
throw e; // to trigger native error message
}
if (result !== undefined) {
console.error(result.toString());
}
return result;
},
});
};

debugTab.console.renderLine = function(errorType, args) {
debugTab.renderLine = function (errorType, args) {
args = Array.prototype.slice.call(args);
var color = '#eee';
switch (errorType) {
case 'error': color = '#FF424D'; break;
case 'warning': color = '#FFDE42'; break;
case 'error':
color = '#FF424D';
break;
case 'warning':
color = '#FFDE42';
break;
}
var text = [];
args.forEach(function (v) {
if (typeof v !== 'string' && typeof v !== 'number') {
var cache = [];
v = JSON.stringify(v, function(key, value) {
v = JSON.stringify(v, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
Expand All @@ -71,34 +76,35 @@ debugTab.console.renderLine = function(errorType, args) {
var d = new Date();
var ta = d.toLocaleTimeString(); // print line instead maybe?
var tb = d.toLocaleString();
var t = '<time title="'+tb+'" data-timestamp="'+d.getTime()+'">'+ta+'</time>';
var s = 'style="color:'+color+'"';
var l = '<tr><td>'+t+'</td><td><mark '+s+'>'+errorType+'</mark></td><td>'+text+'</td></tr>';
$('#debugconsole table').prepend(l);
var t = '<time title="' + tb + '" data-timestamp="' + d.getTime() + '">' + ta + '</time>';
var s = 'style="color:' + color + '"';
var l = '<tr><td>' + t + '</td><td><mark ' + s + '>' + errorType + '</mark></td><td>' + text + '</td></tr>';
$('#chatdebug table').append(l);
};

debugTab.console.log = function() {
debugTab.console.renderLine('notice', arguments);
debugTab.console = {};
debugTab.console.log = function () {
debugTab.renderLine('notice', arguments);
};

debugTab.console.warn = function() {
debugTab.console.renderLine('warning', arguments);
debugTab.console.warn = function () {
debugTab.renderLine('warning', arguments);
};

debugTab.console.error = function() {
debugTab.console.renderLine('error', arguments);
debugTab.console.error = function () {
debugTab.renderLine('error', arguments);
};

debugTab.console.debug = function() {
debugTab.console.renderLine('debug', arguments);
debugTab.console.debug = function () {
debugTab.renderLine('debug', arguments);
};

function overwriteNative() {
var nativeConsole = window.console;
window.console = L.extend({}, window.console);

function overwrite(which) {
window.console[which] = function() {
window.console[which] = function () {
if (nativeConsole) {
nativeConsole[which].apply(nativeConsole, arguments);
}
Expand All @@ -112,80 +118,44 @@ function overwriteNative() {
overwrite('debug');
}

function setupPosting() {
if (!window.isSmartphone()) {
$('#chatinput input').keydown(function(event) {
var kc = event.keyCode ? event.keyCode : event.which;
if (kc === 13) { // enter
if ($('#chatcontrols .active').text() === 'debug') {
event.preventDefault();
userInput();
}
}
});
}

$('#chatinput').submit(function(event) {
event.preventDefault();
userInput();
});
}

function userInput() {
if ($('#chatcontrols .active').text() !== 'debug') {return;}

var msg = $.trim($('#chatinput input').val());
if (!msg) { return; }

var result;
try {
result = eval(msg);
} catch (e) {
if (e.stack) { debugTab.console.error(e.stack); }
throw e; // to trigger native error message
}
if (result !== undefined) {
debugTab.console.log(result.toString());
}
}


function create() {
if ($('#debugconsole').length) return;
$('#chatcontrols').append('<a>debug</a>');
$('#chatcontrols a:last').click(debugTab.console.show);
$('#chat').append('<div style="display: none" id="debugconsole"><table></table></div>');
// Old API utils
debugTab.renderDetails = function () {
debugTab.console.log('portals: ' + Object.keys(window.portals).length);
debugTab.console.log('links: ' + Object.keys(window.links).length);
debugTab.console.log('fields: ' + Object.keys(window.fields).length);
};

setupPosting();
debugTab.printStackTrace = function () {
var e = new Error('dummy');
debugTab.console.error(e.stack);
return e.stack;
};

if (window.useAndroidPanes()) {
android.addPane('debug', 'Debug', 'ic_action_view_as_list');
window.addHook('paneChanged', function (id) {
if (id === 'debug') {
debugTab.console.show();
}
});
}
}
debugTab.show = function () {
window.chat.show('debug');
};

function setup() {
window.plugin.debug = debugTab;
create();
debugTab.create();
overwriteNative();

$('<style>')
.text('#chat #chatdebug td:nth-child(-n+2) { \n width: 51px\n' + '}\n#chat #chatdebug td:nth-child(3) {\n font-family: monospace\n}')
.appendTo('head');

// emulate old API
window.debug = function () {};
window.debug.renderDetails = debugTab.renderDetails;
window.debug.printStackTrace = debugTab.printStackTrace;
window.debug.console = function () {};
window.debug.console.show = debugTab.console.show;
window.debug.console.show = debugTab.show;
window.debug.console.renderLine = function (text, errorType) {
return debugTab.console.renderLine(errorType, [text]);
return debugTab.renderLine(errorType, [text]);
};
window.debug.console.log = debugTab.console.log;
window.debug.console.warn = debugTab.console.warn;
window.debug.console.error = debugTab.console.error;
}

setup.priority = 'boot';

0 comments on commit c4f18b7

Please sign in to comment.