MOOC Utils

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

Extension lifecycle management

Introduction#

How icon clicks open the side panel, how the service worker registers runtime listeners, how the UI boots against the background, and how content scripts bridge the page DOM. Cross-browser quirks and panel config are included.

Project structure#

Extension layers:

  • Background service worker orchestrates messaging, panel behavior, and runtime listeners
  • Platform adapters abstract browser APIs for cross-browser compatibility
  • UI handles initialization, event binding, and user feedback
  • Content scripts extract page data and apply answers to forms
  • Handlers process messages from the UI and content scripts

Core components#

  • Background service worker: Initializes adapters, registers message handlers, sets up browser action click listener, and configures panel behavior.
  • Panel adapter: Unifies Chrome sidePanel and Firefox sidebarAction APIs for opening/closing panels and setting behavior.
  • Runtime adapter: Wraps browser.runtime for cross-browser messaging.
  • Tabs and Scripting adapters: Manage tab queries, content script injection, and tab-specific messaging.
  • Message router: Centralized handler dispatch with proper async response handling for Firefox.
  • UI entry point: Waits for background readiness, initializes controllers, and binds UI events.
  • Content script: Listens for messages, extracts page data, applies answers, and submits assignments.
  • Handlers: Implement specific tasks like HTML extraction, screenshot capture, and answer application.

Architecture overview#

The extension uses a unidirectional messaging model:

  • UI sends requests to the background via runtime.sendMessage
  • Background routes messages to appropriate handlers
  • Handlers may query tabs, inject content scripts, or call services
  • Content scripts respond directly to UI requests for page data
  • Panel behavior is configured per browser

Detailed component analysis#

Browser action integration and panel opening#

  • The background listens for browser action clicks and opens the panel via the panel adapter.
  • Panel behavior is configured for Chrome to open the panel on action click.
  • The panel adapter abstracts differences between Chrome sidePanel and Firefox sidebarAction.

Panel behavior configuration (chrome)#

  • The background sets panel behavior to open on action click for Chrome.
  • Firefox does not expose a direct close API for sidePanel; closing is handled implicitly.

Runtime event listeners and message routing#

  • The background registers a message router that dispatches to handlers based on message type.
  • Handlers return promises and ensure sendResponse is called to avoid hanging message ports.
  • Firefox requires returning true synchronously to keep the message channel open for async responses.

UI initialization and health checks#

  • The side panel waits for the background to be ready using a PING mechanism with exponential backoff.
  • Controllers initialize event listeners and bind UI actions to background handlers.

Content script extraction and answer application#

  • The content script listens for messages and performs page extraction, scrolling info retrieval, and answer/application/submission.
  • It responds to Gemini debug messages and logs to the page console for visibility.

Cross-Browser compatibility and API detection#

  • The browser adapter detects Chrome vs Firefox and exposes safe API accessors.
  • Panel adapter checks availability of sidePanel/sideBarAction APIs and falls back gracefully.

Relationship between background events and UI triggers#

  • UI triggers (solve, settings, detection) send messages to the background.
  • Background handlers may inject content scripts, query tabs, and coordinate with content scripts.
  • UI listens for tab updates and re-detects assignments when the background signals.

Dependency analysis#

The extension has strong modularity with clear dependency boundaries:

  • Background depends on platform adapters and message router
  • UI depends on runtime adapter and controllers
  • Content script depends on extractor and applicator
  • Handlers depend on tabs/scripting adapters and services

Performance considerations#

  • Message retries: The UI uses a retry mechanism with exponential backoff for transient connection failures, particularly important for Firefox.
  • Content script injection delays: The background waits for content scripts to initialize before proceeding, with extra delays for Firefox.
  • Token limit handling: The solve controller splits large inputs recursively to avoid MAX_TOKENS errors, merging results afterward.
  • Progress reporting: UI shows determinate progress where possible and indeterminate progress for long-running steps.

Troubleshooting guide#

Common issues and their handling:

  • Background not ready: The UI performs health checks with retries; if unsuccessful, it warns and continues.
  • Content script injection failures: The background attempts injection and verifies responsiveness; errors suggest refreshing the page.
  • Panel open/close failures: Panel adapter logs errors and throws exceptions; Firefox lacks a direct close API.
  • Message port closures: Router still calls sendResponse and keeps channels open for Firefox-compatible async responses.
  • Gemini debug relay: UI relays debug payloads to the background and content script; failures are logged and ignored to prevent blocking.

Conclusion#

Icon click opens the panel; listeners and content-script injection keep the page side alive. Failures usually mean the panel never got a ready ping.