-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
105 lines (93 loc) · 3.08 KB
/
background.html
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
<html>
<head>
<script>
//----------------------------------------------------------------------
// JsError extension for Google Chrome
// Under Firefox and other browsers, a developer can have a visual indicator of
// errors without having to open the developer console - this provides the same
// for Chrome
//----------------------------------------------------------------------
// Author: Tim Meadowcroft -- http://schmerg.com -- 2011
//----------------------------------------------------------------------
// keep a hash of errors per tabid
var tabErrors = {
};
// apply all properties of src to tgt
var applyObj = function (tgt,src) {
if (tgt && src && typeof src=="object") {
for (var k in src){
tgt[k] = src[k];
}
}
return tgt;
};
// Slight variation of the String.prototype.supplant function from Douglas Crockford's Remedial JavaScript
var supplant = function (str, opts, defval) {
return str.replace(/\{([^{}]*)\}/g, function (s, m) {
var r = opts[m];
if (typeof r === 'string' || typeof r === 'number') {
return r;
}
if (typeof defval === 'string' || typeof defvalr === 'number') {
return defval;
}
return m;
});
};
// set the toolbar button for this tab and this err
var setCtrls = function(tabid, err) {
var emsg = err && supplant("<span class='etype'>{rtype}:</span> <span class='emsg'>{msg}</span>\n"+
"<span class='eurl'>{url}</span> :<span class='eline'>{line}</span>", err, ""),
errcount = err ? 1 : 0,
bOpt = { };
if (tabid) {
bOpt.tabId = tabid;
if (err) {
errcount = tabErrors[tabid].push(emsg);
}
}
chrome.browserAction.setIcon(applyObj({
path: err ? "err16.png" : "ok16.png"
}, bOpt));
chrome.browserAction.setTitle(applyObj({
title: err ? errcount+" error(s): "+err.msg : "No errors"
}, bOpt));
chrome.browserAction.setBadgeText(applyObj({
text: (tabid && tabErrors[tabid] && tabErrors[tabid].length) ? (""+tabErrors[tabid].length) : ""
}, bOpt));
};
// We've inserted a small listener for the error event into each page
// so we record a new error when we're called, and the popup sends a "get"
// request to ask for the errors for a tab
chrome.extension.onRequest.addListener(function(r, sender, sendResponse) {
var tabid = sender.tab ? sender.tab.id : r.tabid;
if (r.rtype && tabid) {
if (r.rtype === "error") {
tabErrors[tabid] = tabErrors[tabid] || [];
setCtrls(tabid, r);
if (sendResponse) {
sendResponse({});
}
}
else if (r.rtype === "clear") {
tabErrors[tabid] = [];
setCtrls(tabid);
if (sendResponse) {
sendResponse({});
}
}
else if (r.rtype === "get") {
if (sendResponse) {
sendResponse({ errors: tabErrors[tabid] || [] });
}
}
}
});
// Be a good boy and clean up when tabs are removed
chrome.tabs.onRemoved.addListener(function(tabid, removeInfo){
delete tabErrors[tabid];
});
console.log("Background.html for JsError loaded");
</script>
</head>
</html>