-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackerRank_auto.js
More file actions
128 lines (97 loc) · 3.82 KB
/
hackerRank_auto.js
File metadata and controls
128 lines (97 loc) · 3.82 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
// node hackerRank_auto.js --url="https://www.hackerrank.com" --config=config.json
// npm init -y
// npm i puppeteer
// npm install minimist
let minimist = require("minimist");
let puppeteer = require("puppeteer");
let fs = require("fs");
let args = minimist(process.argv);
let configJSON = fs.readFileSync(args.config, "utf-8");
let configJSO = JSON.parse(configJSON);
run();
async function run() {
// start the browser
let browser = await puppeteer.launch({
defaultViewport: null,
args: [
"--start-maximized"
],
headless: false
});
// get a tab
let pages = await browser.pages();
let page = pages[0];
// go to url
await page.goto(args.url);
// click on login1
await page.waitForSelector("a[data-event-action='Login']");
await page.click("a[data-event-action='Login']");
// click on login2
await page.waitForSelector("a[href='https://www.hackerrank.com/login']");
await page.click("a[href='https://www.hackerrank.com/login']");
// type userid
await page.waitForSelector("input[name='username']");
await page.type("input[name='username']", configJSO.userid, { delay: 50 });
// type password
await page.waitForSelector("input[name='password']");
await page.type("input[name='password']", configJSO.password, { delay: 50 });
// click on login3
await page.waitForSelector("button[data-analytics='LoginPassword']");
await page.click("button[data-analytics='LoginPassword']");
// click on compete
await page.waitForSelector("a[data-analytics='NavBarContests']");
await page.click("a[data-analytics='NavBarContests']");
// click on Manage contests
await page.waitForSelector("a[href='/administration/contests/']");
await page.click("a[href='/administration/contests/']");
// find pages
await page.waitForSelector("a[data-attr1='Last']");
let numPages = await page.$eval("a[data-attr1='Last']", function (atag) {
let np = parseInt(atag.getAttribute('data-page'));
return np;
})
// move through all pages
for (let i = 0; i < numPages; i++) {
await handlePage(browser, page);
}
}
async function handlePage(browser, page) {
// do some code
await page.waitForSelector("a.backbone.block-center");
let curls = await page.$$eval("a.backbone.block-center", function (atags) {
// let urls = [];
// for (let i = 0; i < atags.length; i++) {
// let url = atags[i].getAttribute("href");
// urls.push(url);
// }
// let urls = atags.map(function(atag, i){
// return atag.getAttribute("href");
// });
// let urls = atags.map(atag => atag.getAttribute(href));
// return urls;
return atags.map(atag => atag.getAttribute(href));
});
for (let i = 0; i < curls.length; i++) {
await handleContest(browser, page, curls[i]);
}
// move to next page
await page.waitFor(1500);
await page.waitForSelector("a[data-attr1='Right']");
await page.click("a[data-attr1='Right']");
}
async function handleContest(browser, page, curl) {
let npage = await browser.newPage();
await npage.goto(args.url + curl);
await npage.waitFor(2000);
await npage.waitForSelector("li[data-tab='moderators']");
await npage.click("li[data-tab='moderators']");
for (let i = 0; i < configJSO.moderators.length; i++) {
let moderator = configJSO.moderators[i];
await npage.waitForSelector("input#moderator");
await npage.type("input#moderator", moderator, { delay: 50 });
await npage.keyboard.press("Enter");
}
await npage.waitFor(1000);
await npage.close();
await page.waitFor(2000);
}