-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
41 lines (32 loc) · 1.7 KB
/
background.js
File metadata and controls
41 lines (32 loc) · 1.7 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
import { github } from './github.js';
console.log("BeatCode DEBUG: Background Service Worker started.");
const GITHUB_USERNAME = "Your profile name";
const GITHUB_REPO = "Your Repo";
const GITHUB_TOKEN = "Your Token";
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("BeatCode DEBUG: Message received in background script.", request);
if (request.type === "pushToGitHub") {
console.log("BeatCode DEBUG: Message type is correct. Processing pushToGitHub.");
if (!GITHUB_USERNAME || !GITHUB_REPO || !GITHUB_TOKEN) {
console.error("BeatCode FATAL ERROR: Credentials are not set in background.js!");
sendResponse({ status: "Error", message: "Credentials missing in background script." });
return;
}
const { problemTitle, code } = request.data;
const fileName = `${problemTitle}.cpp`;
const commitMessage = `Solve ${problemTitle}`;
console.log(`BeatCode DEBUG: Preparing to push file: ${fileName}`);
github.pushFile(GITHUB_USERNAME, GITHUB_REPO, GITHUB_TOKEN, fileName, code, commitMessage)
.then(response => {
console.log(`BeatCode SUCCESS: Successfully pushed/updated ${fileName}.`, response);
sendResponse({ status: "Success", message: `Pushed ${fileName} successfully.` });
})
.catch(error => {
console.error("BeatCode FATAL ERROR during GitHub push:", error);
sendResponse({ status: "Error", message: error.message });
});
return true;
} else {
console.warn("BeatCode DEBUG: Received a message with an unknown type:", request.type);
}
});