From: Alexander Ebert Date: Thu, 5 Nov 2020 16:27:36 +0000 (+0100) Subject: Filter the keys in `localStorage` before processing them X-Git-Tag: 5.4.0_Alpha_1~636^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c1bded53d3c333c894dcbcbadfcecf549b4c23ff;p=GitHub%2FWoltLab%2FWCF.git Filter the keys in `localStorage` before processing them --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Autosave.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Autosave.js index b0a5f1373c..6c1d351b97 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Autosave.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Autosave.js @@ -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) || ""; diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Redactor/Autosave.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Redactor/Autosave.ts index d4751d5083..1bb6e3cbcd 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Redactor/Autosave.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Redactor/Autosave.ts @@ -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}`); + } + } + }); } }