|
| 1 | +// Copyright (C) 2023 Gwyneth Llewelyn |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2. |
| 4 | +// |
| 5 | +// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 6 | +// |
| 7 | +// You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 8 | + |
| 9 | +(function (message, doc) { |
| 10 | + // If there is no localStorage support, give up |
| 11 | + if (!message.localStorage) return; |
| 12 | + // phpBB3 uses a textarea with id and name 'message' |
| 13 | + var textarea = doc.querySelector("textarea#message"); |
| 14 | + // no point in being around if this is nil; also: avoids crashing below (gwyneth 20220303) |
| 15 | + if (!textarea) return; |
| 16 | + // The key for the key/value pair in localStorage is the current URL. |
| 17 | + var key = message.location.href; |
| 18 | + var item = null; |
| 19 | + // Use the 'pagehide' event in modern browsers or 'beforeunload' in older browsers. |
| 20 | + var unloadEvent; |
| 21 | + if ("onpagehide" in message) { |
| 22 | + unloadEvent = "pagehide"; |
| 23 | + } else { |
| 24 | + unloadEvent = "beforeunload"; |
| 25 | + } |
| 26 | + // If there's a localStorage entry for the current URL, update the textarea with the saved value. |
| 27 | + item = message.localStorage.getItem(key); |
| 28 | + if (item) { |
| 29 | + var data = JSON.parse(item); |
| 30 | + textarea.value = data.content; |
| 31 | + } |
| 32 | + // This function will store the current value of the textarea in localStorage (or delete it if the textarea is blank). |
| 33 | + function updateStorage() { |
| 34 | + if (textarea.value) { |
| 35 | + item = JSON.stringify({ content: textarea.value }); |
| 36 | + message.localStorage.setItem(key, item); |
| 37 | + } else { |
| 38 | + message.localStorage.removeItem(key); |
| 39 | + } |
| 40 | + // This event listener is no longer needed now so remove it. |
| 41 | + message.removeEventListener(unloadEvent, updateStorage); |
| 42 | + } |
| 43 | + // When the user presses a key just *once* inside the textarea, run the storage function when the page is unloaded. |
| 44 | + textarea.addEventListener( |
| 45 | + "keyup", |
| 46 | + function () { |
| 47 | + message.addEventListener(unloadEvent, updateStorage); |
| 48 | + message.setInterval(updateStorage, 10000); |
| 49 | + }, |
| 50 | + { once: true } |
| 51 | + ); |
| 52 | + // When the form is submitted, delete the localStorage key/value pair. |
| 53 | + textarea.form.addEventListener("submit", function () { |
| 54 | + message.localStorage.removeItem(key); |
| 55 | + message.removeEventListener(unloadEvent, updateStorage); |
| 56 | + }); |
| 57 | +})(this, this.document); |
0 commit comments