Skip to content

Instantly share code, notes, and snippets.

@kmxz
Last active March 18, 2016 03:10
Show Gist options
  • Save kmxz/6a197cfce9d2cc1d50e1 to your computer and use it in GitHub Desktop.
Save kmxz/6a197cfce9d2cc1d50e1 to your computer and use it in GitHub Desktop.
滑上上下下左右左右开启 debug
(function () {
var buffer = [];
window.addEventListener('error', function (ee) {
addResult({
source: 'window.onerror',
stack: ee.error ? ee.error.stack : (ee.filename + ':' + ee.lineno + ':' + ee.colno),
message: ee.message
});
});
['error', 'warn', 'log', 'info'].map(function (verb) {
var old = console[verb];
console[verb] = function () {
old.apply(console, arguments);
addResult({
source: 'console.' + verb,
stack: new Error().stack,
message: Array.prototype.map.call(arguments, function (content) {
return JSON.stringify(content);
}).join(', ')
});
};
});
var lastStart;
var actions = '';
var showResult;
var addResult = function (newEntry) {
buffer.push(newEntry);
if (showResult) { showResult(newEntry); }
};
var openDebugConsole = function () {
var dom = function (tag, parent, style) {
var el = document.createElement(tag);
el.style.position = 'absolute';
el.style.color = '#000';
el.style.fontSize = '14px';
Object.keys(style).forEach(function (sk) { el.style[sk] = style[sk]; });
parent.appendChild(el);
return el;
};
var consoleWrap = dom('div', document.body, {
'top': '32px', 'left': '32px', 'bottom': '32px', 'right': '32px', 'border': '2px solid #000', 'display': 'none'
});
var outputs = dom('div', consoleWrap, {
'top': '32px', 'left': '0', 'bottom': '32px', 'right': '0', 'overflow': 'scroll', 'background': '#fff'
});
var input = dom('input', consoleWrap, {
'bottom': '0', 'height': '32px', 'left': '0', 'border': '0 none', 'padding': '0', 'background': '#fcc', 'lineHeight': '32px', 'width': '100%', 'borderRight': '64px', 'boxSizing': 'border-box !important'
});
var button = dom('button', consoleWrap, {
'bottom': '0', 'height': '32px', 'width': '64px', 'right': '0', 'border': '0 none', 'padding': '0', 'textAlign': 'center', 'background': '#ccf', 'lineHeight': '32px'
});
showResult = function (entry) {
var b = document.createElement('b');
b.appendChild(document.createTextNode(entry.source));
outputs.appendChild(b);
var i;
if ('stack' in entry) {
i = document.createElement('i');
i.appendChild(document.createTextNode(' [stack]'));
i.addEventListener('click', function () {
addResult({
'source': 'stack',
'message': entry.stack
});
});
outputs.appendChild(i);
}
var pre = document.createElement('pre');
pre.appendChild(document.createTextNode(entry.message));
outputs.appendChild(pre);
outputs.scrollTop = outputs.scrollHeight;
outputs.scrollLeft = 0;
};
button.appendChild(document.createTextNode('EVAL'));
button.addEventListener('click', function () {
addResult({
source: 'eval',
message: JSON.stringify(eval(input.value))
});
});
var close = dom('div', consoleWrap, {
'top': '0', 'height': '32px', 'left': '0', 'right': '0', 'border': '0 none', 'padding': '0', 'textAlign': 'center', 'background': '#ccf', 'lineHeight': '32px'
});
close.appendChild(document.createTextNode('CLOSE'));
close.addEventListener('click', function () {
showResult = null;
document.body.removeChild(consoleWrap);
});
buffer.forEach(showResult);
consoleWrap.style.display = 'block';
};
document.addEventListener('touchstart', function (e) {
lastStart = [e.changedTouches[0].screenX, e.changedTouches[0].screenY];
});
document.addEventListener('touchend', function (e) {
if (!lastStart) { return; }
if (showResult) { return; }
var deltaX = e.changedTouches[0].screenX - lastStart[0];
var deltaY = e.changedTouches[0].screenY - lastStart[1];
if (deltaX > 120 && Math.abs(deltaY) < 60) { actions += ('r'); }
if (deltaX < -120 && Math.abs(deltaY) < 60) { actions += ('l'); }
if (deltaY > 120 && Math.abs(deltaX) < 60) { actions += ('d'); }
if (deltaY < -120 && Math.abs(deltaX) < 60) { actions += ('u'); }
if (actions.length > 15) {
actions = actions.substr(5);
}
if (actions.indexOf('uuddlrlr') >= 0) {
actions = '';
if (window.confirm('Open debug console?')) {
openDebugConsole();
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment