codesync-network-interception
Architecture, monkey-patching, GraphQL detection, and bridge communication for CodeSync's native Manifest V3 network interceptors.
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-10
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- codesync-network-interception
- description
- Architecture, monkey-patching, GraphQL detection, and bridge communication for CodeSync's native Manifest V3 network interceptors.
03 SKILL.md 正文
CodeSync Network Interception & Content Script Architecture
Use this skill when modifying, maintaining, or debugging the real-time submission detection pipeline in src/content/interceptor.ts and src/content/index.ts.
1. Architectural Overview & Context Worlds
In Chrome Manifest V3, content scripts run in an isolated execution environment. To intercept network calls made by single-page application frameworks (such as Next.js on LeetCode), CodeSync uses a two-stage bridge:
┌─────────────────────────────────────────────────────────────┐
│ Web Page Context (MAIN World) │
│ src/content/interceptor.ts │
│ - Monkey-patches window.fetch and window.XMLHttpRequest │
│ - Inspects responses without consuming stream (cloned) │
│ - Detects judging success: statusCode === 10 │
└──────────────────────────────┬──────────────────────────────┘
│ window.postMessage
│ (CODESYNC_JUDGING_ACCEPTED)
┌──────────────────────────────▼──────────────────────────────┐
│ Extension Content Script (ISOLATED World) │
│ src/content/index.ts │
│ - Listens to window messages │
│ - Resolves question metadata via LRUCache │
│ - Fetches full submissionDetails if needed │
│ - Normalizes Problem and Submission objects │
└──────────────────────────────┬──────────────────────────────┘
│ chrome.runtime.sendMessage
│ (action: 'ENQUEUE_SUBMISSION')
┌──────────────────────────────▼──────────────────────────────┐
│ Background Service Worker │
│ src/background/index.ts -> src/queue/index.ts │
└─────────────────────────────────────────────────────────────┘
2. Manifest V3 Registration
The interceptor is declared in public/manifest.json under content_scripts with "world": "MAIN" and "run_at": "document_start":
{
"content_scripts": [
{
"matches": [
"*://*.leetcode.com/*",
"*://leetcode.com/*",
"*://*.leetcode.cn/*",
"*://leetcode.cn/*"
],
"js": ["interceptor.js"],
"run_at": "document_start",
"world": "MAIN"
},
{
"matches": [
"*://*.leetcode.com/*",
"*://leetcode.com/*",
"*://*.leetcode.cn/*",
"*://leetcode.cn/*"
],
"js": ["content.js"],
"run_at": "document_idle"
}
]
}
3. Monkey-Patching Invariants
1. Non-Destructive Response Cloning
You must never consume the original response body stream, as the host application needs it to render the UI:
const originalFetch = window.fetch;
window.fetch = async function(...args) {
const response = await originalFetch(...args);
try {
const url = typeof args[0] === 'string' ? args[0] : (args[0] instanceof Request ? args[0].url : '');
if (url.includes('/graphql') || url.includes('/check/') || url.includes('/submit')) {
// CLONE before reading JSON
const cloned = response.clone();
cloned.json().then((json) => {
handleInterceptedPayload(url, json);
}).catch(() => {});
}
} catch (err) {
// Interceptor must never crash host page operations
}
return response;
};
2. LeetCode Judging Status Codes
LeetCode assigns integer status codes to submissions:
10: Accepted (AC) $\rightarrow$ Trigger synchronization11: Wrong Answer (WA) $\rightarrow$ Ignore12: Memory Limit Exceeded $\rightarrow$ Ignore13: Output Limit Exceeded $\rightarrow$ Ignore14: Time Limit Exceeded (TLE) $\rightarrow$ Ignore15: Runtime Error $\rightarrow$ Ignore16: Compile Error $\rightarrow$ Ignore20: Pending / Judging $\rightarrow$ Wait for subsequent check polling
4. Zero-Latency Metadata Pre-Caching
To enqueue submissions in under 10 milliseconds without making blocking network requests after judge completion, interceptor.ts intercepts GraphQL queries containing question details and caches them immediately:
// Detect question metadata during normal problem page load
if (json?.data?.question) {
const q = json.data.question;
window.postMessage({
type: 'CODESYNC_QUESTION_METADATA',
payload: {
questionId: q.questionId,
title: q.title,
titleSlug: q.titleSlug,
difficulty: q.difficulty,
content: q.content,
topicTags: q.topicTags,
}
}, '*');
}
In src/content/index.ts, incoming metadata is stored in an in-memory LRUCache:
const questionCache = new LRUCache<string, QuestionMetadata>(50);
const processedSubmissionIds = new LRUCache<string, boolean>(200);
5. Message Contracts
MAIN $\rightarrow$ ISOLATED Bridge:
CODESYNC_LOG: Diagnostic logging relayed to extension logger.CODESYNC_QUESTION_METADATA: Pre-cached problem definition and HTML description.CODESYNC_JUDGING_ACCEPTED: Emitted when judging returnsstatusCode: 10. ContainssubmissionId.
ISOLATED $\rightarrow$ Background Bridge:
chrome.runtime.sendMessage({
action: 'ENQUEUE_SUBMISSION',
payload: submissionObject,
}, (response) => {
if (response?.success) {
logToBackground('Successfully enqueued submission:', submissionObject.id);
}
});
6. Troubleshooting Guide
| Issue | Root Cause | Resolution |
|---|---|---|
| Submissions not captured | Page uses dynamic URL patterns or GraphQL query names changed | Check service worker logs (action: 'LOG') and verify if URL matches /graphql, /check/, or /submit/. |
| Response body already read error | Interceptor called .json() or .text() on original response |
Ensure response.clone() is called before reading the stream. |
| Extension context invalidated | Extension reloaded during development while tab remained open | Wrap calls with isContextInvalidated() check in src/content/index.ts to prevent runtime exception spam. |
| Duplicate submissions enqueued | Both fetch and XHR captured the same poll event | Enforce bounded deduplication using processedSubmissionIds.has(submissionId) in src/content/index.ts. |
04 同仓库更多 Skills
openspec-update-change
Update an OpenSpec change by revising its existing planning artifacts and keeping them coherent with one another. Use when the user wants to revise a change's plan, fold new decisions into it, or reconcile its artifacts after an edit. Never edits code.
openspec-sync-specs
Sync delta specs from a change to main specs. Use when the user wants to update main specs with changes from a delta spec, without archiving the change.
openspec-propose
Propose a new change with all artifacts generated in one step. Use when the user wants to quickly describe what they want to build and get a complete proposal with design, specs, and tasks ready for implementation.
openspec-explore
Enter explore mode - a thinking partner for exploring ideas, investigating problems, and clarifying requirements. Use when the user wants to think through something before or during a change.
openspec-archive-change
Archive a completed change in the experimental workflow. Use when the user wants to finalize and archive a change after implementation is complete.
openspec-apply-change
Implement tasks from an OpenSpec change. Use when the user wants to start implementing, continue implementation, or work through tasks.