Intercepting a close file event

Is there any way of intercepting a close file event so I could automatically run a procedure to export changed tiddlers?

Bobj

There is a javascript-based event called “beforeunload” that can be triggered when you try to close a browser window.

The TWCore uses this to put up a special-purpose browser confirmation message box (“Changes you made may not be saved”) which lets you choose between two buttons: “Leave” or “Cancel”.

However, for security reasons, this message box can ONLY be used to display a simple text message and offer those two choices. The “beforeunload” event cannot be used to trigger other processing activities (such as autosaving or exporting changed tiddlers).

see Window: beforeunload event - Web APIs | MDN for technical details.

-e

I guess the simplest way out for me is to have a global button, called close wiki, which the. Lets me do whatever I need and closes the wiki when completed. Not as elegant as the event way but at least controllable actions as required.

Bobj

Thought I’d ask ChatGPT. Its reply:

TiddlyWiki is a single-page application (SPA) typically run in a browser, so “quitting” it doesn’t follow the same semantics as a typical desktop application. However, there are a few programmable ways to simulate quitting or trigger saving and closing depending on the context. Here’s a breakdown of possibilities based on usage scenarios:


:white_check_mark: In a Browser (Standard HTML file)

You can simulate a “quit” by doing one of the following:

1. Trigger Save and Close Tab

This requires custom scripting, often in a browser extension or userscript, because JavaScript from within a page cannot close the window it didn’t open.

However, you can do this:

// Attempt to save and close the tab
if (confirm("Save and close TiddlyWiki?")) {
  $tw.rootWidget.invokeActionString("save-all"); // Save (if configured)
  window.close(); // Only works if the window was opened via script
}

:warning: window.close() only works if the tab was opened by JavaScript (e.g., via window.open()); otherwise, the browser will block it.


:white_check_mark: Node.js TiddlyWiki (Server Mode)

If you’re running TiddlyWiki as a Node.js server (tiddlywiki --listen), then:

1. Graceful Shutdown

You can quit programmatically with something like this in your custom Node.js plugin or script:

const process = require('process');
process.exit(0); // Clean exit

Or send a SIGINT (e.g., Ctrl+C in terminal or via code).

2. From Inside the Wiki (Experimental)

Create a tiddler with a button that calls a command using an ActionWidget:

<$button>
Quit Server
<$action-sendmessage $message="tm-exit"/>
</$button>

But tm-exit isn’t a standard message. You’d have to hook it in a plugin with a message handler that calls process.exit().


:white_check_mark: Electron-based TiddlyWiki Apps

If you’re running TiddlyWiki inside Electron (as a desktop app), you can programmatically quit using Electron’s API:

const { app } = require('electron');
app.quit();

You’d need to connect this logic to a widget in TiddlyWiki that sends a message to the Electron backend.


Summary

Platform Method
Browser (HTML) Save via ActionWidget, but cannot close the tab reliably
Node.js Server Use process.exit() in plugin or script, or send SIGINT
Electron App Call app.quit() from backend, possibly triggered by TiddlyWiki event
Userscript Inject JS to save + close tab (with permission)

This has come up a number of times in my own research.

We have a plethora of startup and load actions but none enable a hackable exit.

Designers could use this in a range of cases such as saving the last timer state for timed wiki or activity use. Checking a document back in for other users to access and more.

  • Any trigger can cause various actions to occur but we dont have a reliable on exit, although we could add it to save.

If we created a logout or exit process that one had to pass through or at least encoraged the user to do it would work for most use cases but it would be great if the warning message could be leveraged to trigger tiddlywiki actions.

Creating a wrapper that allows initial opening of the wiki to occur through a window.open() we may find away to leverage the window close to trigger actions. Idealy there could be a $:/system/onexit-actions tag, allowing any action(s) to be triggered.