forked from russss/callsign-scrape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ofcom-scrape.js
109 lines (95 loc) · 3.21 KB
/
ofcom-scrape.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
const puppeteer = require('puppeteer');
function transformSelector(selector) {
return selector.replace(/:/gu, '\\3a ');
}
function waitFor(page, selector) {
return page.waitFor(transformSelector(selector), {timeout: 10000});
}
async function fillFields(page, form) {
// Disappointingly, you can't type into multiple fields simultaneously.
for (let key of Object.keys(form)) {
await page.type(transformSelector(key), form[key]);
}
}
async function click(page, selector) {
try {
await Promise.all([
page.waitForNavigation({timeout: 10000}),
page.$eval(transformSelector(selector), el => el.click()),
]);
} catch (err) {
throw new Error(
`Error when clicking ${selector} on URL ${page.url()}: ${err}`,
);
}
}
/* This function is run in the browser and makes the callsign-checking call to the
* server, returning the JS status object.
*/
function avcheck(suffix) {
let inputParam = {};
inputParam['licenceType'] = CS.getAttributeValue('Licence_Type_0');
inputParam['requestedCallSignSuffix'] = suffix.toUpperCase();
inputParam['requestedCallSignPrefix'] = '';
inputParam['licenseeId'] = CS.getAttributeValue('Licensee_0');
let promise = new Promise((resolve, reject) => {
Visualforce.remoting.Manager.invokeAction(
'LicenceConfigWidgets.checkCallSignAvailability',
JSON.stringify(inputParam),
function(result, event) {
if (event.statusCode === 200) {
resolve(JSON.parse(result));
} else {
reject();
}
},
{escape: false},
);
});
return promise;
}
class OfcomScrape {
async init(options) {
this.browser = await puppeteer.launch(options);
this.page = await this.browser.newPage();
await this.page.setViewport({width: 1000, height: 1500});
}
async login(email, password) {
await this.page.goto('https://ofcom.force.com');
await waitFor(this.page, 'input#login:template:guest:login');
await fillFields(this.page, {
'input#login:template:guest:username': email,
'input#login:template:guest:password': password,
});
await click(this.page, 'input#login:template:guest:login');
// We need to wait for an actual page to appear, as the login does two redirects.
await waitFor(this.page, 'div.of-wrapper');
// Check for a login error
if (await this.page.$(transformSelector('#login:template:error'))) {
return false;
}
// Wait for the whole logged-in page to load
await waitFor(
this.page,
'input#dashboard:template:internal:licensingTab:j_id109:applyForLicence',
);
return true;
}
async selectPendingLicense() {
await this.page.goto('https://ofcom.force.com/LicensingComLicences');
await click(
this.page,
'#licences:template:internal:licenceApplicationsList:licenceApplicationList:licenceApplications:j_id237:0:continueApplication',
);
await waitFor(this.page, 'button[data-cs-group="Next"]');
await this.page.click('button[data-cs-group="Next"]');
await waitFor(this.page, 'input#checkCallSignAvailabilityButton');
}
async testCallsign(suffix) {
return this.page.evaluate(avcheck, suffix);
}
async close() {
this.browser.close();
}
}
exports.default = OfcomScrape;