Skip to content

Commit ad6f713

Browse files
Fix: deal with edge cases!
1. If there is no <div id=“expiry-time”>…</div> item, assign `NaN` to expiryTime; 2. Check to see if expiryTime is valid; if not, short-circuit this and continue processing; 3. If it *is* a valid expiryTime, *and* our date is already past the expiry time, then yes, go ahead.
1 parent a50d968 commit ad6f713

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

gwynethllewelyn/postlocalstorage/styles/all/template/postlocalstorage_functions.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@
242242
}, { once: true }
243243
);
244244

245-
// When the form is submitted, delete the localStorage key/value pair.
245+
// When the form is submitted via clicking on the 'Submit' built-in button,
246+
// delete the localStorage key/value pair.
246247
textarea.form.addEventListener(
247248
"submit",
248249
/**
@@ -254,24 +255,34 @@
254255
* Retrieve the session expiry time that was stamped on the post submit page.
255256
* Force it to a number!
256257
*
258+
* Note: 1) if the div element does not exist, expiryTime becomes `NaN`
259+
* 2) if it exists but has not been properly initialised, expiryTime becomes `0`
260+
* In both cases, we will skip the test for expiryTime
257261
* @type {number}
258262
*/
259-
const expiryTime = parseInt(document.getElementById('expiry-time').innerText.trim(), 10);
263+
const expiryTime = parseInt(document.getElementById('expiry-time')?.innerText.trim(), 10);
260264
const dateNow = Math.floor(Date.now() / 1000); // we get milliseconds, so we need to convert to seconds.
261265
console.debug("Date.now() in seconds is " + dateNow + " and expiryTime is " + expiryTime);
262266

263267
//the if statement for deleting local storage in PM'ing, because expiryTime = 0, it must be fixed
264-
if (!key.includes("ucp.php")) {
265-
if (dateNow > expiryTime) {
268+
//if (!key.includes("ucp.php")) {
269+
//
270+
// Now we do 2 things:
271+
// - the error came from an event not being fired for PMs;
272+
// - just to be absolutely sure, do first an extra check for expiryTime being non-zero
273+
// (gwyneth 20240425)
274+
if ((expiryTime) && (dateNow > expiryTime)) {
266275
// We won't clear anything if the session already expired, so return.
267276
return;
268-
}
269277
}
278+
//}
270279
// Now remove the local storage on `Submit` — it'll get saved to the database as a post/PM,
271280
// so we don't need it around any longer.
272-
// ... except on Preview. We still want to keep the storage around during preview!
281+
// ... except on Preview (or draft, or whatever). We still want to keep the storage around during preview!
273282
// Kudos to @kylesands for this (gwyneth 20240416)
274-
if (document.activeElement.tagName.toLowerCase() == "input" && document.activeElement.value.toLowerCase() == ("submit") || document.activeElement.value.toLowerCase() == ("υποβολή")) { // Added to only clear on Input button with Submit value
283+
// Fixed lack of translation issue by using the 'name' value instead, but if either does
284+
// not exist, localStorage will remain untouched.
285+
if (document.activeElement.tagName?.toLowerCase() == "input" && document.activeElement.name?.toLowerCase() == "post") { // Added to only clear on Input button with Submit value
275286
message.localStorage.removeItem(key);
276287
message.removeEventListener(unloadEvent, updateStorage);
277288
console.debug("Text submitted (not in preview!); removed from localStorage");

0 commit comments

Comments
 (0)