@@ -34,6 +34,9 @@ const Button = styled.button<{ primary?: boolean }>`
3434`
3535
3636const Clock : FC = ( ) => {
37+ const pathnames = location . pathname . split ( '/' ) . filter ( Boolean )
38+ const slug = pathnames [ 1 ]
39+
3740 const [ leetCodeApi ] = useState ( new LeetCodeApi ( location . origin ) )
3841 const [ hidden , setHidden ] = useState ( false )
3942
@@ -45,18 +48,43 @@ const Clock: FC = () => {
4548
4649 async function getSubmissionId ( ) : Promise < string > {
4750 return new Promise ( function ( resolve , reject ) {
48- const originalSend = XMLHttpRequest . prototype . send
49- XMLHttpRequest . prototype . send = function ( ...args ) {
50- XMLHttpRequest . prototype . send = originalSend
51- originalSend . apply ( this , args )
52- this . addEventListener ( 'load' , function ( ) {
53- try {
54- const data = JSON . parse ( this . responseText )
55- resolve ( data . submission_id + '' )
56- } catch ( error ) {
57- reject ( error )
58- }
59- } )
51+ const originalOpen = XMLHttpRequest . prototype . open
52+ XMLHttpRequest . prototype . open = function newOpen (
53+ this : XMLHttpRequest ,
54+ method : string ,
55+ url : string ,
56+ async ?: boolean ,
57+ user ?: string ,
58+ password ?: string
59+ ) {
60+ if (
61+ method . toLocaleLowerCase ( ) === 'post' &&
62+ url === `/problems/${ slug } /submit/`
63+ ) {
64+ this . addEventListener ( 'readystatechange' , function ( ) {
65+ const DONE = XMLHttpRequest . DONE ?? 4
66+ if ( this . readyState === DONE ) {
67+ const status = this . status
68+ if ( status === 0 || ( status >= 200 && status < 400 ) ) {
69+ const data = JSON . parse ( this . responseText )
70+ if ( XMLHttpRequest . prototype . open === newOpen ) {
71+ XMLHttpRequest . prototype . open = originalOpen
72+ }
73+
74+ resolve ( data . submission_id + '' )
75+ } else {
76+ if ( status !== 429 ) {
77+ if ( XMLHttpRequest . prototype . open === newOpen ) {
78+ XMLHttpRequest . prototype . open = originalOpen
79+ }
80+
81+ reject ( `提交错误,状态 ${ status } ` )
82+ }
83+ }
84+ }
85+ } )
86+ }
87+ originalOpen . apply ( this , [ method , url , async ! , user , password ] )
6088 }
6189 } )
6290 }
0 commit comments