-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
94 lines (78 loc) · 2.44 KB
/
background.js
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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Background received message:", message.type);
if (message.type === "debug") {
console.log("Debug:", message.message);
return;
}
if (message.type === "push_to_github") {
handleGitHubPush(message.payload);
}
});
async function handleGitHubPush(payload) {
try {
console.log("Attempting GitHub push for:", payload.problemTitle);
// Retrieve settings
const settings = await chrome.storage.sync.get([
"githubToken",
"repoName",
"branchName",
"debugMode",
]);
if (!settings.githubToken || !settings.repoName) {
console.error("GitHub settings not configured");
return;
}
// Create language-specific filename
const fileName = `${payload.problemTitle
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)/g, "")}.${payload.language}`;
const fileContent = `/*
* ${payload.problemTitle}
* Difficulty: ${payload.difficulty}
*
* ${payload.description}
*/
${payload.code}`;
const apiUrl = `https://api.github.com/repos/${settings.repoName}/contents/leetcode/${fileName}`;
const response = await fetch(apiUrl, {
method: "PUT",
headers: {
Authorization: `token ${settings.githubToken}`,
"Content-Type": "application/json",
Accept: "application/vnd.github.v3+json",
},
body: JSON.stringify({
message: `Add LeetCode solution: ${fileName}`,
content: btoa(unescape(encodeURIComponent(fileContent))),
branch: settings.branchName || "main",
}),
});
const responseData = await response.json();
if (!response.ok) {
throw new Error(
`GitHub API error: ${response.status} - ${JSON.stringify(responseData)}`
);
}
console.log("Successfully pushed to GitHub");
// Chrome notification
chrome.notifications.create({
type: "basic",
iconUrl: "icon48.png",
title: "LeetWithGit",
message: `Successfully pushed ${payload.problemTitle} to GitHub!`,
});
} catch (error) {
console.error("GitHub push error:", error);
// Error notification
chrome.notifications.create({
type: "basic",
iconUrl: "icon48.png",
title: "LeetWithGit Error",
message: `Failed to push solution: ${error.message}`,
});
}
}
chrome.runtime.onInstalled.addListener(() => {
console.log("LeetWithGit extension installed");
});