@@ -186,14 +186,24 @@ function assignments (node) {
186186 */
187187async function getSavedGrades ( username ) {
188188 const courses = [ ] ;
189- const output = await browser . storage . local . get ( "USERDATA_" + username ) ;
190- if ( output !== undefined && output [ "USERDATA_" + username ] !== undefined ) {
191- const course_list = output [ "USERDATA_" + username ] . courses || [ ] ;
192- for ( let i = 0 ; i < course_list . length ; i ++ ) {
193- const course = course_list [ i ] ;
194- courses . push ( new Course ( course . name , course . link , course . grade , course . finalPercent , course . assignments ) ) ;
189+ const user_data = ( await browser . storage . local . get ( "user_data" ) ) . user_data ;
190+ if ( user_data !== undefined ) {
191+ const user = user_data [ "USERDATA_" + username ] ;
192+ if ( user !== undefined ) {
193+ const course_list = user . courses || [ ] ;
194+ for ( let ind = 0 ; ind < course_list . length ; ind ++ ) {
195+ const course = course_list [ ind ] ;
196+ courses . push ( new Course (
197+ course . name ,
198+ course . link ,
199+ course . grade ,
200+ course . finalPercent ,
201+ course . assignments ,
202+ ) ) ;
203+ }
204+
205+ return courses ;
195206 }
196- return courses ;
197207 }
198208}
199209
@@ -204,14 +214,43 @@ async function getSavedGrades (username) {
204214 * @param {Course[] } courses list of course objects to save
205215 */
206216async function saveGradesLocally ( username , courses ) {
207- const user_data = { } ;
217+ const data = await getLocalConfig ( ) || { } ;
218+
219+ if ( data . opted_in === undefined ) {
220+ data . opted_in = {
221+ value : true ,
222+ changed : false ,
223+ } ;
224+ }
225+
226+ if ( data . showExtensionInfo === undefined ) {
227+ data . showExtensionInfo = {
228+ value : true ,
229+ changed : false ,
230+ } ;
231+ }
232+
233+ const user_data = await browser . storage . local . get ( "user_data" ) || { } ;
208234 const course_list = [ ] ;
209235 for ( let i = 0 ; i < courses . length ; i ++ ) {
210236 course_list . push ( courses [ i ] . toObject ( ) ) ;
211237 }
212238 user_data [ "USERDATA_" + username ] = { "courses" : course_list } ;
213- user_data . most_recent_user = username ;
214- browser . storage . local . set ( user_data ) ;
239+
240+ data . user_data = user_data ;
241+ data . most_recent_user = username ;
242+
243+ browser . storage . local . set ( data ) ;
244+ }
245+
246+ /**
247+ * Retrieves the config from the browser's local storage
248+ * @async
249+ * @returns {Config } an object representing the user's config from the browser's local storage
250+ */
251+ async function getLocalConfig ( ) {
252+ const data = await browser . storage . local . get ( null ) ;
253+ return data ;
215254}
216255
217256export {
0 commit comments