MOOC Utils

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

Core modules

Introduction#

Map of the extension’s core layers: background worker, content scripts, UI controllers, and shared utilities. Use this as the table of contents for the deeper module docs.

Project structure#

The extension is organized into distinct layers:

  • Background service worker orchestrates messaging, platform adapters, and service integrations.
  • Content scripts run on target pages to extract DOM content and apply answers.
  • UI controllers manage state, progress, settings, and detection workflows.
  • Services encapsulate AI model interactions and parsing.
  • Platform adapters abstract browser APIs for cross-browser compatibility.
  • Core utilities define shared types, messages, and helpers.

Core components#

  • Background service worker initializes platform adapters, services, and registers the message router. It exposes handlers for extraction, screenshots, Gemini requests, answer application, and submission.
  • Content scripts provide DOM extraction and answer application, responding to messages from the background.
  • UI controllers manage state, progress, settings, and detection. They coordinate the solve flow and interact with the background via message protocols.
  • Services encapsulate AI model interactions, including extraction and solving, with reliable retry and error handling.
  • Platform adapters abstract browser APIs for cross-browser compatibility.
  • Core utilities define shared types, message constants, and helper functions for message sending with retry logic.

Architecture overview#

The extension follows a message-driven architecture:

  • The background service worker listens for UI-triggered actions and routes them to appropriate handlers.
  • Handlers interact with platform adapters and services, and may forward messages to content scripts.
  • Content scripts execute in-page DOM operations and respond to messages with extracted data or applied answers.
  • UI controllers orchestrate the full solve workflow, updating state and progress.

Detailed component analysis#

Background service worker and message routing#

  • Initializes platform adapters and services.
  • Creates handlers for PING, EXTRACT_HTML, GET_PAGE_INFO, CAPTURE_FULL_PAGE, GEMINI_REQUEST, GEMINI_DEBUG, APPLY_ANSWERS, and SUBMIT_ASSIGNMENT.
  • Registers a router that dispatches messages to handlers and waits for async completion before responding.
  • Sets up extension action click to open the side panel and panel behavior for Chrome.

Message protocols and utilities#

  • Defines MESSAGE_TYPES for content/background communication and internal messages.
  • Provides createMessage for constructing typed messages.
  • sendMessageWithRetry adds reliable retry logic for transient connection failures, especially important for Firefox.

Content scripts: DOM extraction and answer application#

  • Content script listens for messages and performs:
  • PING health checks.
  • GET_PAGE_HTML extraction via extractor service.
  • GET_PAGE_INFO quick page info for assignment detection.
  • SCROLL_INFO and SCROLL_TO for screenshot capture.
  • APPLY_ANSWERS and SUBMIT_ASSIGNMENT via applicator service.
  • GEMINI_DEBUG console logging for debugging.
  • Extractor locates assignment containers, extracts HTML and images, and finds submit/confirmation button IDs.
  • Applicator applies answers to radio buttons, checkboxes, and text inputs, and triggers submission.

UI components: state management, controllers, and user interface elements#

  • State manager holds processing state, extraction result, and current step.
  • Progress controller manages status messages, step indicators, and progress bars.
  • Settings controller handles API key and model preference persistence and UI binding.
  • Detection controller checks current page for assignments and toggles UI visibility.
  • Solve controller coordinates the full workflow: extract, capture screenshots, call Gemini for extraction and solving, apply answers, and optionally submit.

Core utilities: types and logging systems#

  • Shared types define ExtractedQuestion, PageData, Screenshot, ExtractionResult, and the Logger interface for consistent typing across modules.
  • Logger instances are passed through adapters and handlers for consistent logging.

Platform adapters and cross-browser compatibility#

  • Browser adapter exports a unified browser API and provides helper functions to detect browser type and safely access optional APIs.
  • Runtime adapter wraps browser.runtime for cross-browser messaging.

Service workers lifecycle and extension integration#

  • The background worker registers message listeners and sets up the extension action to open the side panel.
  • Panel behavior is configured for Chrome to open the panel on action clicks.
  • The worker relays debug messages to content scripts for visibility in the page console.

Dependency analysis#

Module boundaries:

  • Background depends on platform adapters, services, and core message utilities.
  • Content scripts depend on extractor and applicator services plus core messages.
  • UI controllers depend on state, progress, settings, and runtime adapters.
  • Services depend on core messages and types.
  • Platform adapters are foundational and used across modules.

Performance considerations#

  • Message retries: sendMessageWithRetry mitigates transient connection issues, particularly in Firefox.
  • Asynchronous handler pattern: Handlers return promises and ensure sendResponse is called to prevent hanging channels.
  • Screenshot capture: Captures full-page screenshots to aid visual understanding; consider batching and rate limiting to reduce overhead.
  • Recursive splitting: The solve and extract controllers split content on MAX_TOKENS errors to stay within model limits, improving reliability at the cost of extra API calls.
  • DOM operations: Content scripts throttle answer application with small delays to improve stability.

Troubleshooting guide#

Common issues and remedies:

  • Content script not responding: The background extraction handler injects content scripts and verifies readiness with a PING message. If injection fails, the handler returns an error suggesting a page refresh.
  • Firefox message channel timeouts: Use sendMessageWithRetry and ensure handlers return true for asynchronous responses to keep channels open.
  • Gemini API failures: The Gemini service throws on non-OK responses and parses candidates; errors are relayed to the UI and logged.
  • Debugging: Use GEMINI_DEBUG messages to relay structured payloads to the page console for inspection.

Conclusion#

Extend at the edges: new handlers, new controllers, new selectors. Avoid folding platform logic into the Gemini service.