Sandcastle-client.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. (function() {
  2. 'use strict';
  3. window.parent.postMessage('reload', '*');
  4. function defined(value) {
  5. return value !== undefined;
  6. }
  7. function print(value) {
  8. if (value === null) {
  9. return 'null';
  10. } else if (defined(value)) {
  11. return value.toString();
  12. }
  13. return 'undefined';
  14. }
  15. console.originalLog = console.log;
  16. console.log = function(d1) {
  17. console.originalLog.apply(console, arguments);
  18. window.parent.postMessage({
  19. 'log' : print(d1)
  20. }, '*');
  21. };
  22. console.originalWarn = console.warn;
  23. console.warn = function(d1) {
  24. console.originalWarn.apply(console, arguments);
  25. window.parent.postMessage({
  26. 'warn' : defined(d1) ? d1.toString() : 'undefined'
  27. }, '*');
  28. };
  29. console.originalError = console.error;
  30. console.error = function(d1) {
  31. console.originalError.apply(console, arguments);
  32. if (!defined(d1)) {
  33. window.parent.postMessage({
  34. 'error' : 'undefined'
  35. }, '*');
  36. return;
  37. }
  38. // Look for d1.stack, "bucket.html:line:char"
  39. var lineNumber = -1;
  40. var errorMsg = d1.toString();
  41. if (typeof d1.stack === 'string') {
  42. var stack = d1.stack;
  43. var pos = stack.indexOf(Sandcastle.bucket);
  44. if (pos < 0) {
  45. pos = stack.indexOf('<anonymous>');
  46. }
  47. if (pos >= 0) {
  48. var lineStart = stack.indexOf(':', pos);
  49. if (lineStart > pos) {
  50. var lineEnd1 = stack.indexOf(':', lineStart + 1);
  51. var lineEnd2 = stack.indexOf('\n', lineStart + 1);
  52. if (lineEnd2 > lineStart && (lineEnd2 < lineEnd1 || lineEnd1 < lineStart)) {
  53. lineEnd1 = lineEnd2;
  54. }
  55. if (lineEnd1 > lineStart) {
  56. /*eslint-disable no-empty*/
  57. try {
  58. lineNumber = parseInt(stack.substring(lineStart + 1, lineEnd1), 10);
  59. } catch (ex) {
  60. }
  61. /*eslint-enable no-empty*/
  62. }
  63. }
  64. }
  65. }
  66. if (lineNumber >= 0) {
  67. window.parent.postMessage({
  68. 'error' : errorMsg,
  69. 'lineNumber' : lineNumber
  70. }, '*');
  71. } else {
  72. window.parent.postMessage({
  73. 'error' : errorMsg
  74. }, '*');
  75. }
  76. };
  77. window.onerror = function(errorMsg, url, lineNumber) {
  78. if (defined(lineNumber)) {
  79. if (defined(url) && url.indexOf(Sandcastle.bucket) > -1) {
  80. // if the URL is the bucket itself, ignore it
  81. url = '';
  82. }
  83. if (lineNumber < 1) {
  84. // Change lineNumber to the local one for highlighting.
  85. /*eslint-disable no-empty*/
  86. try {
  87. var pos = errorMsg.indexOf(Sandcastle.bucket + ':');
  88. if (pos < 0) {
  89. pos = errorMsg.indexOf('<anonymous>');
  90. }
  91. if (pos >= 0) {
  92. pos += 12;
  93. lineNumber = parseInt(errorMsg.substring(pos), 10);
  94. }
  95. } catch (ex) {
  96. }
  97. /*eslint-enable no-empty*/
  98. }
  99. window.parent.postMessage({
  100. 'error' : errorMsg,
  101. 'url' : url,
  102. 'lineNumber' : lineNumber
  103. }, '*');
  104. } else {
  105. window.parent.postMessage({
  106. 'error' : errorMsg,
  107. 'url' : url
  108. }, '*');
  109. }
  110. console.originalError.apply(console, [errorMsg]);
  111. return false;
  112. };
  113. Sandcastle.declare = function(obj) {
  114. /*eslint-disable no-empty*/
  115. try {
  116. //Browsers such as IE don't have a stack property until you actually throw the error.
  117. var stack = '';
  118. try {
  119. throw new Error();
  120. } catch (ex) {
  121. stack = ex.stack.toString();
  122. }
  123. var needle = Sandcastle.bucket + ':'; // Firefox
  124. var pos = stack.indexOf(needle);
  125. if (pos < 0) {
  126. needle = ' (<anonymous>:'; // Chrome
  127. pos = stack.indexOf(needle);
  128. }
  129. if (pos < 0) {
  130. needle = ' (Unknown script code:'; // IE 11
  131. pos = stack.indexOf(needle);
  132. }
  133. if (pos >= 0) {
  134. pos += needle.length;
  135. var lineNumber = parseInt(stack.substring(pos), 10);
  136. Sandcastle.registered.push({
  137. 'obj' : obj,
  138. 'lineNumber' : lineNumber
  139. });
  140. }
  141. } catch (ex) {
  142. }
  143. /*eslint-enable no-empty*/
  144. };
  145. Sandcastle.highlight = function(obj) {
  146. if (typeof obj !== 'undefined') {
  147. for (var i = 0, len = Sandcastle.registered.length; i < len; ++i) {
  148. if (obj === Sandcastle.registered[i].obj || obj.primitive === Sandcastle.registered[i].obj) {
  149. window.parent.postMessage({
  150. 'highlight' : Sandcastle.registered[i].lineNumber
  151. }, '*');
  152. return;
  153. }
  154. }
  155. }
  156. window.parent.postMessage({
  157. 'highlight' : 0
  158. }, '*');
  159. };
  160. }());