박시진

[Add]Remark [Modify]connection information

1 const puppeteer = require("puppeteer"); 1 const puppeteer = require("puppeteer");
2 -function login(id, pw) { 2 +//use puppeteer
3 - const crawler = async() => { 3 +
4 +function setTimeoutPromise(ms) {
5 + return new Promise((resolve, reject) => {
6 + setTimeout(() => resolve(), ms);
7 + });
8 +}
9 +
10 +async function loglogin(id, pw) {
4 try{ 11 try{
12 + //for visibility, headless: false
5 const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); 13 const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
14 +
6 const page = await browser.newPage(); 15 const page = await browser.newPage();
7 await page.setViewport({ 16 await page.setViewport({
8 width:1920, 17 width:1920,
...@@ -11,19 +20,32 @@ function login(id, pw) { ...@@ -11,19 +20,32 @@ function login(id, pw) {
11 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'); 20 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');
12 await page.type("#login_user_id", id); 21 await page.type("#login_user_id", id);
13 await page.type("#login_user_password", pw); 22 await page.type("#login_user_password", pw);
23 +
24 + //press the login button on E_Campus homepage
14 await page.click('#form1 > div > div.login_btn > a > span'); 25 await page.click('#form1 > div > div.login_btn > a > span');
26 + await page.waitForTimeout(500);
27 + await setTimeoutPromise(1000);
28 +
29 + //When login is failed, return [id, pw, message] -> ["", "", "Incorrect~"]
15 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'){ 30 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'){
16 - return "Incorrect user ID or password." 31 + return ["", "", "Incorrect user ID or password."];
17 } 32 }
18 else { 33 else {
19 - return `I got data { id:"${id}" pw:"${pw}" }`; 34 + return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`];
20 - } 35 + }
21 }catch(err){ 36 }catch(err){
22 console.log(err) 37 console.log(err)
23 } 38 }
39 +}
40 +async function login(id, pw) {
41 + await setTimeoutPromise(1000);
42 + try {
43 + // value has the list ["","","Incorrect~"] or [id, pw, "I got data~"]
44 + const value = await loglogin(id, pw);
45 + console.log(value[2]);
46 + } catch (e) {
47 + console.error(e);
24 } 48 }
25 -
26 - crawler();
27 } 49 }
28 50
29 export { login }; 51 export { login };
......
1 -const puppeteer = require("puppeteer"); 1 +//The prototype
2 +// const puppeteer = require("puppeteer");
3 +// const crawler = async() => {
4 +// try{
5 +// const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
6 +// // for observing the process of login, headless -> false
7 +// const page = await browser.newPage();
8 +// await page.setViewport({
9 +// width:1920,
10 +// height:1080
11 +// })
12 +// 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');
13 +// await page.evaluate(() => {
14 +// document.querySelector("#login_user_id").value = //자신의 E_Campus account id
15 +// document.querySelector("#login_user_password").value = // pw
16 +// document.querySelector('#form1 > div > div.login_btn > a > span').click();
17 +// })
18 +// }catch(err){
19 +// console.log(err)
20 +// }
21 +// }
22 +// crawler();
2 23
3 -const crawler = async() => { 24 +
25 +//The test on nodejs (evaluate version for using input data)
26 +const puppeteer = require("puppeteer");
27 +//Example for not using user_input_login_information
28 +const id = "lorem";
29 +const pw = "lorem";
30 +const informationList = new Object();
31 +
32 +function setTimeoutPromise(ms) {
33 + return new Promise((resolve, reject) => {
34 + setTimeout(() => resolve(), ms);
35 + });
36 +}
37 +
38 +async function loglogin(id, pw) {
4 try{ 39 try{
5 const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); 40 const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
6 - // for observing the process of login, headless -> false
7 const page = await browser.newPage(); 41 const page = await browser.newPage();
8 await page.setViewport({ 42 await page.setViewport({
9 width:1920, 43 width:1920,
10 height:1080 44 height:1080
11 }) 45 })
12 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'); 46 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');
13 - await page.evaluate(() => { 47 + await page.type("#login_user_id", id);
14 - document.querySelector("#login_user_id").value = //자신의 E_Campus account id 48 + await page.type("#login_user_password", pw);
15 - document.querySelector("#login_user_password").value = // pw 49 + await page.click('#form1 > div > div.login_btn > a > span');
16 - document.querySelector('#form1 > div > div.login_btn > a > span').click(); 50 + await page.waitForTimeout(500);
17 - }) 51 + await setTimeoutPromise(1000);
18 52
53 + 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'){
54 + return ["", "", "Incorrect user ID or password."];
55 + }
56 + else {
57 + return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`];
58 + }
19 }catch(err){ 59 }catch(err){
20 console.log(err) 60 console.log(err)
21 } 61 }
22 } 62 }
63 +async function login(id, pw) {
64 + await setTimeoutPromise(1000);
65 + try {
66 + const value = await loglogin(id, pw);
67 + // promise1.then
68 + console.log("Id : "+ value[0] +", password : " + value[1] + ", message : " + value[2]);
69 + return value[2];
70 +
71 +
72 + } catch (e) {
73 + console.error(e);
74 + }
75 +}
76 +
77 +login(id, pw);
78 +//[id : , pw : , message : "Incorrect user ID or password."]
23 79
24 -crawler();
...\ No newline at end of file ...\ No newline at end of file
......