-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.js
403 lines (311 loc) · 9.01 KB
/
run.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
var stopping = false;
var intervals = [];
var savedDetails = {};
var stoppingAfter = 0;
var text_done = '(Done)';
var text_already_done = '(Skipped - Already Done)';
var text_visiting_extracting = '(Visiting & Extracting)';
var text_next_to_visit = '(Next To Visit)';
var color_done = '#39D5FF';
var color_already_done = '#E7E7E7';
var color_visiting_extracting = '#5EFAF7';
var color_next_to_visit = '#FFCCBC';
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action == 'update' && message.value) {
savedDetails = message.value;
}
});
function start() {
stopping = false;
$('#extractor-start-button').text('Stop');
stoppingAfter = parseInt($('#extractor-stopping-number').val()) || 0;
$('#extractor-stopping-after-number').text(stoppingAfter);
$('#extractor-stopping').fadeIn();
startVisiting(0);
}
function stop() {
stopping = true;
$('#extractor-start-button').text('Start');
$('#extractor-stopping').hide();
for (var i = 0; i < intervals.length; i++) {
clearInterval(intervals[i]);
}
}
function startVisiting(i) {
if (stopping) {
stop();
return;
}
var peopleRows = $('#results .people');
var personRow = peopleRows[i];
var personTitle = $(personRow).find('a.title');
if (personRow && personTitle) {
var nowText = $(personTitle).text();
var personLink = $(personTitle).attr('href');
i++;
//dont visit again if already visited
if (!personLink || nowText.indexOf('LinkedIn Member') > -1 || nowText.indexOf(
text_next_to_visit) > -1 || nowText.indexOf(text_done) > -1 || nowText.indexOf(
text_already_done) > -1 || !canVisit(personLink)) {
if (nowText.indexOf(text_already_done) === -1) {
nowText = nowText.replace(' ' + text_done, '').replace(' ' +
text_next_to_visit, '');
$(personTitle).text(nowText + ' ' + text_already_done);
$(personRow).css('background-color', color_already_done);
}
startVisiting(i);
return;
}
nowText += " " + text_next_to_visit;
$(personRow).css('background-color', color_next_to_visit);
$(personTitle).text(nowText);
var delay = Math.round(Math.random() * (25000 - 10000)) + 10000;
var delayS = Math.round(delay / 1000);
$('#extractor-next-visit').text(delayS + 's');
var interval = setInterval(function() {
delayS--;
$('#extractor-next-visit').text(delayS + 's');
if (delayS <= 0) {
var intervalIndex = intervals.indexOf(interval);
if (intervalIndex > -1) {
clearInterval(intervals[intervalIndex]);
intervals.splice(intervalIndex, 1);
}
if (nowText.indexOf(text_visiting_extracting) === -1) {
nowText = nowText.replace(text_next_to_visit, text_visiting_extracting);
$(personTitle).text(nowText);
$(personRow).css('background-color', color_visiting_extracting);
}
visitPerson(personLink, function(profileDetails) {
saveOrPrint(profileDetails);
incrementVisitCount();
if (nowText.indexOf(text_done) === -1) {
nowText = nowText.replace(text_visiting_extracting, text_done);
$(personTitle).text(nowText);
$(personRow).css('background-color', color_done);
}
stoppingAfter--;
$('#extractor-stopping-after-number').text(stoppingAfter);
if (stoppingAfter <= 0) {
stop();
} else {
startVisiting(i);
}
return;
});
}
}, 1000);
intervals.push(interval);
} else {
var next = $('a.page-link[rel="next"]');
if (next) {
$('a.page-link[rel="next"]')[0].click();
setTimeout(function() {
startVisiting(0);
}, 4000);
} else {
stop();
}
}
}
function visitPerson(link, completed) {
$.ajax({
url: link,
type: 'post',
headers: {
'contentType': 'application/x-www-form-urlencoded'
},
timeout: 10000
}).done(function(data) {
var html = $.parseHTML(data);
var name = $($(html).find('.full-name')).text();
var headline = $($(html).find('#headline')).text();
var title = $($(html).find(
'#background-experience .current-position header h4')).map(function() {
return $(this).text();
}).get().join(' ,');
var locality = $($(html).find('#top-card .locality')).text();
var industry = $($(html).find('#top-card .industry')).text();
var currentCompanies = $($(html).find('#overview-summary-current ol')).text();
var previousCompanies = $($(html).find('#overview-summary-past ol')).text();
var lastCompany = $($(html).find(
'#overview-summary-current a[href*="/company/"]:first'));
var lastCompanyLink = '';
if (lastCompany && lastCompany.attr('href')) {
lastCompanyLink = 'https://www.linkedin.com' + lastCompany.attr('href');
}
var education = $($(html).find('#overview-summary-education ol')).text();
var connections = $($(html).find('.member-connections > strong')).text();
var skills = $($(html).find('#profile-skills .endorse-item-name-text')).map(
function() {
return $(this).text();
}).get().join(' ,');
var interests = $($(html).find('#interests-view li')).text();
var link = $($(html).find('#top-card .view-public-profile')).text();
var companyWebsite = '';
var profileDetails = {
name: name,
headline: headline,
title: title,
locality: locality,
industry: industry,
currentCompanies: currentCompanies,
previousCompanies: previousCompanies,
lastCompany: lastCompanyLink,
education: education,
connections: connections,
skills: skills,
interests: interests,
link: link,
companyWebsite: companyWebsite
};
if (!lastCompanyLink) {
completed(profileDetails);
return;
} else {
$.ajax({
url: lastCompanyLink,
type: 'get',
timeout: 10000
}).done(function(data) {
var htmlAgain = $.parseHTML(data);
var code = $($(htmlAgain).find('#stream-about-section-embed-id-content'))
.html() || '{}';
var str = code.replace("<!--", "").replace("-->", "");
var companyWebsite = JSON.parse(str).website;
if (companyWebsite)
profileDetails.companyWebsite = companyWebsite.toLowerCase().trim();
completed(profileDetails);
return;
});
}
});
}
function incrementVisitCount() {
var day = savedDetails.day || 0;
var visited = savedDetails.visited || 0;
var nowDay = new Date().getDate();
if (day != nowDay) {
visited = 0;
savedDetails.day = nowDay;
savedDetails.visited = 0;
}
visited++;
savedDetails.visited = visited;
$('#extractor-visited').text(visited);
chrome.runtime.sendMessage({
'message': 'save',
'toSaveDetails': savedDetails
}, function() {});
}
function saveOrPrint(details) {
var API = savedDetails.API;
if (API) {
try {
$.ajax({
url: API,
type: 'POST',
data: {
data: details
}
}).complete(function(returned) {
});
} catch (e) {}
}
console.log(details);
}
function getProfileId(viewLink) {
var matched = viewLink.match(/targetId%3.*?%/i);
var idStr = '';
if (matched) {
idStr = matched[0];
}
return idStr.replace(/%/gi, '').replace(/targetId/i, '').trim();
}
function canVisit(viewLink) {
var idStr = getProfileId(viewLink);
if (!idStr)
return true;
if (typeof savedDetails.idsVisited === 'undefined') {
savedDetails.idsVisited = {};
}
var idsVisited = savedDetails.idsVisited;
var daysSkip = savedDetails.daysSkip;
if (typeof daysSkip === 'undefined') {
daysSkip = 365;
savedDetails.daysSkip = 365;
}
var nowTime = new Date().getTime();
var visitedAgo = idsVisited[idStr] || 0;
if ((nowTime - visitedAgo) <= daysSkip * 24 * 60 * 1000) {
return false;
} else {
idsVisited[idStr] = nowTime;
//saved
savedDetails.idsVisited[idStr] = nowTime;
chrome.runtime.sendMessage({
'message': 'save',
'toSaveDetails': savedDetails
}, function() {});
return true;
}
}
function initialize(complete) {
chrome.runtime.sendMessage({
'message': 'load'
}, function(returnedDetails) {
if (returnedDetails)
savedDetails = returnedDetails;
$.get(chrome.extension.getURL("toolbar.html"), function(toolbarHTML) {
$('#srp_main_').append(toolbarHTML);
if (API) {
$('#extractor-save-to').val(API);
}
var API = savedDetails.API || '';
var day = savedDetails.day || 0;
var visited = savedDetails.visited || 0;
var nowDay = new Date().getDate();
if (day != nowDay) {
visited = 0;
savedDetails.day = nowDay;
savedDetails.visited = 0;
chrome.runtime.sendMessage({
'message': 'save',
'toSaveDetails': savedDetails
}, function() {});
}
$('#extractor-visited').text(visited);
complete();
});
});
}
$(function() {
$('#srp_main_').on('click', '#extractor-start-button', function() {
var text = $(this).text();
if (text === 'Start') {
start();
} else {
stop();
}
});
$('#srp_main_').on('click', '#extractor-options', function() {
chrome.runtime.sendMessage({
'message': 'options'
}, function() {});
});
$('#srp_main_').on('change', '#extractor-stopping-number', function() {
var max = parseInt($(this).attr('max'));
var min = parseInt($(this).attr('min'));
if ($(this).val() > max) {
$(this).val(max);
} else if ($(this).val() < min) {
$(this).val(min);
}
});
initialize(function() {
extend();
});
});
//add more functions
function extend() {
}