-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
473 lines (411 loc) · 13.4 KB
/
index.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
require("dotenv").config();
const { exit } = require("process");
const puppeteer = require("puppeteer");
const fs = require("fs");
const createCsvWriter = require("csv-writer").createObjectCsvWriter;
const data = require("./config.json");
const { EMAIL: email, PASSWORD: password } = process.env;
const {
locale,
baseURL,
keyword,
workPlaceTypes,
location,
AvgExperience,
periodOfTime,
browserPath,
resolution,
numberOfJobsPerPage,
avoidJobTitles,
avoidCompanies,
} = data;
const t = require(`./i18n/${locale}.json`);
let page = "";
let browser = "";
let csvWriter = null;
function logs() {
console.clear();
console.log("\n==========================================\n");
console.log(`\t${t.appTitle}`);
console.log("\n==========================================\n");
}
async function login() {
await findTargetAndType('[name="session_key"]', email);
await findTargetAndType('[name="session_password"]', password);
page.keyboard.press("Enter");
}
async function initializer() {
browser = await puppeteer.launch({
headless: false,
executablePath: browserPath,
args: [resolution],
defaultViewport: null,
timeout: 60000,
// userDataDir: "./userData",
});
page = await browser.newPage();
const pages = await browser.pages();
if (pages.length > 1) {
await pages[0].close();
}
await page.goto(baseURL);
csvWriter = createCsvWriter({
path: "report.csv",
header: [
{ id: "jobTitle", title: "Job Title" },
{ id: "link", title: "Link" },
{ id: "status", title: "Status" },
],
});
}
async function findTargetAndType(target, value) {
const f = await page.$(target);
await f.type(value);
}
async function waitForSelectorAndType(target, value) {
const typer = await page.waitForSelector(target, { visible: true });
await typer.type(value);
}
async function clickElement(selector) {
try {
await page.waitForSelector(selector);
const element = await page.$(selector);
if (element !== null) {
await element.click();
} else {
console.error(`\n${t.elSelector}: "${selector}" ${t.notFound}.`);
}
} catch (error) {
console.error(error);
}
}
const pause = async (ms = 3000) => {
await new Promise((resolve) => setTimeout(resolve, ms));
};
async function filterByKeywords() {
const searchBox = "#global-nav > div > nav > ul > li:nth-child(3)";
await clickElement(searchBox);
await pause();
await waitForSelectorAndType(
'[id^="jobs-search-box-keyword-id"]',
keyword.join(" OR ")
);
}
async function filterByLocation() {
const jobLocationSelector = '[id^="jobs-search-box-location-id"]';
await page.evaluate((selector) => {
const locationSelector = document.querySelector(selector);
if (locationSelector) locationSelector.value = "";
}, jobLocationSelector);
await waitForSelectorAndType(jobLocationSelector, location);
}
const easyApplyFilter = async () => {
await clickElement(".search-reusables__filter-binary-toggle");
await pause();
};
async function filterByTime() {
await clickElement(
"ul.search-reusables__filter-list>li:nth-child(4)>div>span>button"
);
await pause(2000);
await clickElement(
`form > fieldset > div.pl4.pr6 > ul > li:nth-child(${
periodOfTime === "Past 24 hours" ? 4 : 3
}) > label`
);
await pause();
await clickElement("form > fieldset > div + hr + div > button + button");
}
async function filterByType() {
await clickElement(".search-reusables__filter-list>li:nth-child(8)>div");
await pause(2000);
for (const selector of Object.values(workPlaceTypes)) {
await clickElement(selector);
}
await pause(2000);
const showResultsBtn =
".search-reusables__filter-list>li:nth-child(8)>div>div>div>div>div>form>fieldset>div+hr+div>button+button";
await clickElement(showResultsBtn);
}
async function Scrolling() {
console.log(`\n${t.scroll}.....`);
try {
await page.evaluate(() => {
const listOfJobs = document.querySelector(
"div.scaffold-layout__list > div > ul"
);
if (listOfJobs) {
listOfJobs.scrollIntoView();
} else {
console.error(`${t.el404Scroll}.`);
}
});
} catch (error) {
console.error(`${t.errorOnScroll}: \n${error}`);
}
}
function changeValue(input, value) {
var nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value"
).set;
nativeInputValueSetter.call(input, value);
var inputEvent = new Event("input", { bubbles: true });
input.dispatchEvent(inputEvent);
}
function writeInCSV(data) {
csvWriter
.writeRecords([data])
.then(() => {
console.log(`${t.csvSuccess}\n`);
})
.catch((error) => {
console.error(`${t.csvError}: \n${error}`);
});
}
async function getCompanyName() {
const companyNameSelector =
".job-details-jobs-unified-top-card__company-name>a";
const companyName = await page.evaluate((selector) => {
const element = document.querySelector(selector);
return element ? element.text : null;
}, companyNameSelector);
return companyName;
}
async function getJobTitle() {
const jobTitleSelector = ".job-details-jobs-unified-top-card__job-title>h1>a";
const jobTitle = await page.evaluate((selector) => {
const element = document.querySelector(selector);
return element ? element.text : null;
}, jobTitleSelector);
return jobTitle;
}
async function getLink() {
const jobLinkSelector = ".job-details-jobs-unified-top-card__job-title>h1>a";
const jobLink = await page.evaluate((selector) => {
const element = document.querySelector(selector);
return element ? element.href : null;
}, jobLinkSelector);
return jobLink;
}
const getTotalJobResult = async () => {
const jobResultString = await page.evaluate(() => {
const el = document.querySelector(
"[class*='jobs-search-results-list__subtitle']"
);
return el ? el.innerText.split(" ")[0] : "";
});
return jobResultString.split(",").join("");
};
const closeJobApplicationDialog = async () => {
await pause();
await page.evaluate(() => {
const xBtn = document.querySelector(
".artdeco-modal__dismiss.artdeco-button.artdeco-button--circle.artdeco-button--muted.artdeco-button--2.artdeco-button--tertiary.ember-view"
);
if (xBtn) xBtn.click();
});
};
const fillAndApply = async () => {
const totalJobCount = await getTotalJobResult();
const maxPagination = parseInt(
Math.ceil(parseFloat(totalJobCount) / parseFloat(numberOfJobsPerPage))
);
let currentPage = 1;
let currentJobIndex = 1;
while (currentPage <= maxPagination) {
for (let index = 0; index < numberOfJobsPerPage; index++) {
if (currentJobIndex > totalJobCount) {
console.log(`\n==========\n${t.endOfScript}.\n==========`);
exit(0);
}
await Scrolling();
console.log(`${t.jobNo} [${currentJobIndex} / ${totalJobCount}]`);
currentJobIndex++;
const activeJob = `[class*='jobs-search-two-pane__job-card-container--viewport-tracking-${index}']>div`;
await clickElement(activeJob);
await pause();
//Check for application button
const easyApplyButton = "[class*=jobs-apply-button]>button";
if ((await page.$(easyApplyButton)) === null) {
console.log(t.alreadyApplied);
continue;
}
let companyName = await getCompanyName();
const containsUnwantedCompanyName = avoidCompanies.some((name) =>
companyName?.toLowerCase().includes(name?.toLowerCase())
);
if (containsUnwantedCompanyName) {
console.log(`${t.skipCompany}: ${companyName}`);
continue;
}
const jobTitle = await getJobTitle();
const jobLink = await getLink();
// Check if the job title is in the list of titles to avoid
const jobTitleRegex = new RegExp(
`\\b(${avoidJobTitles
.map((title) => title.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.join("|")})(?=\\b|[^a-zA-Z0-9])`,
"i"
);
if (jobTitleRegex.test(jobTitle)) {
console.log(`${t.skipTitle}: ${jobTitle}`);
continue;
}
console.log(`${t.applyTo} ${jobTitle} ...`);
await pause();
const easyApplyLimitReached = await page.evaluate(() => {
const easyApplyLimitEl = document.querySelector(
".artdeco-inline-feedback__message"
);
return easyApplyLimitEl && easyApplyLimitEl.innerText.includes("limit");
});
if (easyApplyLimitReached) {
console.log(`==========\n${t.limit}...\n==========`);
exit(0);
}
await clickElement(easyApplyButton);
// Check to see if the "Job search safety reminder" dialog is displayed
await pause();
await page.evaluate(() => {
const continueApplyingButton = document.querySelector(
'div[class="artdeco-modal__actionbar ember-view job-trust-pre-apply-safety-tips-modal__footer"]>button+div>div>button'
);
if (continueApplyingButton) continueApplyingButton.click();
});
const isSingleStepApplication = await page.evaluate(() => {
const submitOrNextBtn = document.querySelector(
'div[class="display-flex justify-flex-end ph5 pv4"]>button'
);
if (submitOrNextBtn.innerText.toLowerCase().includes("submit")) {
submitOrNextBtn.click();
return true;
}
return false;
});
if (isSingleStepApplication) await closeJobApplicationDialog();
let skipped = false;
let firstPage = true;
while (firstPage == true && !isSingleStepApplication) {
if (
await page.evaluate(() => {
const nextBtn = document.querySelector(
'div[class="display-flex justify-flex-end ph5 pv4"]>button'
);
if (nextBtn) nextBtn.click();
})
) {
firstPage = true;
} else {
firstPage = false;
break;
}
await pause();
}
if (firstPage == false && !isSingleStepApplication) {
const nextBtn =
'div[class="display-flex justify-flex-end ph5 pv4"]>button + button';
await clickElement(nextBtn);
await pause();
// TODO: This part currently does nothing but should be used to auto input, to be done later.
// if (
// (await page.$(
// 'input[class="ember-text-field ember-view fb-single-line-text__input"]'
// )) != null
// ) {
// await page.evaluate(() => {
// const divElem = document.querySelector("div.pb4");
// const inputElements = divElem.querySelectorAll("input");
// let value = 3;
// var nativeInputValueSetter = Object.getOwnPropertyDescriptor(
// window.HTMLInputElement.prototype,
// "value"
// ).set;
// for (let index = 0; index < inputElements.length; index++) {
// nativeInputValueSetter.call(inputElements[index], value);
// var inputEvent = new Event("input", { bubbles: true });
// inputElements[index].dispatchEvent(inputEvent);
// }
// });
// }
let counter = 30;
let finalPage = false;
do {
await pause();
const modalExists = await page.$(
'div[class*="artdeco-modal-overlay"]>div>div+div>div>button>span'
);
if (!modalExists) {
counter--;
process.stdout.write(`\r${t.waiting}: ${counter}${t.remains}`);
finalPage = await page.evaluate(() => {
const nextButton = document.querySelector(
'div[class="display-flex justify-flex-end ph5 pv4"]>button + button'
);
if (nextButton) {
nextButton.click();
return false;
} else {
return true;
}
});
} else {
counter = -2;
}
} while (counter > 0 && counter <= 30 && finalPage === false);
if (finalPage === false) {
// due to inactivity, skip the job
await pause();
await clickElement(
".artdeco-modal__dismiss.artdeco-button.artdeco-button--circle.artdeco-button--muted.artdeco-button--2.artdeco-button--tertiary.ember-view"
);
await pause();
await clickElement(
'[data-control-name="discard_application_confirm_btn"]'
);
skipped = true;
console.log(`\n${t.jobSkipped}`);
} else {
await closeJobApplicationDialog();
}
}
// Add the Job to the CSV file
writeInCSV({
jobTitle: jobTitle,
link: jobLink,
status: skipped ? "Skipped" : "Applied",
});
}
await Scrolling();
console.log(`${t.scrolledPage} ${currentPage}`);
if (currentPage < maxPagination) {
await clickElement(
`ul[class="artdeco-pagination__pages artdeco-pagination__pages--number"]>li:nth-child(${
currentPage + 1
})`
);
}
currentPage++;
}
};
async function filterAndSearch() {
await filterByKeywords();
await pause(1000);
await filterByLocation();
await page.keyboard.press("Enter");
await pause(1000);
await easyApplyFilter();
await filterByTime();
await pause();
await filterByType();
await pause();
}
async function main() {
logs();
await initializer();
await login();
await filterAndSearch();
await fillAndApply();
await browser.close();
}
main();