Text2Mindmap Autosave

Autosaves mindmaps on Text2Mindmap

  1. // ==UserScript==
  2. // @name Text2Mindmap Autosave
  3. // @version 0.02
  4. // @description Autosaves mindmaps on Text2Mindmap
  5. // @author Domination9987
  6. // @match http*://www.text2mindmap.com/***********************************************************************************************************************
  7. // @grant none
  8. // @namespace http://your.homepage/
  9. // ==/UserScript==
  10.  
  11. //Plans
  12. //* Allow autosave without refreshing - Add an event listener to the close button, and inititate then
  13. //* Add a method of changing autosave time (input box)
  14.  
  15. var checkInt, saveInt;
  16. var timerInt = false;
  17. var currentTime = 0;
  18. var autosaveTime = localStorage.autosaveTime || 10;
  19. var saveCountdown = localStorage.saveCountdown || false;
  20.  
  21. Init();
  22. setTimeout(Init2, 1000);
  23.  
  24. function Init(){
  25. SaveFile();
  26. }
  27.  
  28. function Init2(){
  29. if ($("#savingStateSaved").css("display") == "inline"){
  30. CheckIfSaveNeeded();
  31. saveInt = setInterval(CheckIfSaveNeeded, 3000);
  32. } else {
  33. SetText("(Auto N/A)");
  34. }
  35. }
  36.  
  37. //Check if required to time
  38. function CheckIfSaveNeeded(){
  39. if ($(".saveMsg").css("display") === "none"){
  40. window.onbeforeunload = null;
  41. SetText("(saved)");
  42. StopTimer();
  43. } else {
  44. if (timerInt === false){
  45. window.onbeforeunload = function() {
  46. return "You have unsaved changes!";
  47. };
  48. timerInt = setInterval(Timer, 1000);
  49. clearInterval(saveInt);
  50. }
  51. }
  52. }
  53.  
  54. //Timer, called every second
  55. function Timer(){
  56. currentTime += 1;
  57. modulo = currentTime % autosaveTime;
  58. if (modulo === 0){
  59. SaveFile();
  60. SetText("(saving...)");
  61. StopTimer();
  62. } else {
  63. if (saveCountdown){
  64. SetText("(autosave " + (autosaveTime - (modulo)).toString() + ")");
  65. } else {
  66. SetText("(unsaved)");
  67. }
  68. }
  69. }
  70.  
  71. //Stops timer
  72. function StopTimer(){
  73. clearInterval(timerInt);
  74. timerInt = false;
  75. }
  76.  
  77. //Saving functions, called every 30 seconds
  78. function SaveFile(){
  79. OpenSave();
  80. checkInt = setInterval(CheckDone, 500);
  81. }
  82.  
  83. function CheckDone(){
  84. if ($("#savingFinished").css("display") == "block" || $("#savingStateUnsaved").css("display") == "block"){
  85. CloseSave();
  86. clearInterval(checkInt);
  87. TextFocus();
  88. saveInt = setInterval(CheckIfSaveNeeded, 3000);
  89. }
  90. }
  91.  
  92. //Changes the text
  93. function SetText(text){
  94. $('.tab-pane td').eq(1).find('h4').text("Outline your text " + text);
  95. }
  96.  
  97. //This function refocuses on the text area after being saved
  98. function TextFocus(){
  99. scrollTop = $("#textArea").scrollTop();
  100. scrollLeft = $("#textArea").scrollLeft();
  101. $("#textArea").focus();
  102. $("#textArea").scrollLeft(scrollLeft);
  103. $("#textArea").scrollTop(scrollTop);
  104. }
  105.  
  106. //This function opens a hidden save box
  107. function OpenSave(){
  108. $("#saveBtn").click();
  109. $(".modal-backdrop").css("display", "none");
  110. $("#saveModal").css("display", "none");
  111. }
  112.  
  113. //This function closes a hidden save box
  114. function CloseSave(){
  115. $("#saveModal").css("display", "");
  116. $(".modal-backdrop").css("display", "");
  117. $(".icon-close").parent().click();
  118. }
  119.  
  120.  
  121. //Keyboard Events
  122. $("#textArea").keydown(function (){
  123. modulo = currentTime % autosaveTime;
  124. if (modulo > autosaveTime - 3){
  125. currentTime += modulo - autosaveTime;
  126. }
  127. });