-
Notifications
You must be signed in to change notification settings - Fork 18
/
cmn.js
424 lines (370 loc) · 11.8 KB
/
cmn.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
cmn.js - sobnik.chrome module
Copyright (c) 2014 Artur Brugeman <brugeman.artur@gmail.com>
Copyright other contributors as noted in the AUTHORS file.
This file is part of sobnik.chrome, Sobnik plugin for Chrome:
http://sobnik.com.
This is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
;(function () {
var sobnik = window.sobnik;
console.assert (sobnik, "Sobnik required");
// public
function zpad (str, n)
{
if (!n)
n = 2;
str += "";
while (str.length < n)
str = "0" + str;
return str;
}
// public
function later (millis, callback)
{
var to = setTimeout (function () {
clearTimeout (to);
callback ();
}, millis);
return to;
}
// public
function repeat (callback, ms)
{
callback ();
if (!ms)
ms = 1000;
later (ms, function () { repeat (callback, ms); });
}
// public
function rdelay (from, till)
{
var ms = 1000;
if (!till)
return from * ms;
return (Math.random () * (till - from) + from) * ms;
}
// public
function getCrawlerAllowed (callback)
{
var keys = ["crawler",
"crawlerOnUntil",
"crawlerOffUntil",
"crawlerSchedule",
"crawlerHour00",
"crawlerHour01",
"crawlerHour02",
"crawlerHour03",
"crawlerHour04",
"crawlerHour05",
"crawlerHour06",
"crawlerHour07",
"crawlerHour08",
"crawlerHour09",
"crawlerHour10",
"crawlerHour11",
"crawlerHour12",
"crawlerHour13",
"crawlerHour14",
"crawlerHour15",
"crawlerHour16",
"crawlerHour17",
"crawlerHour18",
"crawlerHour19",
"crawlerHour20",
"crawlerHour21",
"crawlerHour22",
"crawlerHour23",
];
chrome.storage.local.get (keys, function (items) {
var allowed = false;
console.log ("Crawler settings", items);
var now = new Date ();
if (items.crawler == "off")
{
allowed = false;
if (items.crawlerOnUntil)
{
var till = new Date (items.crawlerOnUntil);
allowed = now.getTime () < till.getTime ();
console.log ("On till", till, allowed);
}
/* if (!allowed && items.crawlerSchedule != "off")
{
var hour = ""+now.getHours ();
if (hour.length < 2) hour = "0"+hour;
allowed = items["crawlerHour"+hour] == "on";
console.log ("Sched on hour", hour, allowed);
}
*/
}
else
{
allowed = true;
if (items.crawlerOffUntil)
{
var till = new Date (items.crawlerOffUntil);
allowed = now.getTime () > till.getTime ();
console.log ("Off till", till, allowed);
}
if (allowed)
{
function hourChecked (h)
{
return items["crawlerHour"+zpad (h)] == "on";
}
var curHourChecked = hourChecked (now.getHours ());
var anyHourChecked = false;
for (var h = 0; h < 24; h++)
anyHourChecked |= hourChecked (h);
if (anyHourChecked)
{
if (items.crawlerSchedule == "off" && curHourChecked)
allowed = false;
if (items.crawlerSchedule != "off" && !curHourChecked)
allowed = false;
}
console.log ("Sched on hour", items.crawlerSchedule, allowed);
}
}
callback (allowed);
});
}
// public
function matchRxs (str, rxs)
{
for (var i = 0; i < rxs.length; i++)
{
// console.log (str, rxs[i], str.match(rxs[i]));
if (str.match(rxs[i]) != null)
return true;
}
return false;
}
// public
function setEventListeners (handlers)
{
chrome.runtime.onMessage.addListener (function (msg, sender, reply) {
if (!msg.type)
return;
function safeReply (data)
{
try
{
reply (data);
return true;
} catch (e) {
return false;
}
}
var handler = handlers[msg.type];
if (handler)
return handler (msg, sender, safeReply);
});
}
// public
function Promise (fn)
{
return new RSVP.Promise (fn);
}
// public
function wait (ms)
{
return Promise (function (fulfill) {
later (ms, fulfill);
})
}
function imageLoaded (img)
{
// http://www.sajithmr.me/javascript-check-an-image-is-loaded-or-not/
if (!img.complete)
return false;
if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0)
return false;
return true;
}
// public
function waitCond (cond)
{
return Promise (function (fulfill) {
function test ()
{
if (cond ())
fulfill ();
else
later (1000, test);
}
// start it
test ();
})
}
// public
function waitDomLoaded (s)
{
return Promise (function (fulfill) {
function findDom (selectors)
{
var $dom = [];
if (!Array.isArray (selectors))
selectors = [selectors];
for (var i = 0; !$dom.length && i < selectors.length; i++)
$dom = $(selectors[i]);
if (!$dom.length)
{
// retry
later (1000, function () {
findDom (selectors);
})
}
else
{
function done ()
{
fulfill ($dom[0]);
}
if ($dom.attr ('src'))
{
// maybe wait until loaded?
if (imageLoaded ($dom[0]))
done ();
else
$dom.on ('load', done);
// FIXME what if it doesn't load?
// reject should be called in that case
}
else
{
done ();
}
}
}
findDom (s);
})
}
// public
function click (objects, type)
{
if (!Array.isArray (objects))
objects = [objects];
objects.forEach (function (o) {
console.log ("clicking", $(o));
// emulation of event
var event = new MouseEvent (type ? type : 'click', {
'view': window,
'bubbles': true,
'cancelable': true
});
// dispatch
$(o).each (function (i, e) {
e.dispatchEvent (event);
});
})
}
// public
function AsyncIterate (iterator, processor)
{
var last = null;
return Promise (function (fulfill) {
function next ()
{
// request next item as a promise
var item = iterator.next ();
if (!item)
{
// empty iterator
fulfill ();
return;
}
// append to last promise
if (last)
// NOTE: you cannot 'then' a promise,
// only a func that returns a promise
last = last.then (function () { return item; });
else
last = item;
// when item is ready
var ready = last.then (function (found) {
if (found)
{
last = ready
// process item
.then (function (data) {
// processor returns a promise
return processor (iterator.value ());
})
// get next item
.then (next);
}
else
{
console.log ("done");
fulfill ();
}
})
}
// start it
next ();
})
}
// public
function AsyncLoop ()
{
var last = null;
return {
next: function (promiseCreator)
{
if (last)
last = last.then (promiseCreator);
else
last = Promise (function (fulfill) {
fulfill (promiseCreator ());
})
},
promise: function ()
{
if (!last)
return Promise (function (fulfill) {
fulfill ();
})
else
return last;
}
}
}
function Now (o)
{
return Promise (function (fulfill) {
if (sobnik.isFunction (o))
fulfill (o ());
else
fulfill (o);
})
}
sobnik.cmn = {
zpad: zpad,
later: later,
wait: wait,
repeat: repeat,
rdelay: rdelay,
matchRxs: matchRxs,
waitDomLoaded: waitDomLoaded,
waitCond: waitCond,
click: click,
setEventListeners: setEventListeners,
getCrawlerAllowed: getCrawlerAllowed,
Promise: Promise,
AsyncLoop: AsyncLoop,
AsyncIterate: AsyncIterate,
Now: Now,
}
}) ();