Skip to content

Instantly share code, notes, and snippets.

@ZhangYiJiang
Last active December 25, 2017 07:47
Show Gist options
  • Save ZhangYiJiang/69a47a58b06c25615793b38eaec6d9d1 to your computer and use it in GitHub Desktop.
Save ZhangYiJiang/69a47a58b06c25615793b38eaec6d9d1 to your computer and use it in GitHub Desktop.
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);
@yangshun
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment