This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9b69c0
commit f270bd4
Showing
7 changed files
with
105 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
CommonPage, | ||
SigaaForm, | ||
CommonSigaaPage, | ||
SigaaPageConstructor | ||
} from '@session/sigaa-page'; | ||
import { URL } from 'url'; | ||
|
||
/** | ||
* @category Internal | ||
*/ | ||
export interface UNILABPage extends CommonPage { | ||
/** | ||
* Extracts the javascript function JSFCLJS from the page, | ||
* this function on the page redirects the user to another | ||
* page using the POST method, often this function is in | ||
* the onclick attribute in some element. | ||
* @param javaScriptCode | ||
* @returns Object with URL action and POST values equivalent to function | ||
*/ | ||
parseJSFCLJS(javaScriptCode: string): SigaaForm; | ||
} | ||
|
||
/** | ||
* Response page of sigaa. | ||
* @category Internal | ||
*/ | ||
export class SigaaPageUNILAB extends CommonSigaaPage { | ||
constructor(options: SigaaPageConstructor) { | ||
super(options); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
parseJSFCLJS(javaScriptCode: string): SigaaForm { | ||
if (!javaScriptCode.includes('getElementById')) | ||
throw new Error('SIGAA: Form not found.'); | ||
|
||
const formQuery = javaScriptCode.match( | ||
/document\.getElementById\('(\w+)'\)/ | ||
); | ||
if (!formQuery) throw new Error('SIGAA: Form without id.'); | ||
|
||
const formEl = this.$(`#${formQuery[1]}`); | ||
if (!formEl) { | ||
throw new Error('SIGAA: Form not found.'); | ||
} | ||
|
||
const formAction = formEl.attr('action'); | ||
if (formAction === undefined) | ||
throw new Error('SIGAA: Form without action.'); | ||
|
||
const action = new URL(formAction, this.url); | ||
const postValues: Record<string, string> = {}; | ||
|
||
formEl.find("input:not([type='submit'])").each((_, element) => { | ||
const name = this.$(element).attr('name'); | ||
const value = this.$(element).val(); | ||
if (name !== undefined) { | ||
postValues[name] = value; | ||
} | ||
}); | ||
const formPostValuesString = `{${javaScriptCode | ||
.replace(/if([\S\s]*?),{|},([\S\s]*?)false/gm, '') | ||
.replace(/"/gm, '\\"') | ||
.replace(/'/gm, '"')}}`; | ||
return { | ||
action, | ||
postValues: { | ||
...postValues, | ||
...JSON.parse(formPostValuesString) | ||
} | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters