How ruiner28/leethub works
Project Type and Purpose
• This is a browser extension built for Google Chrome (and Firefox, as indicated by the README) that acts as a bridge between LeetCode and GitHub. The main purpose is to detect when a user has solved a problem on LeetCode and automatically push the relevant code to a GitHub repository.
• The extension handles authentication with GitHub (via OAuth2) and interacts with the DOM of LeetCode pages to extract code, which it then commits using GitHub APIs.
File Structure
• Key Configuration/Manifest Files:
• manifest.json: This file defines the Chrome extension’s settings, permissions, and background scripts.
• package.json and package-lock.json: Manage npm dependencies and scripts.
• .eslintrc, .prettierrc, and .gitignore: Enforce code style and ignore unnecessary files.
• Assets and UI:
• welcome.html & css/welcome.css: Another UI component for onboarding or instructions.
• Scripts:
• Several scripts in the "scripts" directory, including authorize.js (for GitHub OAuth), background.js (to handle background events), and domain-specific scripts (leetcode.js, gfg.js, oauth2.js, and welcome.js). These scripts encapsulate the core business logic for authentication, data interception, and automated syncing.
Architectural Components and Relationships
a. Main Components:
• Browser Extension UI:
• Popup Interface (popup.html and popup.js): This is the user-facing interface that allows them to start the syncing process, view status, or trigger manual actions.
• Welcome/Onboarding Page (welcome.html): Used for first-time instructions.
• Background & Service Scripts:
• Background Script (scripts/background.js): Manages long-running tasks/events, such as listening for events from the browser or handling push notifications.
• Authorization & OAuth2 Modules (scripts/authorize.js, scripts/oauth2.js): Implement the GitHub authentication workflow.
• Code Extraction and Integration:
• Domain-specific Scripts (scripts/leetcode.js and scripts/gfg.js): These likely interact with the LeetCode (or GeeksForGeeks) websites, extract code when a problem is solved and trigger the sync.
• GitHub Integration:
• API interaction logic integrated into the scripts: responsible for pushing code to GitHub repositories using GitHub REST/GraphQL APIs.
• Extension Asset Configuration:
• manifest.json: Configures necessary permissions (like activeTab, storage, etc.) and registers scripts.
b. Relationships and Interactions:
• The popup (UI) triggers user-initiated actions (e.g., “authorize with GitHub”).
• On authorization, the OAuth2 scripts communicate with GitHub by redirecting the user to the GitHub login, then returning an authorization token.
• The background script continually monitors the browser’s activity (e.g., detecting that a LeetCode problem was solved).
• Once the code is extracted via the LeetCode integration script, the extension uses the GitHub API (via the authenticated session) to push the changes.
• From the User → UI (Popup, Welcome) → Authorization Request → GitHub OAuth Flow → API Access Token.
• LeetCode site → Code Extraction Script → Background Script → GitHub API for commit.
Architectural Patterns, Technologies, and Design Principles
• Client-Side Scripting for Browser Extensions: Use of standard JavaScript with modular scripts organized into specific responsibilities.
• Event-Driven Architecture: Leveraging background scripts and event listeners to detect changes on web pages.
• Separation of Concerns: Distinct scripts handle UI, OAuth, background processing, and code extraction.
• Integration with External Services (GitHub OAuth/API, LeetCode's site).
Instructing the Principal Software Engineer
• UI Components: Popup, Welcome Page.
• Background Services: Background.js, OAuth, and Code Extraction Modules.
• External Services: GitHub API endpoints.
• Configuration: Manifest and npm build tools/config scripts.
• Data flow from the browser extension UI to the background script.
• Event triggers from code detection on LeetCode to the sync logic.
• Authorization flows from the extension to GitHub and back.
• For internal scripts and modules (e.g., rectangles).