-
Notifications
You must be signed in to change notification settings - Fork 4
/
shared.js
213 lines (192 loc) · 6.07 KB
/
shared.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
/*jshint esversion:6 */
var debug_mode = true;
var log_count = 0;
function log(t) {
try {
sd = document.getElementById('statusdiv');
sd.innerText += '(' + log_count + '): ' + t + "\n";
sd.scrollTop = sd.scrollHeight;
log_count += 1;
} catch (e) {}
if (debug_mode) {
console.log(t);
}
}
var statusOK = function(xhr) {
var s = xhr.status;
var ok = (s === 0) || ((s >= 200) && (s < 300));
return ok;
};
var doGet = function(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// console.log(xhr);
rv = {};
rv.text = xhr.responseText;
rv.status = xhr.status;
rv.err = statusOK(xhr) ? 'OK' : 'not_2xx';
// console.log(rv);
cb(rv);
}
};
var source = url;
// extra weirdness to force dropbox to download rather than page,
// and the date to force no cache
source += '?dl=1&_=' + (new Date()).getTime();
xhr.open('GET', source, true);
xhr.send();
//
// to tell chrome to let us call the cb after
// returning we must return true;
return true;
}
// sets the initial stored url for configuration fetching
// if it is not already set. It also stores it to the local store.
function set_initial_url(cb) {
var source = null;
chrome.storage.local.get(['config_source'], function(items) {
if (items.hasOwnProperty('config_source')) {
source = items.config_source;
cb(source);
} else {
log('resetting config_source at set_initial_url');
source = defaults.config_source;
chrome.storage.local.set({
'config_source': source
}, function() {
cb(source);
});
}
});
}
function zapStorage(cb) {
chrome.storage.local.clear(function() {
set_initial_url(cb);
});
}
// send a message to an event page to have it do an xhr for us
var loadConfigRemote = function(settings, cb) {
if (true) {
if (settings.hasOwnProperty('config_source')) {
doGet(settings.config_source, function(resp) {
var resp_exists = resp && resp.hasOwnProperty('err');
var resp_ok = resp_exists && (resp.err == 'OK');
var resp_nok = resp_exists && (resp.err != 'OK');
if (resp_ok) {
storeConfig(null, resp.text, cb);
} else if (resp_nok) {
cb('err', resp.status);
} else {
cb('err', 'error in eventpage code');
}
});
} else {
cb('err', 'no config source');
}
}
};
function loadConfig(settings, cb, try_remote = true) {
log('loadConfig START');
if (true) {
log('loadConfig readLocal');
var now = (new Date()).getTime();
var have_config = settings.hasOwnProperty('config_valid') && settings.config_valid;
var max_age = defaults.max_age;
if (have_config) {
if (settings.cfgdata.hasOwnProperty('refresh_age')) {
max_age = settings.cfgdata.refresh_age;
}
}
// if max_age is set to negative then we never refresh
var use_stored = max_age < 0 ? true :
((now - settings.config_date) < max_age);
if (have_config && use_stored) {
log('loading from local storage');
cb(null, settings.cfgdata);
} else if (try_remote) {
log('calling loadConfigRemote');
loadConfigRemote(settings, cb);
} else {
log('loadConfig FAILED');
cb('load_config_failed');
}
}
log('loadConfig DONE');
}
function storeConfig(err, txt, cb) {
log("storeConfig START");
if (err !== null) {
cb(err, txt);
return;
}
var data = {};
try {
data = JSON.parse(txt);
} catch (e) {
log("JSON parse error");
log(e);
cb(e, txt);
return;
}
if (data.schema.match(/InsultMarkupLanguage\/0.\d/)) {
chrome.storage.local.set({
'cfgdata': data
}, function() {
if (chrome.lastError) {
log('error storing configdata');
cb(chrome.lastError);
} else {
date = (new Date()).getTime();
chrome.storage.local.set({
'config_date': date,
'config_valid': true,
'last_chosen_time': 0
},
function() {
log("STORE SUCCESS");
chrome.storage.local.get(null, function(settings) {
loadConfig(settings, cb, false);
});
}
);
}
});
}
}
function copyDictByKeys(dst, src) {
var ks = Object.keys(src);
for (var i = 0; i < ks.length; i++) {
dst[ks[i]] = src[ks[i]];
}
}
function createAndSetStyle(name, style_text) {
var style_element_id = 'detrumpify_style_element';
var se = document.getElementById(style_element_id);
if (se === null) {
se = document.createElement('style');
se.setAttribute('id', style_element_id);
se.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(se);
}
se.textContent = name + ' { ' + style_text + ' } ';
}
function removeChildrenReplaceWith(elem, newchildren) {
while (elem.firstChild) {
elem.removeChild(elem.firstChild);
}
for (var i = 0; i < newchildren.length; i++) {
elem.appendChild(newchildren[i]);
}
}
function useIfElse(dict, name, deflt) {
return dict.hasOwnProperty(name) ? dict[name] : deflt;
}
function replace_elem_with_array_of_elems(orig, arry) {
// log('replace_elem_with_array_of_elems');
var newnode = document.createElement('span');
for (var k = 0; k < arry.length; k++) {
newnode.appendChild(arry[k]);
}
orig.parentNode.replaceChild(newnode, orig);
}