save.js 939 B

123456789101112131415161718192021222324252627282930
  1. setup.editorSave = (passage,newContent) => {
  2. const escapedChars = {
  3. '&' : '&',
  4. '<' : '&lt;',
  5. '>' : '&gt;',
  6. '"' : '&quot;',
  7. "'" : '&#39;',
  8. '`' : '&#96;'
  9. };
  10. let sanitizedNewContent = newContent;
  11. for(const [toBeEscaped, asEscaped] of Object.entries(escapedChars))
  12. sanitizedNewContent = sanitizedNewContent.replaceAll(toBeEscaped,asEscaped);
  13. const localStorageModsKey = "SweetCube.Modifications";
  14. const updateObject = {};
  15. updateObject[passage] = sanitizedNewContent;
  16. const currentModificationsString = localStorage.getItem(localStorageModsKey) ?? "{}";
  17. const currentModifications = JSON.parse(currentModificationsString);
  18. const newModifications = Object.assign({},currentModifications,updateObject);
  19. const newModificationsString = JSON.stringify(newModifications);
  20. localStorage.setItem(localStorageModsKey, newModificationsString);
  21. location.reload();
  22. }