NUSMods Export Planner

Export and import NUS mods' planner

当前为 2025-02-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         NUSMods Export Planner
// @namespace    http://tampermonkey.net/
// @version      2025-02-27
// @description  Export and import NUS mods' planner
// @author       Someone
// @match        https://nusmods.com/planner
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nusmods.com
// @run-at       context-menu
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    console.log('Script start');

    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();
        }
    }

    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>';

})();