Skip to content

Instantly share code, notes, and snippets.

@ZhangYiJiang ZhangYiJiang/extract.js
Last active Dec 25, 2017

Embed
What would you like to do?
v2 -> v3 data migration test scripts
// NUSMods v2 timetable data extractor
var loaderTemplate = data => `
var s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/localforage/1.5.5/localforage.min.js';
s.onload = function() {
const data = ${data};
let count = 0;
data.forEach(([key, queryString]) => {
localforage
.setItem(key, queryString)
.then(() => {
count++;
if (count === data.length) {
localStorage.clear();
window.location.reload();
}
});
});
};
document.body.append(s);`;
var s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/localforage/1.5.5/localforage.min.js';
s.onload = function() {
const keys = [
'timetable/2017-2018/sem1:queryString',
'timetable/2017-2018/sem2:queryString',
'timetable/2017-2018/sem3:queryString',
'timetable/2017-2018/sem4:queryString',
];
const data = [];
keys.forEach(key => localforage.getItem(key)
.then(queryString => {
data.push([key, queryString]);
if (data.length === keys.length) {
const stringified = JSON.stringify(data, null, 2);
console.log('Data extracted! Go to https://deploy-preview-518--nusmods.netlify.com/ and paste the code below into the console');
console.log(loaderTemplate(stringified));
}
}))
};
document.body.append(s);
@ZhangYiJiang

This comment has been minimized.

Copy link
Owner Author

ZhangYiJiang commented Dec 16, 2017

To use:

  1. Run the extractor.js code in the browser console (Ctrl + Shift + J) on https://nusmods.com/
  2. Copy the outputted code from the console
  3. Go to https://deploy-preview-518--nusmods.netlify.com/ and paste the code into the console there. The page will reload after the v2 data have been saved.
  4. Verify that the the timetable has been migrated successfully for all four semesters. This means that
    • The modules match
    • The position of lessons match
    • localStorage should have an extra v2Migration key that indicates the migration has been completed successfully
@yangshun

This comment has been minimized.

Copy link

yangshun commented Dec 16, 2017

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.