@@ -371,3 +371,71 @@ function getRandomURL() {
371371function randRange ( min , max ) {
372372 return Math . floor ( Math . random ( ) * ( max - min ) + min ) ;
373373}
374+
375+ function exportSaveData ( ) {
376+ function getCookies ( ) {
377+ let cookies = document . cookie . split ( '; ' ) ;
378+ let cookieObj = { } ;
379+ cookies . forEach ( cookie => {
380+ let [ name , value ] = cookie . split ( '=' ) ;
381+ cookieObj [ name ] = value ;
382+ } ) ;
383+ return cookieObj ;
384+ }
385+ function getLocalStorage ( ) {
386+ let localStorageObj = { } ;
387+ for ( let key in localStorage ) {
388+ if ( localStorage . hasOwnProperty ( key ) ) {
389+ localStorageObj [ key ] = localStorage . getItem ( key ) ;
390+ }
391+ }
392+ return localStorageObj ;
393+ }
394+ const data = {
395+ cookies : getCookies ( ) ,
396+ localStorage : getLocalStorage ( )
397+ } ;
398+ const dataStr = JSON . stringify ( data , null , 2 ) ;
399+ const blob = new Blob ( [ dataStr ] , { type : 'application/json' } ) ;
400+ const url = URL . createObjectURL ( blob ) ;
401+ const a = document . createElement ( 'a' ) ;
402+ a . href = url ;
403+ a . download = 'save_data.json' ;
404+ document . body . appendChild ( a ) ;
405+ a . click ( ) ;
406+ document . body . removeChild ( a ) ;
407+ URL . revokeObjectURL ( url ) ;
408+ }
409+
410+ function importSaveData ( ) {
411+ const input = document . createElement ( 'input' ) ;
412+ input . type = 'file' ;
413+ input . accept = 'application/json' ;
414+ input . onchange = function ( event ) {
415+ const file = event . target . files [ 0 ] ;
416+ if ( ! file ) return ;
417+ const reader = new FileReader ( ) ;
418+ reader . onload = function ( e ) {
419+ try {
420+ const data = JSON . parse ( e . target . result ) ;
421+ if ( data . cookies ) {
422+ Object . entries ( data . cookies ) . forEach ( ( [ key , value ] ) => {
423+ document . cookie = `${ key } =${ value } ; path=/` ;
424+ } ) ;
425+ }
426+ if ( data . localStorage ) {
427+ Object . entries ( data . localStorage ) . forEach ( ( [ key , value ] ) => {
428+ localStorage . setItem ( key , value ) ;
429+ } ) ;
430+ }
431+ alert ( 'Your save data has been imported. Please test it out.' )
432+ alert ( 'If you find any issues then report it in GitHub or the Interstellar Discord.' )
433+ } catch ( error ) {
434+ console . error ( 'Error parsing JSON file:' , error ) ;
435+ }
436+ } ;
437+ reader . readAsText ( file ) ;
438+ } ;
439+ input . click ( ) ;
440+ }
441+
0 commit comments