1+ $ ( document ) . ready ( function ( ) {
2+ add_button ( ) ;
3+ const url_string = window . location . href ;
4+ const url = new URL ( url_string ) ;
5+ const planner = url . searchParams . get ( "planner" ) ;
6+ const ysl = url . searchParams . get ( "ysl" ) ;
7+ if ( planner && ysl ) {
8+ let import_mah = confirm ( 'Import data?' ) ;
9+ if ( import_mah ) {
10+ importData ( ) ;
11+ }
12+ }
13+ } ) ;
14+
15+ function exportData ( ) {
16+ const export_url = ( `https://nusmods.com/planner?planner=${ encodeURIComponent ( localStorage . getItem ( 'persist:planner' ) ) } &ysl=${ encodeURIComponent ( localStorage . getItem ( 'YSL:data' ) ) } ` ) ;
17+ // setTimeout(() => {
18+ // navigator.clipboard.writeText(export_url).then(function () {
19+ // console.log('succeeded');
20+ // confirm('URL copied to clipboard!');
21+ // }, function () {
22+ // console.log('failed');
23+ // });
24+ // }, 1000);
25+ navigator . clipboard . writeText ( export_url ) . then ( function ( ) {
26+ console . log ( 'succeeded' ) ;
27+ confirm ( 'URL copied to clipboard!' ) ;
28+ } , function ( ) {
29+ console . log ( 'failed' ) ;
30+ } ) ;
31+ }
32+
33+ function populateModuleBank ( mod ) {
34+ let moduleBank = localStorage . getItem ( 'persist:moduleBank' ) ;
35+ if ( moduleBank ) {
36+ moduleBank = JSON . parse ( moduleBank ) ;
37+ } else {
38+ moduleBank = { } ;
39+ }
40+ let modules = moduleBank [ 'modules' ] ;
41+ if ( modules ) {
42+ modules = JSON . parse ( modules ) ;
43+ } else {
44+ modules = { } ;
45+ }
46+ if ( modules [ mod ] ) {
47+ return ;
48+ }
49+ const yr = new Date ( ) . getFullYear ( )
50+ const mth = new Date ( ) . getMonth ( ) ;
51+ let ay ;
52+ if ( mth < 6 ) {
53+ ay = '' + ( yr - 1 ) + '-' + yr ;
54+ } else {
55+ ay = '' + yr + '-' + ( yr + 1 ) ;
56+ }
57+ fetch ( `https://api.nusmods.com/v2/${ ay } /modules/${ mod } .json` )
58+ . then ( res => res . json ( ) )
59+ . then ( data => {
60+ modules [ mod ] = data ;
61+ moduleBank [ 'modules' ] = JSON . stringify ( modules ) ;
62+ moduleBank = JSON . stringify ( moduleBank ) ;
63+ localStorage . setItem ( 'persist:moduleBank' , moduleBank ) ;
64+ } ) ;
65+ }
66+
67+ function importData ( ) {
68+ const url_string = ( window . location . href ) ;
69+ const url = new URL ( url_string ) ;
70+ const planner = decodeURIComponent ( url . searchParams . get ( "planner" ) ) ;
71+ const ysl = decodeURIComponent ( url . searchParams . get ( "ysl" ) ) ;
72+ localStorage . setItem ( 'persist:planner' , planner ) ;
73+ localStorage . setItem ( 'YSL:data' , ysl ) ;
74+ const planner_obj = JSON . parse ( planner ) ;
75+ const modules = Object . values ( JSON . parse ( planner_obj [ 'modules' ] ) ) . map ( x => x [ 'moduleCode' ] ) ;
76+ modules . forEach ( x => {
77+ populateModuleBank ( x ) ;
78+ } ) ;
79+ window . location . href = '/planner' ;
80+ }
81+
82+ function add_button ( ) {
83+ var export_btn = $ ( `<button class="btn btn-svg btn-outline-primary" style="margin-left:8px" type="button">
84+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg svg-small"><polyline points="17 1 21 5 17 9"></polyline><path d="M3 11V9a4 4 0 0 1 4-4h14"></path><polyline points="7 23 3 19 7 15"></polyline><path d="M21 13v2a4 4 0 0 1-4 4H3"></path></svg>
85+ Export</button>` ) ;
86+ var space = $ ( '<div class="divider"/>' ) ;
87+ checkExist ( ".main-content > div > header > div" , function ( ) {
88+ space . appendTo ( $ ( ".main-content > div > header > div" ) ) ;
89+ export_btn . appendTo ( $ ( ".main-content > div > header > div" ) ) ;
90+ export_btn . click ( exportData ) ;
91+ } ) ;
92+ }
0 commit comments