-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (38 loc) · 1.37 KB
/
background.js
File metadata and controls
44 lines (38 loc) · 1.37 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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "submission") {
console.log("📨 Received submission from content script", message.data);
const { title, code } = message.data;
chrome.storage.sync.get(["repo", "token"], ({ repo, token }) => {
if (!repo || !token) {
console.error("❌ Missing GitHub repo or token");
return;
}
const path = `cses/${title}.cpp`;
const commitMsg = `Add solution for ${title}`;
const content = btoa(unescape(encodeURIComponent(code)));
const body = JSON.stringify({
message: commitMsg,
content: content
});
console.log(`📤 Sending commit to https://api.github.com/repos/${repo}/contents/${path}`);
fetch(`https://api.github.com/repos/${repo}/contents/${path}`, {
method: "PUT",
headers: {
Authorization: `token ${token}`,
"Content-Type": "application/json"
},
body: body
})
.then(res => res.json())
.then(json => {
console.log("✅ GitHub response:", json);
if (json.content) {
console.log("🎉 Commit succeeded!");
} else {
console.warn("⚠️ Commit failed or file exists:", json);
}
})
.catch(err => console.error("🔥 GitHub error:", err));
});
}
});