-
Notifications
You must be signed in to change notification settings - Fork 1
/
awe-ue.js
112 lines (95 loc) · 3.41 KB
/
awe-ue.js
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
/*
* Artefact Web Extensions
*
* Copyright 2012, Artefact Group LLC
* Licensed under MIT.
*/
(function(Awe, global, document, undefined) {
var _mailto;
var _subject;
var _additionalOnErrorCallback;
var _ui;
/*
* purpose: Helper function to create a mailto href with error information.
*/
var getMailtoHref = function(message, details) {
var body = "%0D%0D%0D%0D";
body += "Thank you for reporting this error. Please use the space above to provide additional details about what you were doing when this error occured.";
body += "%0D%0DReferring Page: " + document.referrer;
body += "%0D%0DError Message:%0D-------------------------------------------%0D" + details;
return 'mailto:' + _mailto + '?subject=' + _subject + " " + message + '&body=' + body;
}
/*
* purpose: display a minimal user interface to indicate that an error has
* occured and give the user the option to report it.
*/
var showOnErrorPrompt = function(mailtoHref) {
if(!_ui) {
_ui = document.createElement("DIV");
_ui.id = "aweOnErrorPrompt";
_ui.className = "aweOnErrorPrompt";
_ui.style.position = "absolute";
_ui.style.top = "0px";
_ui.style.left = "50px";
_ui.style.width = "400px";
_ui.style.height = "auto";
_ui.style.margin = "0px";
_ui.style.marginTop = "20px";
_ui.style.padding = "10px";
_ui.style.color = "#333";
_ui.style.zIndex = "9999";
_ui.style.lineHeight = "1.25";
_ui.style.backgroundColor = "#f66";
_ui.style.fontFamily = "Arial,Helvetica";
}
document.body.appendChild(_ui);
while (_ui.childNodes.length > 0 ) {
_ui.removeChild(_ui.childNodes[0]);
}
var prompt = document.createElement("DIV");
prompt.innerHTML = "Oops! There was an unexpected error on this page.";
_ui.appendChild(prompt);
var br1 = document.createElement("BR");
_ui.appendChild(br1);
var report = document.createElement("A");
report.href = mailtoHref;
report.innerHTML = "<strong>Click here to report this error via email. Thanks!</strong>";
_ui.appendChild(report);
var br2 = document.createElement("BR");
_ui.appendChild(br2);
var closeThis = document.createElement("A");
closeThis.href = "/";
closeThis.innerHTML = "Close this error message";
xAddEventListener( closeThis, "click", hideOnErrorPrompt, true );
_ui.appendChild(closeThis);
}
/*
* purpose: hide the user interface show after detecting an unhandled error.
*/
var hideOnErrorPrompt = function(evt) {
Awe.cancelEvent(evt);
document.body.removeChild(_ui);
return false;
}
/*
* purpose: window.onerror handler.
*/
var handleError = function(message, url, linenumber) {
showOnErrorPrompt(getMailtoHref(message, "Error: " + message + ", on line " + linenumber + " of " + url));
if(_additionalOnErrorCallback) {
_additionalOnErrorCallback(message, url, linenumber);
}
}
/*
* method: Awe.reportUnhandledErrors
*
* Attach a handler for the window.onerror event to cllect data about unhandled
* script errors.
*/
Awe.reportUnhandledErrors = function(mailto, subject, additionalOnErrorCallback) {
_mailto = mailto;
_subject = subject || "[js-error-report]";
_additionalOnErrorCallback = additionalOnErrorCallback;
window.onerror = handleError;
}
})(Awe, this, document);