-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbackground.js
218 lines (192 loc) · 6.97 KB
/
background.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
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
function htmlentities(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
function getBaseHostByUrl(url) {
var localUrlRegexp = /(file:\/\/.*)|(:\/\/[^.:]+([\/?:]|$))/; // file:// | local
var rootHostRegexp = /:\/\/(([\w-]+\.\w+)|(\d+\.\d+\.\d+\.\d+)|(\[[\w:]+\]))([\/?:]|$)/; // domain.com | IPv4 | IPv6
var subDomainRegexp = /:\/\/[^\/]*\.([\w-]+\.\w+)([\/?:]|$)/; // sub.domain.com
return localUrlRegexp.exec(url) ? 'localhost' : (rootHostRegexp.exec(url) || subDomainRegexp.exec(url))[1];
}
function initDefaultOptions() {
var optionsValues = {
showIcon: true,
ignore404others: true,
ignoreBlockedByClient: true,
relativeErrorUrl: true,
popupMaxWidth: 70,
popupMaxHeight: 40
};
for(var option in optionsValues) {
if(typeof localStorage[option] == 'undefined') {
var value = optionsValues[option];
localStorage[option] = typeof(value) == 'boolean' ? (value ? 1 : '') : value;
}
}
}
initDefaultOptions();
// Ignore net::ERR_BLOCKED_BY_CLIENT initiated by AdPlus & etc
var ignoredUrlsHashes = {};
var ignoredUrlsLimit = 100;
function isUrlIgnoredByType(url) {
if(!url.indexOf('chrome-extension://')) { // ignore Google Chrome extensions 404 errors
return true;
}
var ext = url.split('.').pop().split(/\#|\?/)[0].toLowerCase();
if(ext == 'js') {
return localStorage['ignore404js'];
}
if(ext == 'css') {
return localStorage['ignore404css'];
}
return localStorage['ignore404others'];
}
function getIgnoredUrlHash(url) {
return url.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')) {
var url = getIgnoredUrlHash(e.url);
if(!isUrlIgnoredByType(url)) {
if(ignoredUrlsHashes[url]) { // move url in the end of list
delete ignoredUrlsHashes[url];
}
ignoredUrlsHashes[url] = true;
var ignoredUrlsArray = Object.keys(ignoredUrlsHashes);
if(ignoredUrlsArray.length > ignoredUrlsLimit) {
delete ignoredUrlsHashes[ignoredUrlsArray[0]];
}
}
}
}, {urls: ["<all_urls>"]});
function handleInitRequest(data, sender, sendResponse) {
var tabHost = getBaseHostByUrl(data.url);
chrome.tabs.get(sender.tab.id, function callback() { // 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: typeof localStorage['icon_' + tabHost] != 'undefined' ? localStorage['icon_' + tabHost] : localStorage['showIcon'],
showPopup: typeof localStorage['popup_' + tabHost] != 'undefined' ? localStorage['popup_' + tabHost] : localStorage['showPopup'],
showPopupOnMouseOver: localStorage['showPopupOnMouseOver'],
popupMaxWidth: localStorage['popupMaxWidth'],
popupMaxHeight: localStorage['popupMaxHeight']
});
}
function handleErrorsRequest(data, sender, sendResponse) {
var popupErrors = [];
var tabHost = getBaseHostByUrl(data.url);
var tabBaseUrl = (/^([\w-]+:\/\/[^\/?]+)/.exec(data.url) || [null, null])[1];
for(var i in data.errors) {
var error = data.errors[i];
var errorHost = getBaseHostByUrl(error.url);
if(localStorage['ignoreExternal'] && errorHost != tabHost) {
continue;
}
if(error.is404) {
if(ignoredUrlsHashes[getIgnoredUrlHash(error.url)] || isUrlIgnoredByType(error.url)) {
delete data.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: /, '');
var errorHtml = 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);
var m = new RegExp('^(\\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;
}
var lines;
if(localStorage['showTrace'] && error.stack && (lines = error.stack.replace(/\n\s*at\s+/g, '\n').split('\n')).length > 2) {
lines.shift();
for(var ii in lines) {
var urlMatch = /^(.*?)\(?(([\w-]+):\/\/.*?)(\)|$)/.exec(lines[ii]);
var url = urlMatch ? urlMatch[2] : null;
var method = urlMatch ? urlMatch[1].trim() : lines[ii];
var lineMatch = url ? (localStorage['showColumn'] ? /^(.*?):([\d:]+)$/ : /^(.*?):(\d+)(:\d+)?$/).exec(url) : null;
var line = 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 {
var url = 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, function callback() { // 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"
}
});
var errorsHtml = 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 + '/');
}
}
var popupUri = '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);
}
else if(data._errors) {
handleErrorsRequest(data, sender, sendResponse);
}
return true;
});