-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
173 lines (149 loc) · 5.73 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
let QUES = '';
let DESCRIPTION = '';
//udca
const pollForQuestion = setInterval(() => {
const headingElem = document.querySelector('.text-2xl.font-bold.text-new_primary.dark\\:text-new_dark_primary.relative');
const paragraphElem = document.querySelector('p.mt-6');
if (headingElem && paragraphElem) {
QUES = headingElem.textContent || '';
DESCRIPTION = paragraphElem.textContent || '';
clearInterval(pollForQuestion);
}
}, 2000);
const storedData = localStorage.getItem('storedData');
const parsedData = JSON.parse(storedData || '[]');
let PROBLEM_SLUG = '';
let SELECTED_LANGUAGE = '';
let PUBLIC_CODE = '';
if (parsedData.length > 0) {
const { problemSlug, selectedLanguage, publicCodeOfSelected } = parsedData.at(-1);
PROBLEM_SLUG = problemSlug;
SELECTED_LANGUAGE = selectedLanguage;
PUBLIC_CODE = publicCodeOfSelected;
}
const GITHUB_CONFIG = {
token: "",
owner: "",
repo: "",
branch: ""
};
const initGitHubConfig = () => {
GITHUB_CONFIG.token = localStorage.getItem('github_token') || prompt('Enter your GitHub token:');
GITHUB_CONFIG.owner = localStorage.getItem('github_owner') || prompt('Enter your GitHub username:');
GITHUB_CONFIG.repo = localStorage.getItem('github_repo') || prompt('Enter your repository name:');
localStorage.setItem('github_token', GITHUB_CONFIG.token);
localStorage.setItem('github_owner', GITHUB_CONFIG.owner);
localStorage.setItem('github_repo', GITHUB_CONFIG.repo);
};
const createOrUpdateFile = async (filePath, content, commitMessage) => {
try {
console.log('Creating/updating file...');
const response = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.owner}/${GITHUB_CONFIG.repo}/contents/${filePath}`, {
headers: {
'Authorization': `token ${GITHUB_CONFIG.token}`,
'Accept': 'application/vnd.github.v3+json'
}
});
const payload = {
message: commitMessage,
content: btoa(content),
branch: GITHUB_CONFIG.branch
};
if (response.ok) {
const data = await response.json();
payload.sha = data.sha;
}
const updateResponse = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.owner}/${GITHUB_CONFIG.repo}/contents/${filePath}`, {
method: 'PUT',
headers: {
'Authorization': `token ${GITHUB_CONFIG.token}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!updateResponse.ok) {
throw new Error(`GitHub API responded with ${updateResponse.status}`);
}
console.log('File successfully created/updated!');
return true;
} catch (error) {
console.error('Error creating/updating file:', error);
return false;
}
};
const handleSubmissionPush = async (Sdata) => {
try {
console.log('Handling submission push...');
if (!Sdata.success) return false;
if (!GITHUB_CONFIG.token) initGitHubConfig();
const commitMessage = `Solved: ${QUES}\n\n` +
`Success: ${Sdata.success}\n` +
`Test Cases: ${Sdata.totalTestCases}\n` +
`Time: ${Sdata.averageTime}\n` +
`Memory: ${Sdata.averageMemory}`;
const fileContent = `/*
Problem: ${QUES}
Problem Link: ${window.location.href}
Description:
${DESCRIPTION}
Stats:
- Success: ${Sdata.success}
- Test Cases: ${Sdata.totalTestCases}
- Time: ${Sdata.averageTime}
- Memory: ${Sdata.averageMemory}
*/
${PUBLIC_CODE}`;
const urlPath = window.location.pathname
.replace('/plus/', '') // Remove 'plus'
.replace('/data-structures-and-algorithm/', '') // Remove DSA prefix
.replace('/submissions', '') // Remove submissions
.split('/')
.filter(part => part.length > 0 && part !== 'data-structures-and-algorithm'); // Remove
// Create directory path from URL parts
const dirPath = urlPath.join('/');
const fileExtension =
SELECTED_LANGUAGE === 'cpp' ? 'cpp' :
SELECTED_LANGUAGE === 'python' ? 'py' :
SELECTED_LANGUAGE === 'javascript' ? 'js' : 'txt';
const filePath = `${dirPath}/solution.${fileExtension}`;
const success = await createOrUpdateFile(filePath, fileContent, commitMessage);
if (success) {
console.log('Successfully pushed to GitHub!');
} else {
console.error('Failed to push to GitHub');
}
return success;
} catch (error) {
console.error('Error in GitHub push:', error);
return false;
}
};
const injectInterceptor = () => {
const script = document.createElement('script');
script.src = chrome.runtime.getURL('interceptor.js');
(document.head || document.documentElement).appendChild(script);
script.onload = () => script.remove();
};
window.addEventListener('message', async (event) => {
console.log('Received submission response');
if (event.data.type === 'SUBMISSION_RESPONSE') {
const submissionData = event.data.payload;
if (submissionData.success === true) {
await handleSubmissionPush(submissionData);
}
}
});
function initSubmitButtonMonitor() {
document.addEventListener('DOMContentLoaded', () => {
const submitBtn = document.querySelector('button[data-tooltip-id="Submit"]');
if (submitBtn) {
submitBtn.addEventListener('click', () => {
console.log('Submit button clicked');
});
}
});
}
// Call initialization methods immediately
injectInterceptor();
initSubmitButtonMonitor();