Text2Mindmap Autosave

Autosaves mindmaps on Text2Mindmap

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Text2Mindmap Autosave
// @version      0.02
// @description  Autosaves mindmaps on Text2Mindmap
// @author       Domination9987
// @match        http*://www.text2mindmap.com/***********************************************************************************************************************
// @grant        none
// @namespace http://your.homepage/
// ==/UserScript==

//Plans
//* Allow autosave without refreshing - Add an event listener to the close button, and inititate then
//* Add a method of changing autosave time (input box)

var checkInt, saveInt;
var timerInt = false;
var currentTime = 0;
var autosaveTime = localStorage.autosaveTime || 10;
var saveCountdown = localStorage.saveCountdown || false;

Init();
setTimeout(Init2, 1000);

function Init(){
    SaveFile();
}

function Init2(){
    if ($("#savingStateSaved").css("display") == "inline"){
        CheckIfSaveNeeded();
        saveInt = setInterval(CheckIfSaveNeeded, 3000);
    } else {
        SetText("(Auto N/A)");
    }
}

//Check if required to time
function CheckIfSaveNeeded(){
    if ($(".saveMsg").css("display") === "none"){
        window.onbeforeunload = null;
        SetText("(saved)");
        StopTimer();
    } else {
        if (timerInt === false){
            window.onbeforeunload = function() {
                return "You have unsaved changes!";
            };
            timerInt = setInterval(Timer, 1000);
            clearInterval(saveInt);
        }
    }
}

//Timer, called every second
function Timer(){
    currentTime += 1;
    modulo = currentTime % autosaveTime;
    if (modulo === 0){
        SaveFile();
        SetText("(saving...)");
        StopTimer();
    } else {
        if (saveCountdown){
            SetText("(autosave " + (autosaveTime - (modulo)).toString() + ")");
        } else {
            SetText("(unsaved)");
        }
    }
}

//Stops timer
function StopTimer(){
    clearInterval(timerInt);
    timerInt = false;
}

//Saving functions, called every 30 seconds
function SaveFile(){
    OpenSave();
    checkInt = setInterval(CheckDone, 500);
}

function CheckDone(){
    if ($("#savingFinished").css("display") == "block" || $("#savingStateUnsaved").css("display") == "block"){
        CloseSave();
        clearInterval(checkInt);
        TextFocus();
        saveInt = setInterval(CheckIfSaveNeeded, 3000);
    }
}

//Changes the text
function SetText(text){
    $('.tab-pane td').eq(1).find('h4').text("Outline your text " + text);
}

//This function refocuses on the text area after being saved
function TextFocus(){
    scrollTop = $("#textArea").scrollTop();
    scrollLeft = $("#textArea").scrollLeft();
    $("#textArea").focus();
    $("#textArea").scrollLeft(scrollLeft);
    $("#textArea").scrollTop(scrollTop);
}

//This function opens a hidden save box
function OpenSave(){
    $("#saveBtn").click();
    $(".modal-backdrop").css("display", "none");
    $("#saveModal").css("display", "none");
}

//This function closes a hidden save box
function CloseSave(){
    $("#saveModal").css("display", "");
    $(".modal-backdrop").css("display", "");
    $(".icon-close").parent().click();
}


//Keyboard Events
$("#textArea").keydown(function (){
    modulo = currentTime % autosaveTime;
    if (modulo > autosaveTime - 3){
        currentTime += modulo - autosaveTime;
    }
});