forked from sobnik/sobnik.chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmn.js
212 lines (183 loc) · 4.75 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
/*
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++)
{
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);
});
}
sobnik.cmn = {
zpad: zpad,
later: later,
repeat: repeat,
rdelay: rdelay,
matchRxs: matchRxs,
setEventListeners: setEventListeners,
getCrawlerAllowed: getCrawlerAllowed,
}
}) ();