sisa-crawl/evaluateStartPakket.js

73 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-11-20 14:33:02 +00:00
const IFRAME_SELECTOR = 'iframe[title="Hoofdinhoud"]';
const CELL_SELECTOR =
'a[title="Sectie uitvouwen Algemene opleidingsonderdelen"]';
2024-11-20 14:33:02 +00:00
/**
* 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);
2024-11-20 14:33:02 +00:00
}
/**
* 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();
2024-11-20 14:33:02 +00:00
}
/**
* 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);
2024-11-20 14:33:02 +00:00
if (!cell) {
throw new Error(
"Cell inside function isStartPakketAvailable is not defined"
);
}
2024-11-20 14:33:02 +00:00
const isCollapsed = await isAriaExpandedFalse(cell);
2024-11-20 14:33:02 +00:00
console.log(`Aria-expanded: ${isCollapsed}`);
2024-11-20 14:33:02 +00:00
return {
isCollapsed: isCollapsed,
// Add more properties here in the future as needed
};
2024-11-20 14:33:02 +00:00
}
/**
* 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"
);
2024-11-20 14:33:02 +00:00
}
module.exports = isStartPakketAvailable;