Filter the keys in `localStorage` before processing them
authorAlexander Ebert <ebert@woltlab.com>
Thu, 5 Nov 2020 16:27:36 +0000 (17:27 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 5 Nov 2020 16:27:36 +0000 (17:27 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Autosave.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Redactor/Autosave.ts

index b0a5f1373cbde06c8ee69c6dc7ab2de089e0c01d..6c1d351b9712b291147f1532b15bf56104a76fca 100644 (file)
@@ -249,11 +249,9 @@ define(["require", "exports", "tslib", "../../Core", "../../Devtools", "../../Ev
          */
         _cleanup() {
             const oneWeekAgo = Date.now() - 7 * 24 * 3600 * 1000;
-            Object.keys(window.localStorage).forEach((key) => {
-                // check if key matches our prefix
-                if (!key.startsWith(Core.getStoragePrefix())) {
-                    return;
-                }
+            Object.keys(window.localStorage)
+                .filter((key) => key.startsWith(Core.getStoragePrefix()))
+                .forEach((key) => {
                 let value = "";
                 try {
                     value = window.localStorage.getItem(key) || "";
index d4751d5083a045e096da6dc0692cc9a55cbac222..1bb6e3cbcd5bb14aec41629264140b85310f3bf4 100644 (file)
@@ -318,37 +318,34 @@ class UiRedactorAutosave {
   protected _cleanup(): void {
     const oneWeekAgo = Date.now() - 7 * 24 * 3_600 * 1_000;
 
-    Object.keys(window.localStorage).forEach((key) => {
-      // check if key matches our prefix
-      if (!key.startsWith(Core.getStoragePrefix())) {
-        return;
-      }
-
-      let value = "";
-      try {
-        value = window.localStorage.getItem(key) || "";
-      } catch (e) {
-        const errorMessage = (e as Error).message;
-        window.console.warn(`Unable to access local storage: ${errorMessage}`);
-      }
-
-      let timestamp = 0;
-      try {
-        const content: AutosaveContent = JSON.parse(value);
-        timestamp = content.timestamp;
-      } catch (e) {
-        // We do not care for JSON errors.
-      }
-
-      if (!value || timestamp < oneWeekAgo) {
+    Object.keys(window.localStorage)
+      .filter((key) => key.startsWith(Core.getStoragePrefix()))
+      .forEach((key) => {
+        let value = "";
         try {
-          window.localStorage.removeItem(key);
+          value = window.localStorage.getItem(key) || "";
         } catch (e) {
           const errorMessage = (e as Error).message;
-          window.console.warn(`Unable to remove from local storage: ${errorMessage}`);
+          window.console.warn(`Unable to access local storage: ${errorMessage}`);
         }
-      }
-    });
+
+        let timestamp = 0;
+        try {
+          const content: AutosaveContent = JSON.parse(value);
+          timestamp = content.timestamp;
+        } catch (e) {
+          // We do not care for JSON errors.
+        }
+
+        if (!value || timestamp < oneWeekAgo) {
+          try {
+            window.localStorage.removeItem(key);
+          } catch (e) {
+            const errorMessage = (e as Error).message;
+            window.console.warn(`Unable to remove from local storage: ${errorMessage}`);
+          }
+        }
+      });
   }
 }