facePrint.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Just for temporarily mobile debug.
  2. (function () {
  3. var infoDom;
  4. var msgs = [];
  5. var count = 0;
  6. window.facePrint = function (msg, printObj) {
  7. if (!infoDom) {
  8. infoDom = createInfoDom();
  9. }
  10. if (printObj && isObject(msg)) {
  11. msg = window.facePrint.objToStr(msg);
  12. }
  13. msgs.push(encodeHTML(msg));
  14. count++;
  15. if (msgs.length > 30) {
  16. msgs.shift();
  17. }
  18. var str = '';
  19. // Make some change in view, otherwise user may
  20. // be not aware that log is still printing.
  21. for (var i = 0; i < msgs.length; i++) {
  22. str += msgs[i] + ' ' + (count - msgs.length + i) + ' ';
  23. }
  24. infoDom.innerHTML = str;
  25. };
  26. window.facePrint.objToStr = function (obj) {
  27. var msgArr = [];
  28. for (var key in obj) {
  29. msgArr.push(key + '=' + obj[key]);
  30. }
  31. return msgArr.join(', ');
  32. };
  33. function createInfoDom() {
  34. var dom = document.createElement('div');
  35. dom.style.cssText = [
  36. 'position: fixed;',
  37. 'top: 0;',
  38. 'width: 100%;',
  39. 'border: 1px solid red;',
  40. 'height: 20px;',
  41. 'line-height: 20px;',
  42. 'z-index: 2147483647'
  43. ].join('');
  44. document.body.appendChild(dom);
  45. return dom;
  46. }
  47. function encodeHTML(source) {
  48. return source == null
  49. ? ''
  50. : String(source)
  51. .replace(/&/g, '&amp;')
  52. .replace(/</g, '&lt;')
  53. .replace(/>/g, '&gt;')
  54. .replace(/"/g, '&quot;')
  55. .replace(/'/g, '&#39;');
  56. }
  57. function isObject(value) {
  58. // Avoid a V8 JIT bug in Chrome 19-20.
  59. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
  60. var type = typeof value;
  61. return type === 'function' || (!!value && type == 'object');
  62. }
  63. })();