diff --git a/crawler/casLogIn.js b/crawler/casLogIn.js index 51866f4..6b32a51 100644 --- a/crawler/casLogIn.js +++ b/crawler/casLogIn.js @@ -13,7 +13,7 @@ const ocr = require('./captcha') */ module.exports = async (school, user) => { // improve school campatibility with defaults and edge-cases - const schoolEdgeCases = require('./school-edge-cases')[school.name] || {} + const schoolEdgeCases = require('./school-edge-cases')(school.name) const headers = { 'Cache-control': 'max-age=0', @@ -44,7 +44,7 @@ module.exports = async (school, user) => { // create document for crawling const body = await res.text() const $ = cheerio.load(body) - const form = $('form[method=post]').get(schoolEdgeCases.formIdx || 0) + const form = $('form[method=post]').get(schoolEdgeCases.formIdx) const hiddenInputList = $('input[type=hidden]', form) // grab hidden input name-value, this maybe error-prone, but compatible @@ -66,10 +66,11 @@ module.exports = async (school, user) => { username: user.username, password: pwdSalt ? new AES(user.password, pwdSalt).encrypt() : user.password, ...hiddenInputNameValueMap, + rememberMe: schoolEdgeCases.rememberMe, }) // check captcha is needed - if (schoolEdgeCases.supportCaptcha || true) { + if (schoolEdgeCases.supportCaptcha) { res = await fetch(`${school.checkCaptcha}?username=${user.username}`, { headers, }) diff --git a/crawler/school-edge-cases.js b/crawler/school-edge-cases.js index 3935883..9ec3e37 100644 --- a/crawler/school-edge-cases.js +++ b/crawler/school-edge-cases.js @@ -1,9 +1,29 @@ // @ts-check -module.exports = { +const schoolEdgeCases = { 武汉轻工大学: { - formIdx: 2, // default 0 + formIdx: 2, }, 宁波大学: { - supportCaptcha: false, // default true + supportCaptcha: false, + rememberMe: 'on', }, } + +// we will using proxy to get the default properties +const defaultProps = { + rememberMe: true, + formIdx: 0, + supportCaptcha: true, +} + +module.exports = (schoolName) => + schoolName + ? new Proxy(schoolEdgeCases[schoolName], { + get(target, prop, receiver) { + if (target[prop] === undefined) { + return defaultProps[prop] + } + return Reflect.get(target, prop, receiver) + }, + }) + : {}