Add Solution Post Auto-Commit Feature
Summary
This PR adds a new feature that automatically commits LeetCode solution posts to GitHub as Solution.md files when users publish solution articles on LeetCode.
Motivation
LeetCode users often write detailed solution explanations and post them as articles on the platform. However, these valuable insights were not being captured in their GitHub repositories alongside their code solutions. This feature bridges that gap by automatically syncing solution posts to maintain a complete record of both code and explanations.
Key Features
🎯 Automatic Solution Post Detection
- Intercepts GraphQL requests when users publish solution articles on LeetCode
- Uses Chrome Extension Manifest V3 with
world: "MAIN"for reliable request interception - Supports both
fetchandXMLHttpRequestmethods used by LeetCode
📝 Smart Content Processing
- Extracts solution content, title, and problem metadata from GraphQL requests
- Maps LeetCode problem slugs to existing repository folder structures
- Creates properly formatted
Solution.mdfiles with titles and content
⚙️ User Controls
- Added toggle in extension popup: "Auto-Commit Solution Posts" (enabled by default)
- Users can disable the feature if they prefer manual solution post management
- Integrates seamlessly with existing LeetHub settings UI
🔄 GitHub Integration
- Fetches commit history from GitHub API to find the most recent solution commit
- Copies the commit message format from the actual code solution (e.g., "Time: 44 ms (100%), Space: 73 MB (100%) - LeetHub")
- Maintains consistency between code commits and solution post commits
- Handles various repository organization patterns (difficulty folders, language folders, etc.)
Technical Implementation
Architecture Changes
-
New interceptor script (
src/js/interceptor.js): Runs in MAIN world context to intercept LeetCode's network requests -
Enhanced content script (
src/js/leetcode.js): Processes intercepted data and handles GitHub uploads - Updated manifest: Dual content script setup for reliable request interception
- UI enhancements: New collapsible settings section in popup
Request Interception Flow
- User publishes a solution post on LeetCode
- Interceptor catches the
ugcArticlePublishSolutionGraphQL operation - Extracts
questionSlug,content, andtitlefrom the request - Maps problem slug to repository folder name using LeetHub's existing logic
- Fetches commit history from GitHub API to find the appropriate commit message
- Uploads
Solution.mdfile with the same commit message as the code solution
Error Handling
- Graceful fallback to default commit messages if GitHub API fails
- Silent handling of network errors to avoid disrupting user experience
- Intelligent problem name matching for edge cases
Files Modified
-
manifest.json: Added MAIN world content script configuration -
src/html/popup.html: Added solution post toggle UI -
src/js/popup.js: Added settings management for the new feature -
src/js/leetcode.js: Enhanced with solution post processing logic -
src/js/interceptor.js: New file for request interception (MAIN world)
Test Plan
- [x] Verify solution post detection works with GraphQL interception
- [x] Test commit message copying from existing solution files
- [x] Confirm toggle setting saves and loads correctly
- [x] Validate proper folder structure mapping
- [x] Test with different repository organization settings
- [x] Ensure backward compatibility with existing LeetHub functionality
Benefits for Users
- Complete Documentation: Solution explanations are now preserved alongside code
- Consistent Commit Messages: Maintains the same format as code solutions
- Zero Manual Work: Completely automatic with option to disable
- GitHub Integration: Leverages existing LeetHub authentication and repository setup
- Flexible Organization: Works with all existing folder organization preferences
This feature enhances LeetHub's value proposition by capturing not just the "what" (code solutions) but also the "how" and "why" (solution explanations) in users' GitHub repositories.
🤖 Generated with Claude Code
This PR addresses the feature request in #62 - "Add support for solution posts".
The implementation provides exactly what was requested:
- ✅ Automatic detection when users post solution articles on LeetCode
- ✅ Extraction of solution content and metadata
- ✅ Creation of
Solution.mdfiles in the same problem folders - ✅ Consistent commit messages matching the original code solutions
- ✅ User control via settings toggle (enabled by default)
The solution uses modern Chrome Extension APIs (Manifest V3 with world: "MAIN") for reliable GraphQL request interception and integrates seamlessly with existing LeetHub functionality.
@raphaelheinz This should fully resolve the feature request! 🚀
Hi @furic, thank you so much for your PR. I appreciate your help supporting this plugin and introduce new features. I'll check asap and sorry for late reply.