Export and import NUS mods
当前为
// ==UserScript==
// @name NUSMods Export Planner
// @namespace http://tampermonkey.net/
// @version 2025-02-29
// @description Export and import NUS mods
// @author Someone
// @match https://nusmods.com/planner
// @icon https://www.google.com/s2/favicons?sz=64&domain=nusmods.com
// @run-at document-end
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
var plannerFunctions = window.plannerFunctions = {};
plannerFunctions.importPlanner = function () {
if (confirm("Are you sure to overwrite the existing planner with new data?")) {
var i0 = document.createElement('input');
var f0;
i0.type = 'file';
i0.onchange = e => {
f0 = e.target.files[0];
try {
// setting up the reader
var reader = new FileReader();
reader.readAsText(f0,'UTF-8');
// here we tell the reader what to do when it's done reading...
reader.onload = readerEvent => {
var content = readerEvent.target.result; // this is the content!
console.log( content );
localStorage.setItem("persist:planner", content);
window.location.reload();
}
} catch (error) {
console.error(error);
alert(error);
}
}
i0.click();
}
}
plannerFunctions.exportPlanner = function () {
var plannerObj = localStorage.getItem("persist:planner");
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
download(plannerObj, 'planner.json', 'text/json');
}
plannerFunctions.clearPlanner = function () {
if (confirm("Are you ABSOLUTELY SURE that you want to remove all existing data from the planner?")) {
localStorage.removeItem("persist:planner");
window.location.reload();
}
}
const delay = ms => new Promise(res => setTimeout(res, ms));
window.addEventListener('load', async function () {
await delay(10000);
document.getElementsByTagName('h1')[0].innerHTML = 'Course Planner <button type="button" onclick="plannerFunctions.exportPlanner();">Export Planner</button> <button type="button" onclick="plannerFunctions.importPlanner();">Import Planner</button> <button type="button" onclick="plannerFunctions.clearPlanner();">Clear Planner</button>';
});
})();