E_Campus.js
1.83 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
const puppeteer = require("puppeteer");
//use puppeteer
function setTimeoutPromise(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms);
});
}
async function loglogin(id, pw) {
try{
//for visibility, headless: false
const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
const page = await browser.newPage();
await page.setViewport({
width:1920,
height:1080
})
await page.goto('https://e-campus.khu.ac.kr/xn-sso/login.php?auto_login=&sso_only=&cvs_lgn=&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Fxn-sso%2Fgw-cb.php%3Ffrom%3D%26login_type%3Dstandalone%26return_url%3Dhttps%253A%252F%252Fe-campus.khu.ac.kr%252Flogin%252Fcallback');
await page.type("#login_user_id", id);
await page.type("#login_user_password", pw);
//press the login button on E_Campus homepage
await page.click('#form1 > div > div.login_btn > a > span');
await page.waitForTimeout(500);
await setTimeoutPromise(1000);
//When login is failed, return [id, pw, message] -> ["", "", "Incorrect~"]
if(page.url() === 'https://e-campus.khu.ac.kr/xn-sso/gw-cb.php?from=&login_type=standalone&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Flogin%2Fcallback'){
return ["", "", "Incorrect user ID or password."];
}
else {
return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`];
}
}catch(err){
console.log(err)
}
}
async function login(id, pw) {
await setTimeoutPromise(1000);
try {
// value has the list ["","","Incorrect~"] or [id, pw, "I got data~"]
const value = await loglogin(id, pw);
console.log(value[2]);
} catch (e) {
console.error(e);
}
}
export { login };