-
Notifications
You must be signed in to change notification settings - Fork 18
/
crawler.js
507 lines (433 loc) · 18.5 KB
/
crawler.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
crawler.js - sobnik.chrome module
Copyright (c) 2014 Artur Brugeman <[email protected]>
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 () {
console.log ("Loading crawler");
var sobnik = window.sobnik;
console.assert (sobnik, "Sobnik required");
var cmn = sobnik.require ("cmn");
var crawlerTabSignal = "sobnik-chrome-crawler-tab-signal";
function bgStart ()
{
console.log ("Loading crawler.bg");
var server = sobnik.require ("server.bg");
var boards = sobnik.require ("boards");
var self = {
ad: {},
tab: null,
to: null,
cb: null,
failures: 0,
isReady: false,
forceNewTab: false,
}
function callback ()
{
if (self.cb)
self.cb ();
self.cb = null;
}
function clearTTL ()
{
if (self.to != null)
clearTimeout (self.to);
self.to = null;
}
function checkTab (t, callback)
{
// executeScript might fail silently w/o calling
// the callback. I don't know how to reproduce that,
// but it may happen and will block the whole process
// w/o proper handling.
chrome.tabs.executeScript (t, {
code: "document.getElementById ('"
+crawlerTabSignal+"') != null",
}, function (result) {
var found = false;
var error = chrome.runtime.lastError;
// console.log ("Tab", t, error, result);
if (!error && result)
{
result.forEach (function (f) {
if (f)
found = true;
});
}
callback (found, error);
})
}
function close ()
{
clearTTL ();
if (!self.tab)
{
callback ();
return;
}
// check if tab is still ours
var t = self.tab;
self.tab = null;
checkTab (t, function (found) {
if (found)
{
console.log ("Tab is still ours");
// remove
chrome.tabs.remove (Number(t), function () {
if (chrome.runtime.lastError)
console.log (chrome.runtime.lastError);
callback ();
});
}
else
{
console.log ("Tab is not ours");
callback ();
}
});
}
function startTTL ()
{
if (self.to != null)
clearTTL ();
// killer
var ttl = 300000; // ms, 300 sec
self.to = cmn.later (ttl, function () {
self.failures++;
console.log ("TTL expired", self.failures);
if (self.tab == null)
{
// no tab was opened, why?
// we should not search the crawler tab next time here
// as our tab might have stuck and not responding
self.forceNewTab = true;
console.log ("Crawler tab stuck? Forcing new tab");
}
if (self.failures > 2)
{
self.failures = 0;
close ();
}
else
{
callback ();
}
});
}
function startTab (t)
{
self.tab = t.id;
if (chrome.runtime.lastError)
console.log (chrome.runtime.lastError);
// maybe user closed it immediately after we requested the update
if (!self.tab)
return;
// notice if tab gets closed
chrome.tabs.onRemoved.addListener(function (id) {
if (id == self.tab)
{
self.tab = null;
clearTTL ();
callback ();
}
})
}
function searchCrawlerTab (incognito, callback)
{
console.log ("searchCrawlerTab");
// reset
self.tab = null;
if (self.forceNewTab)
{
self.forceNewTab = false;
callback ();
return;
}
// search crawler tab
chrome.windows.getAll ({populate: true}, function (ws) {
// no windows?
if (ws.length == 0)
{
callback ();
return;
}
// for each window
var tabsCount = 0;
ws.forEach (function (w) {
// for each tab
w.tabs.forEach (function (t) {
// count this tab
tabsCount++;
checkTab (t.id, function (found) {
// count back this tab
tabsCount--;
// found?
if (found && self.tab == null)
{
if (!t.incognito && incognito)
{
// close our tab as we've obviously
// switched to incognito crawling
chrome.tabs.remove (Number(t.id));
}
else
{
// yes!
self.tab = t.id;
callback ();
}
}
// was it the last tab and we found nothing?
if (tabsCount == 0 && self.tab == null)
callback ();
})
})
})
if (tabsCount == 0 && self.tab == null)
callback ();
})
}
function open (ad, cback)
{
console.log (ad);
self.ad = ad;
self.cb = cback;
self.isReady = false;
// Start killer immediately.
// Somewhere there may be a failure that will stop the whole
// bg process (i.e. executeScript may fail silently).
// So, TTL now works for any kind of failures and will restart
// crawler, hopefully.
startTTL ();
var incognito = false;
function doOpen ()
{
console.log ("doOpen");
// now, if no crawler tab found - go create one
if (self.tab == null)
{
var wid = null;
function start ()
{
chrome.tabs.create ({
windowId: wid,
url: ad.Url,
active: false,
selected: false,
}, startTab);
}
// search incognito window - use it by default
chrome.windows.getAll (function (windows) {
if (chrome.runtime.lastError)
console.log (chrome.runtime.lastError);
windows.forEach (function (w) {
if (w.incognito && w.id)
wid = w.id;
});
// no incognito window found, but user
// requested incognito mode - open new window
if (incognito && wid == null)
chrome.windows.create ({incognito: true}, function (w) {
console.log (w);
if (chrome.runtime.lastError)
console.log (chrome.runtime.lastError);
// w == null if user disallowed
// incognito access while opening
if (w)
wid = w.id;
start ();
})
else
start ();
})
}
else
{
console.log ("reusing", self.tab);
chrome.tabs.update (self.tab, {
url: ad.Url
}, startTab);
}
}
// get settings and start
chrome.storage.local.get ("crawlerIncognito", function (items) {
incognito = items.crawlerIncognito == "on";
if (incognito)
chrome.extension.isAllowedIncognitoAccess (function (allowed) {
incognito = allowed;
searchCrawlerTab (incognito, doOpen);
});
else
searchCrawlerTab (incognito, doOpen);
});
}
function create ()
{
// random delays 50-120 seconds
var delays = [];
var m = sobnik.debugFastCrawler ? 1 : 10;
for (var i = 0; i < 30; i++)
delays.push (cmn.rdelay (5 * m, 12 * m));
// multiplier used for back-off
var delayMult = 1.0;
function backoff () {
delayMult *= 1.5;
if (delayMult > 5.0)
delayMult = 5.0;
}
function speedup () {
delayMult = 1.0;
}
function retry () {
backoff ();
getJob ();
}
function getJob () {
var r = Math.floor (Math.random () * delays.length);
var d = delays[r] * delayMult;
console.log ("Next job after "+d);
function get ()
{
cmn.getCrawlerAllowed (function (allowed) {
if (!allowed)
{
// retry later after the same delay
getJob ();
}
else
{
// get the job
var request = {
"Boards": boards.allBoards (),
};
server.crawlerJob (request, function (data) {
speedup ();
open (data, getJob);
}, retry);
}
});
}
cmn.later (d, get, retry);
}
return getJob;
}
function isCrawlerTabReady (message, sender)
{
// return message.AdId == self.ad.AdId
return self.tab && sender.tab && self.tab == sender.tab.id;
}
function ready (message, sender, reply)
{
if (!self.isReady && isCrawlerTabReady (message, sender))
{
self.isReady = true;
console.log ("Crawler tab ready");
if (!reply ({type: "startCrawler"}))
callback ();
}
else
{
console.log ("Tab ready");
reply ();
}
}
function done ()
{
console.log ("Crawler tab done");
clearTTL ();
callback ();
}
// public
function start ()
{
cmn.setEventListeners ({
"crawlerOff": close,
"parserDone": done,
"ready": ready,
});
var crawler = create ();
crawler ();
}
window.sobnik.crawler.bg = {
start: start,
}
}
function tabStart ()
{
console.log ("Loading crawler.tab");
var board = sobnik.require ("boards/current");
var parser = sobnik.require ("parser");
function insertBanner ()
{
// add signal so that plugin can find this tab
// even when restarted
var div = document.createElement ('div');
div.id = crawlerTabSignal;
$("body").append (div);
var settingsUrl = chrome.extension.getURL ("settings.html");
var html = "<div id='sobnikCrawlerInfoDiv' "
+ "style='position: fixed; left: 10%; top: 10%; "
+ "border: 1px solid #aaa; background:rgba(220,220,220,0.9); "
+ "width: 80%; height: 80%; z-index: 100000; "
+ "padding: 20px 40px; margin: 0; font-family: Arial'>"
+ "<a href='#' onclick=\"$('#sobnikCrawlerInfoDiv').hide (); return false;\" "
+ "style='position: absolute; top: 10px; right: 10px'>X</a>"
+ "<h1 style='margin: 20px 10px; padding: 0; font-size: 6em; "
+ "text-align: center;'>"
+ "Тут работает S<span style='color:#2c3'>o</span>bnik!"
+ "</h1>"
+ "<h2 style='font-size: 2em; text-align: center; "
+ "margin: 20px 10px; padding: 0; '>"
+ "Это не реклама, пожалуйста, прочитайте это сообщение."
+ "</h2>"
+ "<p style='font-size: larger'>У вас установлен <a href='http://sobnik.com' target='_blank'>плагин Sobnik</a>, который фильтрует риэлторов. Для работы плагину нужно анализировать содержимое объявлений. В день публикуется очень много объявлений, сбор их — ресурсоемкий процесс. Чтобы Sobnik мог оставаться <strong>бесплатным</strong>, теперь каждый пользователь сможет вносить вклад в общее дело. Чтобы узнать подробности — прочитайте <a href='http://sobnik.com/kak-rabotaet-sobnik.html' target='_blank'>инструкцию</a>.</p>"
+ "<p style='font-size: larger; margin-top: 10px;'>В этой вкладке вашего браузера Sobnik будет сканировать объявления, и отправлять информацию в общую базу. Sobnik вам не помешает — он открывает не более одного объявления в минуту. Таким образом, без особых неудобств и усилий, Вы сможете помогать множеству людей по всей стране, а они в ответ будут помогать Вам.</p>"
+ "<p style='font-size: larger; margin-top: 10px;'>Вам стоит отключить сбор объявлений в <a href='#' onclick=\"return false;\" id='sobnikShowSettings'>настройках</a>, если у вас не безлимитный доступ в Интернет. Там же можно настроить сканирование по расписанию.</p>"
+ "<p style='font-size: larger; margin-top: 10px;'>Если у Вас есть предложения или вопросы, пожалуйста, пишите в нашей группе <a href='https://vk.com/sobnik_com' target='_blank'>Вконтакте</a>, или на электронную почту <strong>[email protected]</strong></p>"
+ "<h2 style='font-size: 3em; text-align: center; margin-top: 30px;'>Спасибо!</h2>"
+ "</div>";
$("body").append (html);
// we may not inline the onclick into the html, as it will be
// executed in the context of web-site, not the extension.
// this way we add handler in the context of extension.
cmn.later (0, function () {
$("#sobnikShowSettings").on ("click", function () {
// FIXME this should be abstracted away
// but currently we can't w/o creating cyclic dep-cy
chrome.runtime.sendMessage(
/* ext_id= */"",
{type: "showSettings"}
);
});
});
}
// public
function start ()
{
if (!sobnik.debugStartCrawler)
insertBanner ();
parser.start ();
}
window.sobnik.crawler.tab = {
start: start
}
}
window.sobnik.crawler = {
bg: bgStart,
tab: tabStart,
}
}) ();