-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
120 additions
and
147 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,79 @@ | ||
import patcher from '@core/patcher'; | ||
import bookwork from '@core/bookwork'; | ||
import utilities from '@utilities'; | ||
import { storages } from '@core/handlers/state'; | ||
|
||
const { storeAnswers } = bookwork; | ||
const { findReact, lazyModule } = utilities; | ||
const { findReact, findInReactTree, lazyDefine, isEmpty } = utilities; | ||
const { bookwork } = storages; | ||
|
||
function processAnswers(input: Record<string, Record<string, any>>) { | ||
const answers = []; | ||
|
||
if (!isEmpty(input.number_fields)) { | ||
Object.values(input.number_fields).forEach(field => | ||
field.value && answers.push(field.value)); | ||
} | ||
|
||
if (!isEmpty(input.cards)) { | ||
Object.values(input.cards).forEach(card => | ||
card.slot_ref && answers.push(card.content[0].text)) | ||
} | ||
|
||
if (!isEmpty(input.choices)) { | ||
Object.values(input.choices).forEach(choice => | ||
choice.selected && answers.push(choice.content[0].text)) | ||
} | ||
|
||
return answers; | ||
} | ||
|
||
function handler() { | ||
const possibleQuestionWrapper = document.querySelector('[class*="_QuestionWrapper_"]'); | ||
const possibleQuestionInfo = document.querySelector('[class*="_QuestionInfo_"]'); | ||
|
||
if (!possibleQuestionWrapper || !possibleQuestionInfo) { | ||
console.warn('Wrappers failed to query:', { possibleQuestionWrapper, possibleQuestionInfo }); | ||
return; | ||
} | ||
|
||
const QuestionWrapper = findReact(possibleQuestionWrapper); | ||
const QuestionInfo = findReact(possibleQuestionInfo); | ||
|
||
const code = QuestionInfo.memoizedProps.bookworkCode; | ||
|
||
const endpoint = findInReactTree(QuestionWrapper.memoizedProps.children, x => x.layout && x.input); | ||
const answer = processAnswers(endpoint.input); | ||
|
||
if (!code || !answer) { | ||
console.warn('Answer failed to parse:', { code, answer }); | ||
return; | ||
} | ||
|
||
bookwork.set(code, answer); | ||
} | ||
|
||
const conditions = (event) => { | ||
return [ | ||
event.target.className.includes('_Content_10evl_'), | ||
event.target.className.includes('_TextFieldNumeric_'), | ||
event.key === 'Enter' | ||
] | ||
} | ||
|
||
export default async function () { | ||
const screenNode = await lazyModule(() => document.querySelector('.screen')); | ||
const SparxWeb = findReact(screenNode); | ||
const page = await lazyDefine(() => document.querySelector('[id="root"]'), undefined, Infinity); | ||
|
||
// This will adapt whenever SparxWeb re-renders | ||
let dynamicSubmitButton: Element | null; | ||
console.log("Found page:", page); | ||
|
||
// Listen for Enter keypresses and store the answer when the dynamicSubmitButton exists (is in scope) | ||
document.addEventListener('keypress', function (event) { | ||
event.key === 'Enter' && dynamicSubmitButton && storeAnswers(); | ||
}) | ||
const storeAnswers = (event) => { | ||
if (conditions(event).some(c => c)) { | ||
handler(); | ||
} | ||
} | ||
|
||
// Assigns `storeAnswers` on click to Submit button on every SparxWeb render | ||
// This then captures the answer(s) in the input whenever the button does exist | ||
patcher.after('render', SparxWeb, function () { | ||
dynamicSubmitButton = document.querySelector('#skill-delivery-submit-button'); | ||
page.addEventListener('click', storeAnswers); | ||
page.addEventListener('keydown', storeAnswers); | ||
|
||
dynamicSubmitButton?.removeEventListener('click', storeAnswers); | ||
dynamicSubmitButton?.addEventListener('click', storeAnswers); | ||
}); | ||
return () => { | ||
page.removeEventListener('click', storeAnswers); | ||
page.removeEventListener('keydown', storeAnswers); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import bookworkBypass from './bookworkBypass'; | ||
import captureAnswers from './captureAnswers'; | ||
import menuButtons from './menuButtons'; | ||
import captureAnswers from './captureAnswers'; | ||
import bookworkBypass from './bookworkBypass'; | ||
|
||
const patches = Promise.allSettled([ | ||
// await bookworkBypass(), | ||
// await captureAnswers(), | ||
menuButtons() | ||
menuButtons(), | ||
captureAnswers(), | ||
bookworkBypass() | ||
]) | ||
|
||
export default patches; |
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,14 @@ | ||
/** | ||
* @description Checks if an object is empty or not | ||
* @param {object} object - The object to check | ||
* @return {boolean} | ||
*/ | ||
function isEmpty(object: Record<any, any>) { | ||
for (const _ in object) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export default isEmpty; |
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
Oops, something went wrong.