2424 .error {
2525 color : # dc3545 ;
2626 }
27+ .version {
28+ font-size : 13px ;
29+ color : # 666 ;
30+ margin-top : 6px ;
31+ }
2732 </ style >
2833</ head >
2934< body >
3035 < div class ="container ">
3136 < h2 > OAuth Authorization</ h2 >
37+ < div id ="version " class ="version "> Version: v2</ div >
3238 < div id ="status "> Processing authorization...</ div >
3339 </ div >
3440
3541 < script >
36- ( function ( ) {
37- const statusDiv = document . getElementById ( 'status' ) ;
38- try {
39- // Parse URL fragment for access_token
40- const hash = window . location . hash . startsWith ( '#' ) ? window . location . hash . substring ( 1 ) : window . location . hash ;
41- const params = new URLSearchParams ( hash ) ;
42-
43- const accessToken = params . get ( 'access_token' ) ;
44- const error = params . get ( 'error' ) ;
45-
46- if ( accessToken ) {
47- statusDiv . innerHTML = '<div class="success">✓ Authorization successful!<br>Stänger fönstret...</div>' ;
42+ ( function ( ) {
43+ const statusDiv = document . getElementById ( 'status' ) ;
44+ const versionDiv = document . getElementById ( 'version' ) ;
45+ // DRY constants
46+ const VERSION = '3' ;
47+ const CLOSE_DELAY_MS = 4400 ; // how long the success message is shown before window.close()
48+ // Centralized visible text so the body content is DRY and easy to change
49+ const TEXTS = {
50+ TITLE : 'OAuth Authorization' ,
51+ PROCESSING : 'Processing authorization...' ,
52+ SUCCESS : '✓ Authorization successful!<br>You can close this window.' ,
53+ ERROR_PREFIX : '✗ Authorization failed:<br>' ,
54+ NO_DATA : '✗ No authorization data received' ,
55+ ERROR_PROCESS : '✗ Error processing authorization:<br>'
56+ } ;
57+ // Set version from the constant (no runtime fetch)
58+ try { if ( versionDiv ) versionDiv . textContent = 'Version: v' + VERSION ; } catch ( _ ) { }
4859
49- // Send token back to parent window
50- if ( window . opener ) {
51- try {
60+ // Use TEXTS to populate the page content (DRY)
61+ try {
62+ const titleEl = document . querySelector ( '.container h2' ) ;
63+ if ( titleEl ) titleEl . textContent = TEXTS . TITLE ;
64+ if ( statusDiv ) statusDiv . innerHTML = TEXTS . PROCESSING ;
65+ } catch ( _ ) { }
66+
67+ try {
68+ // Parse URL fragment for access_token
69+ const hash = window . location . hash . substring ( 1 ) ;
70+ const params = new URLSearchParams ( hash ) ;
71+
72+ const accessToken = params . get ( 'access_token' ) ;
73+ const error = params . get ( 'error' ) ;
74+
75+ if ( accessToken ) {
76+ statusDiv . innerHTML = '<div class="success">' + TEXTS . SUCCESS + '</div>' ;
77+
78+ // Send token back to parent window
79+ if ( window . opener ) {
5280 window . opener . postMessage ( {
5381 access_token : accessToken ,
5482 expires_in : params . get ( 'expires_in' ) ,
5583 token_type : params . get ( 'token_type' )
5684 } , '*' ) ;
57- } catch ( _ ) { }
58- }
59-
60- // Close after a short delay so the user sees the success briefly
61- setTimeout ( ( ) => {
62- try { window . close ( ) ; } catch ( _ ) { }
63- } , 400 ) ;
64-
65- } else if ( error ) {
66- statusDiv . innerHTML = '<div class="error">✗ Authorization failed:<br>' + error + '</div>' ;
67-
68- if ( window . opener ) {
69- try {
85+ }
86+
87+ // Close after a brief delay (configured above)
88+ setTimeout ( ( ) => {
89+ try { window . close ( ) ; } catch ( _ ) { }
90+ } , CLOSE_DELAY_MS ) ;
91+
92+ } else if ( error ) {
93+ statusDiv . innerHTML = '<div class="error">' + TEXTS . ERROR_PREFIX + error + '</div>' ;
94+
95+ if ( window . opener ) {
7096 window . opener . postMessage ( {
7197 error : error ,
7298 error_description : params . get ( 'error_description' )
7399 } , '*' ) ;
74- } catch ( _ ) { }
100+ }
101+
102+ } else {
103+ statusDiv . innerHTML = '<div class="error">' + TEXTS . NO_DATA + '</div>' ;
104+
105+ if ( window . opener ) {
106+ window . opener . postMessage ( {
107+ error : 'no_token_received'
108+ } , '*' ) ;
109+ }
75110 }
76-
77- } else {
78- statusDiv . innerHTML = '<div class="error">✗ No authorization data received </div>' ;
79-
111+
112+ } catch ( e ) {
113+ statusDiv . innerHTML = '<div class="error">' + TEXTS . ERROR_PROCESS + e . message + ' </div>';
114+
80115 if ( window . opener ) {
81- try { window . opener . postMessage ( { error : 'no_token_received' } , '*' ) ; } catch ( _ ) { }
116+ window . opener . postMessage ( {
117+ error : 'processing_error' ,
118+ error_description : e . message
119+ } , '*' ) ;
82120 }
83121 }
84-
85- } catch ( e ) {
86- statusDiv . innerHTML = '<div class="error">✗ Error processing authorization:<br>' + e . message + '</div>' ;
87-
88- if ( window . opener ) {
89- try { window . opener . postMessage ( { error : 'processing_error' , error_description : e . message } , '*' ) ; } catch ( _ ) { }
90- }
91- }
92- } ) ( ) ;
122+ } ) ( ) ;
93123 </ script >
94124</ body >
95125</ html >
0 commit comments