sisa-crawl/evaluateStartPakket.js
2024-11-20 15:33:02 +01:00

67 lines
2.1 KiB
JavaScript

const IFRAME_SELECTOR = 'iframe[title="Hoofdinhoud"]';
const CELL_SELECTOR = 'a[title="Sectie uitvouwen Algemene opleidingsonderdelen"]';
/**
* Extracted function to get the text content of a node.
* @param {ElementHandle} node - The node to get the text content from.
* @returns {Promise<string>} The text content of the node.
*/
async function getNodeTextContent(node) {
return await node.evaluate(node => node.textContent);
}
/**
* Extracted function to retrieve the content frame.
* @param {Page} page - The Puppeteer page object.
* @returns {Promise<Frame>} The content frame.
*/
async function getContentFrame(page) {
await page.waitForSelector(IFRAME_SELECTOR);
const iframe = await page.$(IFRAME_SELECTOR);
if (!iframe) {
throw new Error('Could not find iframe on the AA-page');
}
console.log('iframe found on AA-page')
return await iframe.contentFrame();
}
/**
* Checks if the 'Startpakket' is available by verifying text content in a cell
* and the `aria-expanded` attribute.
*
* @param {Page} page - The Puppeteer page object.
* @returns {Promise<boolean>} True if the 'Startpakket' is available, false otherwise.
*/
async function isStartPakketAvailable(page) {
const contentFrame = await getContentFrame(page);
await contentFrame.waitForSelector(CELL_SELECTOR);
const cell = await contentFrame.$(CELL_SELECTOR);
console.log(cell);
if (!cell) {
console.error('Cell not found');
return false;
}
const isCollapsed = await isAriaExpandedFalse(cell);
console.log(`Aria-expanded: ${isCollapsed}`);
return isCollapsed;
}
/**
* Checks if the given cell node has an `aria-expanded` attribute with a value of 'false'.
*
* @param {ElementHandle} node - The node to check the `aria-expanded` attribute.
* @returns {Promise<boolean>} True if the `aria-expanded` attribute is 'false', false otherwise.
*/
async function isAriaExpandedFalse(node) {
if (!node) {
throw new Error('Node is not defined');
}
return await node.evaluate(element => element.getAttribute('aria-expanded') === 'false');
}
module.exports = isStartPakketAvailable ;