File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ let requestAuthKey = null ;
4+
5+ function request ( body , timeout = 5000 ) {
6+ let xhr = new XMLHttpRequest ( ) ;
7+ xhr . open ( 'POST' , `http://${ location . hostname } :8774/socket` ) ;
8+
9+ return new Promise ( function ( resolve , reject ) {
10+ xhr . onload = function ( e ) {
11+ if ( xhr . status === 200 ) {
12+ resolve ( xhr . responseText ) ;
13+ } else if ( [ 400 , 403 , 500 ] . includes ( xhr . status ) ) {
14+ reject ( `${ xhr . statusText } : ${ xhr . responseText } ` ) ;
15+ } else {
16+ reject ( `Unknown response: ${ xhr . status } ${ xhr . statusText } (${ xhr . responseText } )` )
17+ }
18+ } ;
19+ xhr . ontimeout = ( e ) => reject ( 'Request timed out!' ) ;
20+ xhr . onerror = ( e ) => reject ( 'Error communicating with bttn!' ) ;
21+
22+ xhr . timeout = timeout ;
23+
24+ if ( requestAuthKey ) {
25+ body = 'auth = ' + requestAuthKey + '\n' + body ;
26+ }
27+ xhr . send ( body ) ;
28+ } ) ;
29+ }
30+
31+ function setRequestAuthKey ( key ) {
32+ requestAuthKey = key ;
33+ }
34+
35+ window . request = request ;
36+ window . setRequestAuthKey = setRequestAuthKey ;
You can’t perform that action at this time.
0 commit comments