Uh oh!
There was an error while loading. Please reload this page.
forked from barbushin/javascript-errors-notifier
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
Latest commit
218 lines (192 loc) · 6.97 KB
/
Copy pathbackground.js
File metadata and controls
218 lines (192 loc) · 6.97 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
functionhtmlentities(str){
vardiv=document.createElement('div');
div.appendChild(document.createTextNode(str));
returndiv.innerHTML;
}
functiongetBaseHostByUrl(url){
varlocalUrlRegexp=/(file:\/\/.*)|(:\/\/[^.:]+([\/?:]|$))/;// file:// | local
varrootHostRegexp=/:\/\/(([\w-]+\.\w+)|(\d+\.\d+\.\d+\.\d+)|(\[[\w:]+\]))([\/?:]|$)/;// domain.com | IPv4 | IPv6
varsubDomainRegexp=/:\/\/[^\/]*\.([\w-]+\.\w+)([\/?:]|$)/;// sub.domain.com
returnlocalUrlRegexp.exec(url) ? 'localhost' : (rootHostRegexp.exec(url)||subDomainRegexp.exec(url))[1];
}
functioninitDefaultOptions(){
varoptionsValues={
showIcon: true,
ignore404others: true,
ignoreBlockedByClient: true,
relativeErrorUrl: true,
popupMaxWidth: 70,
popupMaxHeight: 40
};
for(varoptioninoptionsValues){
if(typeoflocalStorage[option]=='undefined'){
varvalue=optionsValues[option];
localStorage[option]=typeof(value)=='boolean' ? (value ? 1 : '') : value;
}
}
}
initDefaultOptions();
// Ignore net::ERR_BLOCKED_BY_CLIENT initiated by AdPlus & etc
varignoredUrlsHashes={};
varignoredUrlsLimit=100;
functionisUrlIgnoredByType(url){
if(!url.indexOf('chrome-extension://')){// ignore Google Chrome extensions 404 errors
returntrue;
}
varext=url.split('.').pop().split(/\#|\?/)[0].toLowerCase();
if(ext=='js'){
returnlocalStorage['ignore404js'];
}
if(ext=='css'){
returnlocalStorage['ignore404css'];
}
returnlocalStorage['ignore404others'];
}
functiongetIgnoredUrlHash(url){
returnurl.replace(/\d+/g,'');
}
chrome.webRequest.onErrorOccurred.addListener(function(e){
if((localStorage['ignoreBlockedByClient']&&e.error=='net::ERR_BLOCKED_BY_CLIENT')||
(localStorage['ignoreConnectionRefused']&&e.error=='net::ERR_CONNECTION_REFUSED')){
varurl=getIgnoredUrlHash(e.url);
if(!isUrlIgnoredByType(url)){
if(ignoredUrlsHashes[url]){// move url in the end of list
deleteignoredUrlsHashes[url];
}
ignoredUrlsHashes[url]=true;
varignoredUrlsArray=Object.keys(ignoredUrlsHashes);
if(ignoredUrlsArray.length>ignoredUrlsLimit){
deleteignoredUrlsHashes[ignoredUrlsArray[0]];
}
}
}
},{urls: ["<all_urls>"]});
functionhandleInitRequest(data,sender,sendResponse){
vartabHost=getBaseHostByUrl(data.url);
chrome.tabs.get(sender.tab.id,functioncallback(){// mute closed tab error
if(chrome.runtime.lastError){
return;
}
chrome.pageAction.setTitle({
tabId: sender.tab.id,
title: 'No errors on this page'
});
chrome.pageAction.setPopup({
tabId: sender.tab.id,
popup: 'popup.html?host='+encodeURIComponent(tabHost)+'&tabId='+sender.tab.id
});
chrome.pageAction.show(sender.tab.id);
});
sendResponse({
showIcon: typeoflocalStorage['icon_'+tabHost]!='undefined' ? localStorage['icon_'+tabHost] : localStorage['showIcon'],
showPopup: typeoflocalStorage['popup_'+tabHost]!='undefined' ? localStorage['popup_'+tabHost] : localStorage['showPopup'],
showPopupOnMouseOver: localStorage['showPopupOnMouseOver'],
popupMaxWidth: localStorage['popupMaxWidth'],
popupMaxHeight: localStorage['popupMaxHeight']
});
}
functionhandleErrorsRequest(data,sender,sendResponse){
varpopupErrors=[];
vartabHost=getBaseHostByUrl(data.url);
vartabBaseUrl=(/^([\w-]+:\/\/[^\/?]+)/.exec(data.url)||[null,null])[1];
for(variindata.errors){
varerror=data.errors[i];
varerrorHost=getBaseHostByUrl(error.url);
if(localStorage['ignoreExternal']&&errorHost!=tabHost){
continue;
}
if(error.is404){
if(ignoredUrlsHashes[getIgnoredUrlHash(error.url)]||isUrlIgnoredByType(error.url)){
deletedata.errors[i];
continue;
}
error.type='File not found';
error.text=error.url;
popupErrors.unshift('File not found: '+htmlentities(error.url));
}
else{
error.text=error.text.replace(/^Uncaught/,'').replace(/^Error:/,'');
varerrorHtml=localStorage['linkStackOverflow']
? '<a target="_blank" href="http://www.google.com/search?q='+encodeURIComponent(htmlentities(error.text))+'%20site%3Astackoverflow.com" id="">'+htmlentities(error.text)+'</a>'
: htmlentities(error.text);
varm=newRegExp('^(\\w+):\s*(.+)').exec(error.text);
error.type=m ? m[1] : 'Uncaught Error';
if(localStorage['showColumn']&&error.line&&error.col){
error.line=error.line+':'+error.col;
}
varlines;
if(localStorage['showTrace']&&error.stack&&(lines=error.stack.replace(/\n\s*at\s+/g,'\n').split('\n')).length>2){
lines.shift();
for(variiinlines){
varurlMatch=/^(.*?)\(?(([\w-]+):\/\/.*?)(\)|$)/.exec(lines[ii]);
varurl=urlMatch ? urlMatch[2] : null;
varmethod=urlMatch ? urlMatch[1].trim() : lines[ii];
varlineMatch=url ? (localStorage['showColumn'] ? /^(.*?):([\d:]+)$/ : /^(.*?):(\d+)(:\d+)?$/).exec(url) : null;
varline=lineMatch ? lineMatch[2] : null;
url=lineMatch ? lineMatch[1] : url;
if(!url&&method=='Error (native)'){
continue;
}
errorHtml+='<br/> ';
if(url){
errorHtml+=localStorage['linkViewSource']
? ('<a href="view-source:'+url+(line ? '#'+line : '')+'" target="_blank">'+url+(line ? ':'+line : '')+'</a>')
: (url+(line ? ':'+line : ''));
}
if(method){
errorHtml+=' '+method+'()';
}
}
}
else{
varurl=error.url+(error.line ? ':'+error.line : '');
errorHtml+='<br/> '+(localStorage['linkViewSource']
? '<a href="view-source:'+error.url+(error.line ? '#'+error.line : '')+'" target="_blank">'+url+'</a>'
: url);
}
popupErrors.push(errorHtml);
}
}
if(!popupErrors.length){
return;
}
chrome.tabs.get(sender.tab.id,functioncallback(){// mute closed tab error
if(chrome.runtime.lastError){
return;
}
chrome.pageAction.setTitle({
tabId: sender.tab.id,
title: 'There are some errors on this page. Click to see details.'
});
chrome.pageAction.setIcon({
tabId: sender.tab.id,
path: {
"19": "img/error_19.png",
"38": "img/error_38.png"
}
});
varerrorsHtml=popupErrors.join('<br/><br/>');
if(localStorage['relativeErrorUrl']&&tabBaseUrl){
errorsHtml=errorsHtml.split(tabBaseUrl+'/').join('/').split(tabBaseUrl).join('/');
if(localStorage['linkViewSource']){
errorsHtml=errorsHtml.split('href="view-source:/').join('href="view-source:'+tabBaseUrl+'/');
}
}
varpopupUri='popup.html?errors='+encodeURIComponent(errorsHtml)+'&host='+encodeURIComponent(tabHost)+'&tabId='+sender.tab.id;
chrome.pageAction.setPopup({
tabId: sender.tab.id,
popup: popupUri
});
chrome.pageAction.show(sender.tab.id);
sendResponse(chrome.extension.getURL(popupUri));
});
}
chrome.runtime.onMessage.addListener(function(data,sender,sendResponse){
if(data._initPage){
handleInitRequest(data,sender,sendResponse);
}
elseif(data._errors){
handleErrorsRequest(data,sender,sendResponse);
}
returntrue;
});