-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.js
119 lines (93 loc) · 2.69 KB
/
config.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
/* Get and set configuration for this extension. */
/* Configuration is persisted to localstorage. */
// get config value for key
function get_config(key) {
// console.debug("get_config", key);
// is config initialized?
if (localStorage.init == undefined)
_init_config();
var s = localStorage.c || '""';
var c = JSON.parse(s);
return c[key];
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "get_config")
sendResponse(get_config(request.key));
else
sendResponse(null);
});
// set whole config, c is an object
function save_config(c) {
console.debug("save_config", c);
localStorage.c = JSON.stringify(c);
localStorage.init = "1"; // init done
}
// initialize config
function _init_config() {
console.debug("_init_config");
var c = {};
c.beautify = true;
c.tooltip = true;
c.isredcount = true;
c.colorize = true;
c.caching = false;
c.onclick = false;
c.linenum = true;
c.hilight = ["//www.google-analytics.com",
"//ajax.googleapis.com",
"//connect.facebook.net",
"//widgets.twimg.com",
"//platform.twitter.com"];
// css from file
c.css = load_file("css.ini");
save_config(c);
}
// should this url be hilighted?
function config_is_hilighted(url) {
var arr = get_config("hilight");
for (var i = 0; i < arr.length; i++) {
if (arr[i] && url.match(arr[i]))
return true;
}
return false;
}
// load a local file
function load_file(fname) {
try {
xhr = new XMLHttpRequest();
xhr.open("GET", fname, false);
xhr.send(null);
return xhr.responseText;
} catch (e) {
console.debug("ERR load_file");
return "//ERR "+fname;
}
}
// update icon badge with counts - max 4 chars!
// (here in config.js since no util.js yet exists)
function update_badge(data) {
if (!get_config("tooltip"))
return;
var txt = "";
var jscount = 0;
if (data) {
jscount = data.js.length;
// var csscount = data.css.length;
txt = ""+jscount;
}
chrome.browserAction.setBadgeText({"text":txt});
// colorize red?
var isred = false;
if (get_config("isredcount")) {
var level = parseInt(get_config("redcount") || 50);
isred = jscount >= level ? true : false;
}
var col = isred ? "#a00" : "#777";
chrome.browserAction.setBadgeBackgroundColor({"color":col});
}
/* insert JSON encoded reply here for debugging */
var debugdata = null;
// startsWith in Chrome 41 only?
String.prototype.startsWith = function(s) {
return this.indexOf(s) === 0;
};