MOOC Utils

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

Content scripts

Introduction#

Content scripts run in the page. This doc covers:

  • Extraction: find questions, parse HTML, structure question data
  • Applicator: write answers into controls and handle input types
  • Lifecycle and messaging with the background worker
  • DOM techniques and how this ties into the solve workflow

Project structure#

The content script is organized as a small module with three primary responsibilities:

  • Message routing and lifecycle management
  • Extraction of page HTML and associated images
  • Application of answers and submission actions

Core components#

  • Content script entry point initializes logging, creates extractor and applicator services, and registers a message listener for bidirectional communication with the background script.
  • Extractor service identifies assignment containers, extracts HTML, collects images, and discovers submit and confirmation button identifiers.
  • Applicator service applies answers to the DOM for single-choice, multiple-choice, and fill-in-the-blank question types, and triggers submission.
  • Platform adapters and core messaging types unify cross-browser behavior and message routing.

Architecture overview#

The content script runs on matching pages and communicates with the background script via a well-defined set of message types. The background script orchestrates content script injection when needed, forwards requests, and coordinates with external services.

Detailed component analysis#

Content script lifecycle and message routing#

  • Loaded automatically on matching URLs per manifest configuration.
  • Initializes logger, extractor, and applicator instances.
  • Registers a message listener that handles:
  • PING for health checks
  • GET_PAGE_HTML for HTML and image extraction
  • GET_PAGE_INFO for quick page metadata
  • SCROLL_INFO and SCROLL_TO for screenshot coordination
  • APPLY_ANSWERS and SUBMIT_ASSIGNMENT for answer application and submission
  • GEMINI_DEBUG for debug relaying

Extraction system#

The extractor locates assignment content, captures images, and discovers UI identifiers for submission and confirmation dialogs.

Key behaviors:

  • Container discovery: Iterates through a curated list of selectors targeting assignment areas.
  • Fallback: Uses main content containers if specific assignment containers are not found.
  • Image extraction: Filters out tiny or unloaded images, supports data URLs and canvas-based conversion, and attaches context via nearest question container.
  • Identifier discovery: Finds submit and confirmation button IDs for reliable automation.

Applicator system#

The applicator applies answers to the DOM based on question type and dispatches appropriate events to trigger change handlers.

Supported question types:

  • Single choice (radio): Attempts lookup by ID, value, name pattern, and partial ID match; clicks and emits change.
  • Multi choice (checkbox): Identifies checkboxes by name/partial ID, toggles checked state as needed, and emits change.
  • Fill in the blank: Targets input or textarea by ID/name/partial ID; sets value and emits input, change, and keyup.

Submission:

  • Locates submit button by ID, generic selectors, or onclick attributes; clicks to submit.

Background script integration and cross-browser compatibility#

  • The background script manages message routing, tab targeting, and content script injection when needed.
  • It uses platform adapters to abstract browser differences and keeps reliable message delivery with retry logic where applicable.
  • The content script uses webextension-polyfill for cross-browser compatibility.

Dependency analysis#

  • Content script depends on:
  • Logger factory for structured logging
  • Extractor for DOM parsing and image collection
  • Applicator for answer application and submission
  • Platform browser polyfill for cross-browser compatibility
  • Core message types for consistent communication
  • Background script depends on:
  • Platform adapters for tabs and scripting APIs
  • Message router for centralized message handling
  • Handlers for extraction, answers, and page info

Performance considerations#

  • Selector traversal: The extractor iterates through a fixed set of selectors and falls back to broader elements only when necessary. This minimizes unnecessary DOM queries.
  • Image processing: Canvas-based conversion is attempted only for non-data URLs; external images that fail due to CORS are skipped to avoid blocking the pipeline.
  • Event dispatching: Applicator emits minimal DOM events (change, input, keyup) to trigger handlers without excessive reflows.
  • Injection timing: Background handlers wait briefly for Firefox initialization to reduce race conditions during content script injection.

Security considerations#

  • Content script scope: Runs only on trusted domains defined in the manifest and uses CSP to restrict connections.
  • Cross-origin images: Canvas-based conversion is guarded against CORS errors; such images are skipped to prevent security exceptions.
  • Message boundaries: All communication is mediated by the background script and platform adapters, reducing direct exposure of internal APIs.
  • Host permissions: Permissions are scoped to NPTEL domains and the Gemini API endpoint.

Troubleshooting guide#

Common issues and resolutions:

  • Content script not responding:
  • The background handler pings the content script before sending messages; if it fails, the handler injects the content script and retries.
  • Ensure the page matches the content script’s URL patterns.
  • Missing submit or confirmation buttons:
  • The extractor and applicator rely on specific IDs and selectors; if custom themes alter these, manual override may be needed.
  • CORS errors on images:
  • Images from external domains that fail CORS are skipped; this prevents exceptions and allows extraction to continue.
  • Firefox initialization delays:
  • The background handler waits longer for Firefox so the content script is ready before sending messages.

Conclusion#

Content scripts own the DOM. Everything else should ask them, not scrape from the background.