Skip to content

Commit 4f7a0c9

Browse files
authored
Merge pull request #243 from priime0/master
2 parents 7125649 + 0dc2e45 commit 4f7a0c9

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

src/js/helpers.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,24 @@ function assignments (node) {
186186
*/
187187
async 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
*/
206216
async 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

217256
export {

src/js/saspowerschoolff.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,28 @@ function class_page () {
113113

114114
async function login_page () {
115115
$('<div id="saspes-info"></div>').insertAfter('div#content');
116-
browser.storage.local.get({ showExtensionInfo: true }).then(result => {
116+
browser.storage.local.get("showExtensionInfo").then(output => {
117117
new (Vue.extend(ExtensionInfo))({
118118
data: {
119-
showInfo: result.showExtensionInfo,
119+
showInfo: output.showExtensionInfo.value,
120120
},
121121
}).$mount('#saspes-info');
122122
});
123+
123124
const LastGradesDiv = document.createElement('div');
124125
LastGradesDiv.classList.add("last-grade-div-fixed");
125126
LastGradesDiv.id = "saspes-last-grades";
126127
document.body.appendChild(LastGradesDiv);
127-
if ((await browser.storage.local.get({ save_last_grades: true })).save_last_grades) {
128+
129+
if ((await browser.storage.local.get("opted_in")).opted_in.value) {
128130
(browser.storage.local.get("most_recent_user")).then(output => {
129-
if (output.most_recent_user !== undefined) {
131+
const most_recent_user = output.most_recent_user;
132+
if (most_recent_user !== undefined) {
130133
(async () => {
131-
const courses = await getSavedGrades(output.most_recent_user);
134+
const courses = await getSavedGrades(most_recent_user);
132135
new (Vue.extend(LastSeenGrades))({
133136
propsData: {
134-
username: output.most_recent_user,
137+
username: most_recent_user,
135138
initialCourses: courses,
136139
},
137140
}).$mount(".last-grade-div-fixed");

0 commit comments

Comments
 (0)