Advertisement
vasylmartyniv

GoIT Scroller Script

Jun 21st, 2025 (edited)
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.04 KB | Software | 0 0
  1. (() => {
  2.   /**
  3.    * Utility: Click a button matching a span text.
  4.    */
  5.   const clickButtonByText = (text) => {
  6.     const button = [...document.querySelectorAll("button")].find(
  7.       (btn) => btn.querySelector("span")?.innerText.trim() === text
  8.     );
  9.     button?.click();
  10.   };
  11.  
  12.   /**
  13.    * Utility: Click a button by selector if enabled.
  14.    */
  15.   const clickEnabledButton = (selector) => {
  16.     const btn = document.querySelector(`${selector}:not([disabled])`);
  17.     btn?.click();
  18.   };
  19.  
  20.   /**
  21.    * Quiz Interaction Steps
  22.    */
  23.   const actions = {
  24.     selectFirstRadioOption: () => {
  25.       const option = document.querySelector('input[type="radio"]:not([disabled])');
  26.       option?.click();
  27.     },
  28.  
  29.     checkAnswer: () => {
  30.       const button = [...document.querySelectorAll("button")].findLast(
  31.         (btn) => btn.querySelector("span")?.innerText.trim() === "Перевірити"
  32.       );
  33.       button?.click();
  34.     },
  35.  
  36.     revealAnswer: () => clickButtonByText("Дізнатися відповідь"),
  37.     nextSection: () => clickEnabledButton("#go-to-next-course-element"),
  38.     nextModule: () => clickButtonByText("До наступного модуля"),
  39.     goToPracticalTask: () => clickButtonByText("Перейти до практичного завдання"),
  40.     next: () => clickButtonByText("Далі"),
  41.     nextBlock: () => clickButtonByText("До наступного блоку"),
  42.     scrollToBottom: () => window.scrollTo(0, document.body.scrollHeight),
  43.   };
  44.  
  45.   /**
  46.    * Observer Callback
  47.    */
  48.   const handleMutations = () => {
  49.     actions.selectFirstRadioOption();
  50.     actions.revealAnswer();
  51.     actions.checkAnswer();
  52.     actions.nextSection();
  53.     actions.nextModule();
  54.     actions.goToPracticalTask();
  55.     actions.next();
  56.     actions.nextBlock();
  57.     actions.scrollToBottom();
  58.   };
  59.  
  60.   /**
  61.    * Start observing DOM changes
  62.    */
  63.   const observer = new MutationObserver(handleMutations);
  64.   observer.observe(document.body, { childList: true, subtree: true });
  65.  
  66.   console.log("Quiz automation started.");
  67. })();
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement
OSZAR »