1- chrome . storage . local . get ( [ "token" ] , function ( items ) {
2- debugger ;
3- if ( items . token ) {
4- window . location . href = '/options_logged_info.html' ;
5- }
6- } ) ;
1+ chrome . storage . local . get ( [ 'token' ] , ( { token } ) => {
2+ debugger
3+ if ( token ) window . location . href = '/options_logged_info.html'
4+ } )
75
8- var submitButton = document . getElementById ( 'submit' ) ;
6+ const submitButton = document . getElementById ( 'submit' )
7+ const userIdInput = document . getElementById ( 'user_id' )
8+ const passwordInput = document . getElementById ( 'password' )
99
10- var userIdInput = document . getElementById ( 'user_id' ) ;
11- var passwordInput = document . getElementById ( 'password' ) ;
10+ const validate = ( ) => {
11+ const { value : user_id } = userIdInput
12+ const { value : password } = passwordInput
13+ const params = { user_id, password }
14+ const xhr = new XMLHttpRequest ( )
15+ xhr . open ( 'POST' , 'https://api.solved.ac/request_token.php' , true )
16+ xhr . setRequestHeader ( 'Content-type' , 'application/json' )
17+ xhr . onload = ( { responseText, status } ) => {
18+ console . log ( responseText )
19+ if ( ! status === 200 ) {
20+ alert ( JSON . parse ( this . responseText ) . error )
21+ return
22+ }
1223
13- function onKeyPress ( ) {
14- if ( event . keyCode == 13 ) {
15- validate ( ) ;
16- }
24+ chrome . storage . local . set ( { token : JSON . parse ( responseText ) . token } , ( ) => {
25+ chrome . tabs . getSelected ( null , tab => {
26+ const code = 'window.location.reload();'
27+ chrome . tabs . executeScript ( tab . id , { code } )
28+ } )
29+ window . location . href = '/options_logged_info.html'
30+ } )
31+ return
32+ }
33+ xhr . send ( JSON . stringify ( params ) )
1734}
1835
19- function validate ( ) {
20- var userId = userIdInput . value ;
21- var password = passwordInput . value ;
22- var params = {
23- "user_id" : userId ,
24- "password" : password
25- } ;
26- var xhr = new XMLHttpRequest ( ) ;
27- xhr . open ( 'POST' , 'https://api.solved.ac/request_token.php' , true ) ;
28- xhr . setRequestHeader ( 'Content-type' , 'application/json' ) ;
29- xhr . onload = function ( ) {
30- console . log ( this . responseText ) ;
31- if ( this . status == 200 ) {
32- chrome . storage . local . set ( { "token" : JSON . parse ( this . responseText ) . token } , function ( ) {
33- chrome . tabs . getSelected ( null , function ( tab ) {
34- var code = 'window.location.reload();' ;
35- chrome . tabs . executeScript ( tab . id , { code : code } ) ;
36- } ) ;
37- window . location . href = '/options_logged_info.html' ;
38- } ) ;
39- } else {
40- alert ( JSON . parse ( this . responseText ) . error ) ;
41- }
42- } ;
43- xhr . send ( JSON . stringify ( params ) ) ;
44- }
36+ const onKeyPress = ( { keyCode } ) => ( keyCode === 13 ? validate ( ) : null )
4537
46- submitButton . addEventListener ( " click" , validate ) ;
47- userIdInput . addEventListener ( " keyup" , onKeyPress ) ;
48- passwordInput . addEventListener ( " keyup" , onKeyPress ) ;
38+ submitButton . addEventListener ( ' click' , validate )
39+ userIdInput . addEventListener ( ' keyup' , onKeyPress )
40+ passwordInput . addEventListener ( ' keyup' , onKeyPress )
0 commit comments