Skip to content

Commit

Permalink
Chore: fine-tuning the localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
GwynethLlewelyn committed Apr 16, 2024
1 parent ac4dcf2 commit b7ab7bf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

For a _much_ more detailed version, see the [commit history](https://github.com/GwynethLlewelyn/post-local-storage/commits/master/) on GitHub.

## 1.1.0
- Bug: when user clicks on Preview, local storage gets cleared by mistake. Reported & fixed by @kylesands (see https://www.phpbb.com/customise/db/extension/postlocalstorage/support/topic/246115?p=877342#p877342)
- Added some checks to deal with stale local storage. This is an extreme case when people just happen to completely forget that they still have a message waiting for them for that particular URL. While this is an edge case, it has been pointed out by the validators. Note that old objects (without timestamps) will still be dealt with.

## 1.0.5
- Renamed `custom_functions.js` to `postlocalstorage_functions.js` as per validation suggestion
- Fix bad JSON indentation on `version_check.json` which prevented version checking from working at all
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ Then go to the **ACP** > **Extensions**, on the sidebar: **Extension Management*

There is no further configuration required.

## Upgrading

There was a bug on the pre-

## Acknowledgements

Many thanks to
[@Tread](https://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=1973496) and [@cabot](https://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=1337922) for gently 'persuading' me to publish this code as a phpBB3.3 extension, by pointing me to all the appropriate bits and pieces of code to be 'assembled' into an extension. Yay!

Kudos to [@gvp9000](https://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=2227069) for finding & fixing an obscure bug that affects Firefox.

Kudos to [@kylesands](https://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=2218926) for pointing out and providing a fix for [incorrect local storage deletion during Preview](https://www.phpbb.com/customise/db/extension/postlocalstorage/support/topic/246115?p=877342#p877342).

Logo designed by [DALL·E 2](https://openai.com/product/dall-e-2).

![phpBB3 Logo](https://img.shields.io/badge/phpBB-3.3-blue) [![CodeQL](https://github.com/GwynethLlewelyn/post-local-storage/actions/workflows/codeql.yml/badge.svg)](https://github.com/GwynethLlewelyn/post-local-storage/actions/workflows/codeql.yml) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/83a20d04433341baa65c78d29fc3410a)](https://www.codacy.com/gh/GwynethLlewelyn/post-local-storage/dashboard?utm_source=github.com&utm_medium=referral&utm_content=GwynethLlewelyn/post-local-storage&utm_campaign=Badge_Grade) [![Codacy Security Scan](https://github.com/GwynethLlewelyn/post-local-storage/actions/workflows/codacy.yml/badge.svg)](https://github.com/GwynethLlewelyn/post-local-storage/actions/workflows/codacy.yml) [![Liberapay](https://img.shields.io/liberapay/receives/GwynethLlewelyn.svg?logo=liberapay")](https://liberapay.com/GwynethLlewelyn/donate)
Expand Down
2 changes: 1 addition & 1 deletion gwynethllewelyn/postlocalstorage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "phpbb-extension",
"description": "A very simple phpBB3 extension to locally store the content of a post being written, to avoid losing everything after a crash or session expiration.",
"homepage": "https://github.com/GwynethLlewelyn/post-local-storage",
"version": "1.0.5",
"version": "1.1.0",
"time": "2024-04-15",
"license": "GPL-2.0-only",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
// 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.

(function(message, doc) {
// One year = 31536000000 milliseconds.
const FRESHNESS_INTERVAL = 365 * 24 * 60 * 60 * 1000;

// If there is no localStorage support, give up
if (!message.localStorage) return;
if (!message.localStorage) {
console.debug("no local storage support");
return;
}
// phpBB3 uses a textarea with id and name 'message'
var textarea = doc.querySelector("textarea#message");
// no point in being around if this is nil; also: avoids crashing below (gwyneth 20220303)
if (!textarea) return;
if (!textarea) {
console.debug("no phpBB3 textarea found");
return;
}
// The key for the key/value pair in localStorage is the current URL.
var key = message.location.href;
// Firefox seems to have an odd bug which affects clicking backspace in quick succession.
Expand All @@ -22,30 +31,53 @@
for (let i = 0; i < count_hash_num - 1; i++) {
key = key.substring(0, key.lastIndexOf('#'));
}
// JSON object to be saved with the textarea content + timestamp.
var item = null;
var item = null;
// Use the 'pagehide' event in modern browsers or 'beforeunload' in older browsers.
var unloadEvent;
if ("onpagehide" in message) {
unloadEvent = "pagehide";
} else {
unloadEvent = "beforeunload";
}
// Event to be used for saving content on demand, when user switches pages.
// Note: both the 'pagehide' or 'beforeunload' are deprecated and should not be used.
const unloadEvent = "visibilitychange";

// If there's a localStorage entry for the current URL, update the textarea with the saved value.
item = message.localStorage.getItem(key);
if (item) {
var data = JSON.parse(item);
textarea.value = data.content;
// Before 1.1.0, 'our' objects did not carry timestamps, so we need to check for them: (gwyneth 20240415)
if (Object.hasOwn(data, "timestamp") && data.timestamp) {
// check if data is stale.
if (Date.now() - data.timestamp > FRESHNESS_INTERVAL) {
// Remove stale data. A new item will be added as soon as the user clicks a key.
message.localStorage.removeItem(key);
console.debug("stale data found; removing from localStorage");
return;
}
// Data is still considered "fresh" enough, so we replace the value of the textarea with
// what we have on the localStorage.
textarea.value = data.content;
console.debug("textarea content successfully restored");
} else {
// We don't know if the existing data is stale or not, since it comes from pre-1.1.0 times.
// So, upgrade object to the new format, i.e. add a timestamp to existing content.
let tempContent = data.content;
message.localStorage.removeItem(key);
item = JSON.stringify({ "content": tempContent, "timestamp": Date.now() });
message.localStorage.setItem(key, item);
}
}
// This function will store the current value of the textarea in localStorage (or delete it if the textarea is blank).
// This function will store the current value of the textarea in localStorage (or delete it if the textarea is blank) with a timestamp.
function updateStorage() {
// Note: if the visibilitychange event is being used, one should check to see if the visibilityState
// is 'hidden' or 'visible'; but we're going to save to storage in both cases.
if (message.visibilityState === "hidden") {
console.debug("Saving existing text before moving tab to background");
}
if (textarea.value) {
item = JSON.stringify({ content: textarea.value });
item = JSON.stringify({ "content": textarea.value, "timestamp": Date.now() });
message.localStorage.setItem(key, item);
} else {
message.localStorage.removeItem(key);
}
// This event listener is no longer needed now so remove it.
// Once the user presses another key, it will be added again!
message.removeEventListener(unloadEvent, updateStorage);
}
// When the user presses a key just *once* inside the textarea, run the storage function when the page is unloaded.
Expand All @@ -58,7 +90,11 @@
);
// When the form is submitted, delete the localStorage key/value pair.
textarea.form.addEventListener("submit", function() {
message.localStorage.removeItem(key);
message.removeEventListener(unloadEvent, updateStorage);
// ... except on Preview. We still want to keep the storage around during preview!
// Kudos to
if (document.activeElement.value != 'Preview') {
message.localStorage.removeItem(key);
message.removeEventListener(unloadEvent, updateStorage);
}
});
})(this, this.document);
2 changes: 1 addition & 1 deletion version_check.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"stable": {
"1.0": {
"current": "1.0.5",
"current": "1.1.0",
"download": "https:\/\/github.com\/GwynethLlewelyn\/post-local-storage\/releases\/download\/v1.0.5\/postlocalstorage.zip",
"announcement": "https:\/\/www.phpbb.com\/customise\/db\/extension\/postlocalstorage\/",
"eol": null,
Expand Down

0 comments on commit b7ab7bf

Please sign in to comment.