forked from SyedMuhammadFaheem/LeetHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
107 lines (96 loc) · 3.24 KB
/
content.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
104
105
106
107
chrome.storage.local.get(["username", "repo", "token"], function (result) {
const username = result.username;
const repo = result.repo;
const token = result.token;
const questionName = document
.getElementsByTagName("h4")[0]
.textContent.split(" ")
.join("");
let extensionName = document.getElementById("result_language").innerHTML;
if (extensionName === "python3") extensionName = ".py";
else if (extensionName === "python") extensionName = ".py";
else if (extensionName === "java") extensionName = ".java";
else if (extensionName === "cpp") extensionName = ".cpp";
else if (extensionName === "c") extensionName = ".c";
else if (extensionName === "cs") extensionName = ".cs";
else if (extensionName === "js") extensionName = ".js";
else if (extensionName === "ts") extensionName = ".ts";
else if (extensionName === "php") extensionName = ".php";
else if (extensionName === "swift") extensionName = ".swift";
const fileName =
questionName + extensionName ||
"solution.txt" + Math.floor(Math.random() * 100);
const COMMIT_MESSAGE = fileName + " " + String(new Date());
const leetCodeSolutionTextarea =
document.getElementsByClassName("ace_content");
let newContent = "";
for (let i = 0; i < leetCodeSolutionTextarea.length; i++) {
const element = leetCodeSolutionTextarea[i];
newContent += element.textContent;
}
for(let i=1;i<newContent.length;i++){
if(newContent[i-1] != ' ' && i+1 < newContent.length && newContent[i]==' ' && newContent[i+1]==' '){
newContent = newContent.slice(0,i) + '\n' + newContent.slice(i+1,newContent.length);
while(i+1<newContent.length && newContent[i+1]==' '){
i++;
}
}
}
if (!newContent) {
console.error("Failed to extract content from LeetCode solution textarea");
return;
}
const fileData = {
message: COMMIT_MESSAGE,
content: btoa(newContent),
};
const credentials = btoa(`${username}:${token}`);
const apiUrl = `https://api.github.com/repos/${username}/${repo}/contents/${fileName}`;
fetch(apiUrl, {
method: "GET",
headers: {
Authorization: `Basic ${credentials}`,
},
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
fetch(apiUrl, {
method: "PUT",
headers: {
Authorization: `Basic ${credentials}`,
"Content-Type": "application/json",
},
body: JSON.stringify(fileData),
}).then((response) => {
if (response.ok) window.alert("File Created!");
else throw new error("Error in creating file!");
});
}
})
.then((data) => {
const sha = data ? data.sha : '';
if (sha) {
return fetch(apiUrl, {
method: "PUT",
headers: {
Authorization: `Basic ${credentials}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
...fileData,
sha: sha,
}),
});
}
})
.then((response) => response ? response.json() : 'No SHA found')
.then((data) => {
if(data!="No SHA found")
window.alert("File Updated!")
})
.catch((error) => {
console.error("Error:", error);
});
});