Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
150 lines (107 loc) · 3.63 KB
/
Copy pathmain.js
File metadata and controls
150 lines (107 loc) · 3.63 KB
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
const puppeteer = require('puppeteer');
const config = require('./config');
const creds = require('./credentials');
const CAPITALIZE_URLS = {
HOME: 'https://www.hicapitalize.com/',
CAREERS: 'careers/',
TRUE_COST: 'resources/the-true-cost-of-forgotten-401ks/'
};
const trueCostRegex = /\d*\.\d+.\s*\w+/g;
async function clickAndNavigate({ page, link }) {
return Promise.all([
link.click(),
page.waitForNavigation()
]);
};
async function clickFooterLink({ page, pageTitle }) {
await page.goto( CAPITALIZE_URLS.HOME );
const href = `${ CAPITALIZE_URLS.HOME }${ CAPITALIZE_URLS[ pageTitle ] }`;
const anchorSel = `#footer a[href="${ href }"]`;
await page.waitForSelector( anchorSel );
const hrefElem = await page.$( anchorSel );
return clickAndNavigate({ link: hrefElem, page });
};
function toNumber( str ) {
const units = {
million: 10e6,
trillion: 10e12
};
const [ num, unit ] = str.split(/\s+/);
return +num * units[ unit ];
};
async function doCapitalizeWork() {
const browser = await puppeteer.launch( config );
const page = await browser.newPage();
await clickFooterLink({ page, pageTitle: 'CAREERS' });
const elem = await page.waitForSelector(`iframe#grnhse_iframe`);
const iframe = await elem.contentFrame();
await iframe.waitForSelector('.opening');
const jobIWant = await iframe.$$eval(
'.opening',
divs => divs.map( el => {
const anchor = el.querySelector('a');
return {
url: anchor.href,
title: anchor.textContent
};
}).find(
({ title }) => title === 'Senior Full Stack Engineer'
)
);
await page.goto( jobIWant.url );
const screenshot = await page.screenshot({ encoding: 'base64' });
const output = [
`Title: ${ jobIWant.title }`,
`URL: ${ jobIWant.url }`,
`Screenshot (base64 encoded): ${ screenshot }`
].join('\n');
console.log( output );
await page.close();
const trueCostPage = await browser.newPage();
await clickFooterLink({ page: trueCostPage, pageTitle: 'TRUE_COST' });
const subeadingSel = '.elementor-text-editor > p:first-child';
const subheadingEl = await trueCostPage.waitForSelector( subeadingSel );
const subheadingText = await subheadingEl.evaluate( el => el.textContent );
const [
accounts,
amount
] = subheadingText
.match( trueCostRegex )
.map( toNumber );
const trueCost = [
'Number of Abandoned 401(k) accounts: ',
accounts,
'Estimated value of abandoned accounts: ',
amount
].join('\n');
console.log( trueCost );
await browser.close();
};
async function doGitHubWork() {
const browser = await puppeteer.launch( config );
const page = await browser.newPage();
await page.goto('https://github.com/alfred/capitalize-automation');
const signInSel = 'a[href^="/login"]';
await page.waitForSelector( signInSel );
const hrefElem = await page.$( signInSel );
await clickAndNavigate({ link: hrefElem, page });
await page.type( '#login_field', creds.username );
await page.type( '#password', creds.password );
const submitBtn = await page.$('#password ~ .btn[type="submit"]');
await clickAndNavigate({ link: submitBtn, page });
await page.waitForSelector('img.avatar');
const display = await page.evaluate( () => {
const starred = document.querySelector('.starred');
return getComputedStyle( starred ).display;
});
if ( display === 'none' ) {
const unstarredBtn = await page.$('.unstarred button');
await unstarredBtn.click();
await page.waitForSelector('.starred button');
}
await browser.close();
};
( async() => {
await doCapitalizeWork();
await doGitHubWork();
})();