MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

UI elements library

Introduction#

Building blocks used in the side panel UI, plus related website UI helpers where they overlap.

Project structure#

The UI systems are organized as follows:

  • Browser extension side panel:
  • UI entry point initializes controllers and wiring.
  • Element registry centralizes DOM queries.
  • Controllers encapsulate UI logic and state.
  • Utilities provide escaping and formatting helpers.
  • Website (Next.js):
  • Shared utility for Tailwind class merging.
  • Reusable React UI primitives (Button, Input, Dialog, Card).
  • Example page-level component demonstrating composition.

Core components#

  • Element factory pattern: Centralized DOM retrieval via a single function returning a map of element references. Centralizing DOM queries keeps tests from scraping the whole panel.
  • State manager: Encapsulated state with getters/setters and reset capability, enabling predictable UI state transitions.
  • Controller factory pattern: Each controller exposes initialization and lifecycle methods, promoting layer boundaries and DI-friendly wiring.
  • Utilities:
  • HTML escaping for safe innerHTML insertion.
  • Question type formatting for display.
  • Tailwind class merging utility for React components.

Examples of element creation and dynamic updates are covered in the detailed component analysis.

Architecture overview#

The extension UI follows a dependency injection pattern:

  • UI entry initializes adapters, services, state, and controllers.
  • Controllers receive shared dependencies (elements, state, storage, runtime, logger).
  • Event listeners are bound in controller initialization methods.
  • Controllers update the DOM via the element registry and manage progress/status.

Detailed component analysis#

Element factory pattern and DOM selection strategies#

  • Centralized element retrieval: All DOM queries are consolidated in a single function returning a named map of elements. This reduces duplication and makes refactoring easier.
  • Safe nested queries: Uses chained getElementById and querySelector to access nested nodes (e.g., status text inside status bar).
  • Modal and form elements: Captures inputs, selects, and buttons for settings and progress UI.
  • Selection strategies:
  • Prefer getElementById for unique identifiers.
  • Use querySelector for scoped children to avoid global conflicts.
  • Guard against missing elements to prevent runtime errors.

State management#

  • Encapsulated state with explicit getters/setters and reset.
  • Tracks processing flag, extraction object, and current step.
  • Used by controllers to coordinate UI updates and guard concurrent operations.

Progress controller#

  • Updates status bar text and styling based on status type (normal/loading/error).
  • Manages step indicators via data attributes.
  • Controls progress bar count, width, and determinate/indeterminate modes.
  • Toggles visibility of progress/results/empty states.

Settings controller#

  • Loads stored API key and model preferences into form controls.
  • Shows/hides modal with transitions.
  • Saves settings to storage and validates presence of required fields.
  • Initializes event listeners for opening/closing and saving.

Solve controller#

  • Orchestrates the full flow: extract HTML/screenshots, AI extraction, AI solving, filling answers, optional auto-submit, and results rendering.
  • Implements recursive splitting on MAX_TOKENS errors to handle long inputs.
  • Uses progress controller for status and progress updates.
  • Uses utilities for HTML escaping and question type formatting.
  • Relays debug info to background via messaging.

Detection controller#

  • Checks current page for assignments and toggles UI states accordingly.
  • Listens for background tab update events to refresh detection.

Website UI components and utilities#

  • Tailwind utility: Merges and deduplicates Tailwind classes safely.
  • Button component: Variants and sizes with Base UI primitive and class variance authority.
  • Input component: Base UI input with consistent styling and accessibility props.
  • Dialog component: Portal-backed overlay with optional close button and slots for header/footer/title/description.
  • Card component: Flexible card with header/content/footer/title/action and size variants.
  • Hero page component: composes UI primitives in a responsive layout.

Utility functions for UI helpers#

  • HTML escaping: Prevents XSS when injecting dynamic text into the DOM.
  • Question type formatting: Maps internal types to display-friendly abbreviations.

Event handling and user interaction management#

  • Event delegation and listener initialization are centralized in controller init methods.
  • Modal open/close/save logic stays out of the content render path.
  • Dynamic UI updates (progress bars, status text, step indicators) occur in response to controller actions.

Styling approaches and responsive design patterns#

  • Extension UI:
  • Uses CSS variables for accent colors and status dot styling.
  • Applies classes conditionally to reflect loading/error states.
  • Progress bar width and pulse animation are toggled dynamically.
  • Website UI:
  • Tailwind utilities for responsive spacing, typography, and layout.
  • Component-level variants (size/variant) and data attributes for styling hooks.
  • Responsive breakpoints and fluid typography via clamp and grid layouts.

Accessibility considerations#

  • Focus management and keyboard navigation are supported by underlying Base UI primitives.
  • Semantic roles and labels are applied via data-slot attributes for assistive technologies.
  • Proper contrast and color usage for status indicators (success/warning/error).
  • Hidden elements and transitions are handled to preserve screen reader semantics.

Examples of element creation, event binding, and dynamic UI updates#

  • Element creation: Centralized in element registry; controllers consume the map.
  • Event binding: Controllers initialize listeners in dedicated methods.
  • Dynamic UI updates: Progress controller updates text, classes, and progress bar; solve controller renders results with escaped HTML.

Dependency analysis#

  • UI entry depends on adapters/services/state and controllers.
  • Controllers depend on the element registry and shared services.
  • Utilities are pure functions injected into controllers where needed.
  • Website components depend on the Tailwind utility for class composition.

Performance considerations#

  • Minimize DOM queries: Use the element registry to fetch once and reuse.
  • Batch UI updates: Group DOM writes and avoid forced synchronous layouts.
  • Debounce or throttle rapid events (e.g., progress updates) to reduce repaint cost.
  • Conditional animations: Toggle CSS classes instead of animating properties in JS loops.
  • Recursive splitting: Limit maximum depth to prevent exponential work and stack growth.
  • Message retries: Use retry mechanisms with exponential backoff for background communication.
  • Virtualization: For large lists, consider virtualizing results rendering.

Troubleshooting guide#

  • Missing elements: Guard DOM queries and log warnings when elements are absent.
  • Background readiness: Wait for background script readiness before proceeding, especially on Firefox.
  • Error propagation: Surface errors to UI with setStatus and optionally relay to background for debugging.
  • Storage failures: Validate required settings (API key) before starting flows.
  • Cross-tab operations: Pin target tab ID early so subsequent messages route correctly.

Conclusion#

Reuse the small element helpers before inventing new markup. Visual consistency beats one-off CSS.

Appendices#

  • Cross-browser compatibility:
  • Use Base UI primitives for standardized behavior.
  • Feature-detect and polyfill where necessary (e.g., message passing APIs).
  • Test modal overlays and transitions across browsers.
  • Accessibility checklist:
  • Ensure focus order and visible focus indicators.
  • Provide ARIA labels and roles where custom elements are used.
  • Maintain sufficient color contrast for status indicators.