Skip to content

Instantly share code, notes, and snippets.

@allain
Last active May 23, 2020 18:14
Show Gist options
  • Save allain/29b97bc28a7da1182d1e0ddb1021dd75 to your computer and use it in GitHub Desktop.
Save allain/29b97bc28a7da1182d1e0ddb1021dd75 to your computer and use it in GitHub Desktop.
Script to auto-fill a form
function insertJquery() {
let loaded = false
if (loaded) return Promise.resolve(jQuery)
return new Promise(resolve => {
const scriptEl = document.createElement('script')
scriptEl.setAttribute('src', 'https://code.jquery.com/jquery-3.5.1.min.js')
scriptEl.setAttribute('integrity', "sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=")
scriptEl.setAttribute('crossorigin', 'anonymous')
scriptEl.onload = () => {
loaded = true
resolve(jQuery)
}
document.head.appendChild(scriptEl)
})
}
function sleep(ms) {
return new Promise(r => setTimeout(r, ms))
}
function $$(selector) {
async function sel(selector) {
const $ = await insertJquery()
let started = Date.now()
let el = null
while (!$(selector).length) {
await sleep(100)
if (Date.now() - started > 5000) {
throw new Error('could not find element for selector: ' + selector)
}
}
return $(selector)
}
return {
async click() {
(await sel(selector)).click()
},
async val(text) {
(await sel(selector)).val(`${text}`)
}
}
}
async function insertTeaching([year, code, creditValue, seniorityPoints]) {
await $$('#ctl00_mainContent_btnInsertRowEmploymentUO-btnInnerEl').click()
await $$('#ctl00_mainContent_txtAcademicYearUO-inputEl').val(year)
await $$('.x-grid-row-selected td:nth-child(2)').click()
await $$('#ctl00_mainContent_txtCourseCodeUO-inputEl').val(code)
await $$('.x-grid-row-selected td:nth-child(3)').click()
await $$('#ctl00_mainContent_txtCreditValue-inputEl').val(creditValue)
await $$('.x-grid-row-selected td:nth-child(4)').click()
await $$('#ctl00_mainContent_txtSeniorityPoints-inputEl').val(seniorityPoints)
}
async function main() {
const info = `
04-05 CRM2301B 3 1
06-07 CRM3301B 3 1
06-07 CRM4313A 3 1
07-08 CRM2303A 3 1
07-08 CRM4304A 3 1
07-08 CRM4304D 3 1
07-08 CRM1301B 3 1
07-08 CRM1301C 3 1
08-09 CRM1301E 3 1
08-09 CRM1301A 3 1
08-09 CRM1301F 3 1
08-09 CRM1301B 3 1
08-09 CRM3301A 3 1
08-09 CRM4304D 3 1
09-10 CRM1301D 3 1
09-10 CRM1301F 3 0
09-10 CRM4304E 3 0
10-11 CRM1301E 3 0
10-11 CRM4304D 3 0
10-11 CRM4304E 3 0
10-11 CRM4304F 3 0
10-11 CRM1301E 3 0
10-11 CRM4304D 3 0
11-12 CRM4304E 3 0
11-12 CRM4304F 3 0
11-12 CRM3301A 3 0
11-12 CRM3317B 3 0
11-12 CRM4304D 3 0
11-12 CRM4304D 3 0
12-13 CRM1301C 3 0
12-13 CRM3301A 3 0
12-13 CRM4304A 3 0
12-13 CRM4310C 3 0
12-13 CRM3301B 3 0
12-13 CRM3301C 3 0
13-14 CRM4304D 3 0
13-14 CRM1301C 3 0
13-14 CRM4304B 3 0
13-14 CRM4304C 3 0
13-14 CRM3301C 3 0
13-14 CRM4304D 3 0
14-15 CRM4304E 3 0
14-15 CRM4304F 3 0
14-15 CRM4310C 3 0
14-15 CRM1300D 3 0
15-16 CRM3317A 3 0
15-16 CRM4304C 3 0
15-16 CRM4304D 3 0
15-16 CRM4304F 3 0
15-16 CRM3317B 3 0
16-17 CRM1301B 3 0
16-17 CRM3317A 3 0
16-17 CRM4304C 3 0
16-17 CRM1301D 3 0
16-17 CRM4304F 3 0
`.trim().split(/\s+/)
while (info.length) {
const line = [
info.shift(), // year
info.shift(),// code
info.shift(),// credits
info.shift()// seniority
]
await insertTeaching(line)
await sleep(100)
}
}
await main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment