NUSMods Export Planner

Export and import NUS mods' planner

目前為 2025-02-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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>';

})();