1- // importScripts("config.js");
2-
31let isPushing = false ;
42let cachedJwt = null ;
53
@@ -153,8 +151,14 @@ function isAcceptedOnly() {
153151 return acceptedElem && acceptedElem . textContent . trim ( ) === "Accepted" ;
154152}
155153
154+ function getSubmissionResult ( ) {
155+ const resultXPath = '/html/body/div[1]/div[2]/div/div/div[4]/div/div/div[11]/div/div/div/div[2]/div/div[1]/div[1]/div[1]/span' ;
156+ const resultElem = document . evaluate ( resultXPath , document , null , XPathResult . FIRST_ORDERED_NODE_TYPE , null ) . singleNodeValue ;
157+ return resultElem ? resultElem . textContent . trim ( ) : null ;
158+ }
159+
156160// Submit 버튼 클릭 후 상태를 모니터링하는 함수
157- function monitorSubmissionStatus ( pushBtn , onAccepted ) {
161+ function monitorSubmissionStatus ( pushBtn ) {
158162 let attempt = 0 ;
159163 const maxAttempts = 10 ; // 최대 15초 대기 (1.5초 * 10)
160164
@@ -163,17 +167,26 @@ function monitorSubmissionStatus(pushBtn, onAccepted) {
163167
164168 // Submit 결과가 나온 경우
165169 if ( document . querySelector ( '.view-line' ) ) {
166- if ( isAcceptedOnly ( ) ) {
170+ const result = getSubmissionResult ( ) ;
171+ if ( result ) {
167172 clearInterval ( interval ) ;
168- onAccepted ( ) ; // Accepted 상태일 때 콜백 실행
173+
174+ if ( result === "Accepted" ) {
175+ pushCodeToGitHub ( pushBtn ) . finally ( ( ) => {
176+ isPushing = false ;
177+ } ) ;
178+ } else {
179+ pushBtn . innerText = `❌ ${ result } ` ;
180+ isPushing = false ;
181+ }
169182 } else if ( attempt >= maxAttempts ) {
170183 clearInterval ( interval ) ;
171- pushBtn . innerText = "❌ Not Accepted " ;
184+ pushBtn . innerText = "❌ No Result " ;
172185 isPushing = false ;
173186 }
174187 }
175188
176- // 최대 시도 횟수 초과
189+ // timeout
177190 if ( attempt >= maxAttempts ) {
178191 clearInterval ( interval ) ;
179192 pushBtn . innerText = "❌ Timeout" ;
@@ -182,7 +195,7 @@ function monitorSubmissionStatus(pushBtn, onAccepted) {
182195 } , 1500 ) ;
183196}
184197
185- // Push 버튼 클릭 핸들러
198+ // Push button click handler
186199function handlePushButtonClick ( ) {
187200 if ( isPushing ) return ;
188201 isPushing = true ;
@@ -197,38 +210,13 @@ function handlePushButtonClick() {
197210 return ;
198211 }
199212
200- // 이미 Accepted 상태인 경우
201- if ( isAcceptedOnly ( ) ) {
202- const confirmed = confirm ( "✅ Problem accepted!\nDo you want to push your code to GitHub?" ) ;
203- if ( confirmed ) {
204- pushCodeToGitHub ( pushBtn ) . finally ( ( ) => {
205- isPushing = false ;
206- } ) ;
207- } else {
208- pushBtn . innerText = "🔁 Push" ;
209- isPushing = false ;
210- }
211- return ;
212- }
213-
214- // Submit 버튼 클릭 및 상태 모니터링
213+ // Submit button click and monitor status
215214 pushBtn . innerText = "⏳ Submitting..." ;
216215 submitButton . click ( ) ;
217-
218- monitorSubmissionStatus ( pushBtn , ( ) => {
219- const confirmed = confirm ( "✅ Problem accepted!\nDo you want to push your code to GitHub?" ) ;
220- if ( confirmed ) {
221- pushCodeToGitHub ( pushBtn ) . finally ( ( ) => {
222- isPushing = false ;
223- } ) ;
224- } else {
225- pushBtn . innerText = "🔁 Push" ;
226- isPushing = false ;
227- }
228- } ) ;
216+ monitorSubmissionStatus ( pushBtn ) ;
229217}
230218
231- // Push 버튼 추가
219+ // Add push button
232220function addPushButton ( ) {
233221 if ( document . getElementById ( "leet-github-push" ) ) return ;
234222
@@ -255,28 +243,23 @@ function addPushButton() {
255243 buttonGroup . insertBefore ( pushBtn , buttonGroup . firstChild ) ;
256244}
257245
258- // Submit 버튼의 상태 변화를 감지하는 옵저버
246+ // Modify monitorSubmitButton to reset button state after manual submission
259247function monitorSubmitButton ( ) {
260248 const pushBtn = document . getElementById ( "leet-github-push" ) ;
261249 if ( ! pushBtn ) return ;
262250
263251 const observer = new MutationObserver ( ( mutations ) => {
264- for ( const mutation of mutations ) {
265- if ( mutation . type === 'childList' || mutation . type === 'characterData' ) {
266- if ( isAcceptedOnly ( ) && ! isPushing ) {
267- const confirmed = confirm ( "✅ Problem accepted!\nDo you want to push your code to GitHub?" ) ;
268- if ( confirmed ) {
269- isPushing = true ;
270- pushCodeToGitHub ( pushBtn ) . finally ( ( ) => {
271- isPushing = false ;
272- } ) ;
273- }
274- }
252+ // Only update button state if not in pushing process
253+ if ( ! isPushing ) {
254+ const currentText = pushBtn . innerText ;
255+ // Reset button only if it was in an error state (starts with ❌)
256+ if ( currentText . startsWith ( "❌" ) ) {
257+ pushBtn . innerText = "🔄 Push" ;
275258 }
276259 }
277260 } ) ;
278261
279- // Submit 결과를 표시하는 요소 감시
262+ // Monitor result container
280263 const resultContainer = document . querySelector ( '.view-line' ) ?. parentElement ;
281264 if ( resultContainer ) {
282265 observer . observe ( resultContainer , {
@@ -353,17 +336,15 @@ async function pushCodeToGitHub(pushBtn) {
353336 const data = await res . json ( ) ;
354337
355338 if ( res . ok ) {
356- const pushedAt = data . pushed_at || new Date ( ) . toISOString ( ) ;
357-
358- chrome . storage . local . set ( { last_push : pushedAt } , ( ) => {
359- console . log ( `[Push] Last push: ${ pushedAt } ` ) ;
360- } ) ;
361-
362339 if ( data . message === "Already pushed!" ) {
363340 pushBtn . innerText = "⚠️ Already" ;
364341 } else if ( data . message === "No change" ) {
365342 pushBtn . innerText = "🟡 No change" ;
366343 } else {
344+ const pushedAt = data . pushed_at || new Date ( ) . toISOString ( ) ;
345+ chrome . storage . local . set ( { last_push : pushedAt } , ( ) => {
346+ console . log ( `[Push] Last push: ${ pushedAt } ` ) ;
347+ } ) ;
367348 pushBtn . innerText = "✅ Push" ;
368349 }
369350 } else {
@@ -385,13 +366,11 @@ function waitForEditorAndInsertButton() {
385366 const editor = document . querySelector ( '.monaco-editor' ) ;
386367 if ( editor ) {
387368 addPushButton ( ) ;
388- // monitorSubmitButton();
389369 } else {
390370 let retry = 0 ;
391371 const interval = setInterval ( ( ) => {
392372 if ( document . querySelector ( '.monaco-editor' ) ) {
393373 addPushButton ( ) ;
394- // monitorSubmitButton();
395374 clearInterval ( interval ) ;
396375 } else if ( retry ++ > 50 ) {
397376 clearInterval ( interval ) ;
@@ -411,4 +390,21 @@ observer.observe(document.body, { childList: true, subtree: true });
411390setTimeout ( ( ) => {
412391 waitForEditorAndInsertButton ( ) ;
413392 monitorSubmitButton ( ) ;
414- } , 1000 ) ;
393+ } , 1000 ) ;
394+
395+ document . addEventListener ( "keydown" , function ( e ) {
396+ const isMac = navigator . platform . toUpperCase ( ) . indexOf ( 'MAC' ) >= 0 ;
397+ const isShortcut = ( isMac && e . metaKey && ! e . ctrlKey && ! e . altKey && e . key === 'm' ) ||
398+ ( ! isMac && e . ctrlKey && ! e . metaKey && ! e . altKey && e . key === 'm' ) ;
399+
400+ if ( isShortcut ) {
401+ e . preventDefault ( ) ; // prevent browser behavior like minimizing
402+ const pushBtn = document . getElementById ( "leet-github-push" ) ;
403+ if ( pushBtn ) {
404+ console . log ( "Shortcut triggered: Push to GitHub" ) ;
405+ handlePushButtonClick ( ) ;
406+ } else {
407+ console . warn ( "Push button not found." ) ;
408+ }
409+ }
410+ } ) ;
0 commit comments