-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
248 lines (216 loc) · 8.27 KB
/
Copy pathbackground.js
File metadata and controls
248 lines (216 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Background service worker for YeetCode extension
// Handles all network requests to GitHub API to bypass CORS/CSP restrictions on LeetCode.com
console.log('YeetCode background service worker started');
// Mapping of Monaco language IDs to file extensions
const LANGUAGE_EXTENSIONS = {
'cpp': '.cpp',
'c': '.c',
'csharp': '.cs',
'java': '.java',
'python': '.py',
'python3': '.py',
'javascript': '.js',
'typescript': '.ts',
'go': '.go',
'golang': '.go',
'ruby': '.rb',
'swift': '.swift',
'kotlin': '.kt',
'scala': '.scala',
'rust': '.rs',
'php': '.php',
'sql': '.sql',
'mysql': '.sql',
'mssql': '.sql',
'oraclesql': '.sql',
'postgresql': '.sql',
'bash': '.sh',
'shell': '.sh',
'r': '.r',
'erlang': '.erl',
'elixir': '.ex',
'dart': '.dart'
};
function getExtensionForLanguage(langId) {
if (!langId) return '.txt';
const cleanLang = langId.toLowerCase().trim();
return LANGUAGE_EXTENSIONS[cleanLang] || '.txt';
}
// Fetch extension settings from storage
async function getExtensionSettings() {
return new Promise((resolve) => {
chrome.storage.sync.get([
'githubToken',
'githubRepo',
'githubBranch',
'githubFolder',
'saveNotesAndTimer'
], function(items) {
resolve({
token: items.githubToken || '',
repo: items.githubRepo || '',
branch: items.githubBranch || 'main',
folder: items.githubFolder || '',
saveNotesAndTimer: items.saveNotesAndTimer !== undefined ? items.saveNotesAndTimer : true
});
});
});
}
// Listen for messages from content script or options page
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'saveToGitHub') {
handleSaveToGitHub(
request.code,
request.problemInfo,
request.languageId,
request.notesText || null,
request.timerValue || null
)
.then(result => sendResponse({ success: true, result }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Keep message channel open for async response
}
if (request.action === 'testConnection') {
handleTestConnection(request.token, request.repo)
.then(result => sendResponse({ success: true, result }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Keep message channel open for async response
}
});
// Test connection to GitHub repository
async function handleTestConnection(token, repo) {
if (!token || !repo) {
throw new Error('Token and repository owner/name are required.');
}
const apiUrl = `https://api.github.com/repos/${repo}`;
const response = await fetch(apiUrl, {
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (!response.ok) {
let errMsg = `HTTP Error ${response.status}`;
try {
const data = await response.json();
errMsg = data.message || errMsg;
} catch (e) {
// Use generic error if parsing fails
}
if (response.status === 401) {
throw new Error(`Unauthorized: Invalid Personal Access Token (${errMsg}).`);
} else if (response.status === 404) {
throw new Error(`Repository not found: Please verify that '${repo}' exists and your token has permission to access it.`);
} else {
throw new Error(`GitHub API Error: ${errMsg}`);
}
}
// Verify token scopes
const scopes = response.headers.get('X-OAuth-Scopes');
if (scopes !== null) {
const scopesArray = scopes.split(',').map(s => s.trim());
if (!scopesArray.includes('repo')) {
throw new Error("Missing Permission: Your token does not have the 'repo' scope. Please create a token with 'repo' scope.");
}
}
const data = await response.json();
return {
private: data.private,
description: data.description,
fullName: data.full_name
};
}
// Commit a single file to GitHub using the Contents API (PUT = create or update)
async function commitFileToGitHub(settings, filePath, content, commitMessage) {
const apiUrl = `https://api.github.com/repos/${settings.repo}/contents/${filePath}`;
// Check if file already exists so we can include its SHA for updates
let existingSha = null;
try {
const checkResponse = await fetch(apiUrl, {
headers: {
'Authorization': `token ${settings.token}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (checkResponse.ok) {
const existing = await checkResponse.json();
existingSha = existing.sha || null;
}
} catch (error) {
console.warn('Error checking existing file, assuming new file:', error);
}
const requestBody = {
message: commitMessage,
content: btoa(unescape(encodeURIComponent(content))),
branch: settings.branch
};
if (existingSha) {
requestBody.sha = existingSha;
}
const response = await fetch(apiUrl, {
method: 'PUT',
headers: {
'Authorization': `token ${settings.token}`,
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
let errMsg = `HTTP Error ${response.status}`;
try {
const errorData = await response.json();
errMsg = errorData.message || errMsg;
} catch (e) {
// Ignore parse error
}
throw new Error(`GitHub API Commit failed: ${errMsg}`);
}
return await response.json();
}
// Build the notes markdown content
function buildNotesMarkdown(problemInfo, notesText, timerValue, languageId) {
const lines = [];
lines.push(`# ${problemInfo.title}`);
lines.push('');
lines.push(`**Problem URL:** ${problemInfo.url || 'N/A'}`);
lines.push(`**Language:** ${languageId || 'N/A'}`);
lines.push(`**Saved At:** ${problemInfo.timestamp || new Date().toISOString()}`);
if (timerValue) {
lines.push(`**Time Taken:** ${timerValue}`);
}
if (notesText) {
lines.push('');
lines.push('## Notes');
lines.push('');
lines.push(notesText);
}
return lines.join('\n');
}
// Save solution to GitHub
async function handleSaveToGitHub(code, problemInfo, languageId, notesText, timerValue) {
const settings = await getExtensionSettings();
if (!settings.token || !settings.repo) {
throw new Error('GitHub token or repository not configured in settings.');
}
// Format stable per-problem paths so repeated submissions update the same files.
const sanitizedTitle = problemInfo.title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '') || 'unknown-problem';
// --- Commit 1: Code file ---
const ext = getExtensionForLanguage(languageId);
const folderPrefix = settings.folder ? `${settings.folder}/` : '';
const codeFilePath = `${folderPrefix}${sanitizedTitle}/${sanitizedTitle}${ext}`;
const codeCommitMessage = `Save LeetCode solution: ${problemInfo.title} (${problemInfo.timestamp || new Date().toISOString()})`;
const codeResult = await commitFileToGitHub(settings, codeFilePath, code, codeCommitMessage);
// --- Commit 2: Notes/Timer markdown file (optional) ---
let notesResult = null;
if (settings.saveNotesAndTimer) {
const notesMarkdown = buildNotesMarkdown(problemInfo, notesText, timerValue, languageId);
const notesFilePath = `${folderPrefix}${sanitizedTitle}/${sanitizedTitle}-notes.md`;
const notesCommitMessage = `Save notes for: ${problemInfo.title} (${problemInfo.timestamp || new Date().toISOString()})`;
notesResult = await commitFileToGitHub(settings, notesFilePath, notesMarkdown, notesCommitMessage);
}
return { code: codeResult, notes: notesResult };
}