-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
103 lines (86 loc) · 3.9 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
95
96
97
98
99
100
101
102
103
chrome.runtime.onInstalled.addListener(() => {
console.log('Extension Installed');
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "upload") {
console.log("Message received for upload:", message);
chrome.storage.local.get(['repoName', 'token'], (result) => {
const { repoName, token } = result;
if (repoName && token) {
console.log('GitHub credentials found:', repoName, token);
// Validate and destructure message.data
const data = message.data || {};
const { questionName, formattedText, code } = data;
console.log('Extracted data:', { questionName, formattedText, code });
if (questionName && formattedText && code) {
uploadToGitHub(repoName, token, { questionName, formattedText, code })
.then(response => {
sendResponse({ status: "success", response });
})
.catch(error => {
console.error('GitHub upload failed:', error);
sendResponse({ status: "failure", error: error.message });
});
} else {
console.error('Invalid data structure:', data);
sendResponse({ status: "failure", error: "Invalid data structure" });
}
} else {
console.log('GitHub credentials not found.');
sendResponse({ status: "no_credentials" });
}
});
// Required to keep the message channel open for asynchronous response
return true;
}
});
async function uploadToGitHub(repoName, token, data) {
const { questionName, formattedText, code } = data;
const folderPath = `${questionName.replace(/\s+/g, '-')}`; // Replace spaces with hyphens for safe directory names
const readmeContent = `# ${questionName}\n\n${formattedText.replace(/\n/g, '\n\n')}`;
const codeContent = code;
const headers = {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
};
// Function to get the file SHA if it exists
const getFileSha = async (path) => {
const url = `https://api.github.com/repos/${repoName}/contents/${path}`;
const response = await fetch(url, {
method: 'GET',
headers: headers
});
if (response.status === 200) { // File exists
const fileData = await response.json();
return fileData.sha; // Return the SHA
} else if (response.status === 404) {
return null; // File does not exist
} else {
const error = await response.json();
throw new Error(`Failed to check file: ${error.message}`);
}
};
const createOrUpdateFile = async (path, content) => {
const sha = await getFileSha(path); // Get the SHA if the file exists
const url = `https://api.github.com/repos/${repoName}/contents/${path}`;
const body = {
message: `Add or update ${path}`,
content: btoa(unescape(encodeURIComponent(content))), // Encode content to base64
sha: sha || undefined // Include SHA if updating, otherwise undefined
};
const response = await fetch(url, {
method: 'PUT',
headers: headers,
body: JSON.stringify(body)
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Failed to create or update file: ${error.message}`);
}
return response.json();
};
// Create or update README.md and code.java in the respective folder
await createOrUpdateFile(`${folderPath}/README.md`, readmeContent);
await createOrUpdateFile(`${folderPath}/code.java`, codeContent);
}
//save