yzt
2023-05-05 634ab285812bcc3eb802cacb9ec54f489bc2728f
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
(function () {
  "use strict";
  window.parent.postMessage("reload", "*");
 
  function defined(value) {
    return value !== undefined;
  }
 
  function print(value) {
    if (value === null) {
      return "null";
    } else if (defined(value)) {
      return value.toString();
    }
    return "undefined";
  }
 
  console.originalLog = console.log;
  console.log = function (d1) {
    console.originalLog.apply(console, arguments);
    window.parent.postMessage(
      {
        log: print(d1),
      },
      "*"
    );
  };
 
  console.originalWarn = console.warn;
  console.warn = function (d1) {
    console.originalWarn.apply(console, arguments);
    window.parent.postMessage(
      {
        warn: defined(d1) ? d1.toString() : "undefined",
      },
      "*"
    );
  };
 
  console.originalError = console.error;
  console.error = function (d1) {
    console.originalError.apply(console, arguments);
    if (!defined(d1)) {
      window.parent.postMessage(
        {
          error: "undefined",
        },
        "*"
      );
      return;
    }
 
    // Look for d1.stack, "bucket.html:line:char"
    var lineNumber = -1;
    var errorMsg = d1.toString();
    if (typeof d1.stack === "string") {
      var stack = d1.stack;
      var pos = stack.indexOf(Sandcastle.bucket);
      if (pos < 0) {
        pos = stack.indexOf("<anonymous>");
      }
      if (pos >= 0) {
        var lineStart = stack.indexOf(":", pos);
        if (lineStart > pos) {
          var lineEnd1 = stack.indexOf(":", lineStart + 1);
          var lineEnd2 = stack.indexOf("\n", lineStart + 1);
          if (
            lineEnd2 > lineStart &&
            (lineEnd2 < lineEnd1 || lineEnd1 < lineStart)
          ) {
            lineEnd1 = lineEnd2;
          }
          if (lineEnd1 > lineStart) {
            /*eslint-disable no-empty*/
            try {
              lineNumber = parseInt(
                stack.substring(lineStart + 1, lineEnd1),
                10
              );
            } catch (ex) {}
            /*eslint-enable no-empty*/
          }
        }
      }
    }
 
    if (lineNumber >= 0) {
      window.parent.postMessage(
        {
          error: errorMsg,
          lineNumber: lineNumber,
        },
        "*"
      );
    } else {
      window.parent.postMessage(
        {
          error: errorMsg,
        },
        "*"
      );
    }
  };
 
  window.onerror = function (errorMsg, url, lineNumber) {
    if (defined(lineNumber)) {
      if (defined(url) && url.indexOf(Sandcastle.bucket) > -1) {
        // if the URL is the bucket itself, ignore it
        url = "";
      }
      if (lineNumber < 1) {
        // Change lineNumber to the local one for highlighting.
        /*eslint-disable no-empty*/
        try {
          var pos = errorMsg.indexOf(Sandcastle.bucket + ":");
          if (pos < 0) {
            pos = errorMsg.indexOf("<anonymous>");
          }
          if (pos >= 0) {
            pos += 12;
            lineNumber = parseInt(errorMsg.substring(pos), 10);
          }
        } catch (ex) {}
        /*eslint-enable no-empty*/
      }
      window.parent.postMessage(
        {
          error: errorMsg,
          url: url,
          lineNumber: lineNumber,
        },
        "*"
      );
    } else {
      window.parent.postMessage(
        {
          error: errorMsg,
          url: url,
        },
        "*"
      );
    }
    console.originalError.apply(console, [errorMsg]);
    return false;
  };
 
  Sandcastle.declare = function (obj) {
    /*eslint-disable no-empty*/
    try {
      //Browsers such as IE don't have a stack property until you actually throw the error.
      var stack = "";
      try {
        throw new Error();
      } catch (ex) {
        stack = ex.stack.toString();
      }
      var needle = Sandcastle.bucket + ":"; // Firefox
      var pos = stack.indexOf(needle);
      if (pos < 0) {
        needle = " (<anonymous>:"; // Chrome
        pos = stack.indexOf(needle);
      }
      if (pos < 0) {
        needle = " (Unknown script code:"; // IE 11
        pos = stack.indexOf(needle);
      }
      if (pos >= 0) {
        pos += needle.length;
        var lineNumber = parseInt(stack.substring(pos), 10);
        Sandcastle.registered.push({
          obj: obj,
          lineNumber: lineNumber,
        });
      }
    } catch (ex) {}
    /*eslint-enable no-empty*/
  };
 
  Sandcastle.highlight = function (obj) {
    if (typeof obj !== "undefined") {
      for (var i = 0, len = Sandcastle.registered.length; i < len; ++i) {
        if (
          obj === Sandcastle.registered[i].obj ||
          obj.primitive === Sandcastle.registered[i].obj
        ) {
          window.parent.postMessage(
            {
              highlight: Sandcastle.registered[i].lineNumber,
            },
            "*"
          );
          return;
        }
      }
    }
    window.parent.postMessage(
      {
        highlight: 0,
      },
      "*"
    );
  };
})();