Alerting users about closing if a variable is true

I have a simple editor that sets document.firstModified to true when the user enters anything into the textbox. Now, how would I go about stopping the closing of the window, and asking the user to save or discard?

You can use the vanilla browser javascript beforeunload or the Electron main process close event of BrowserWindow to prevent closing the window. For asking the user to save/discard if you want to use a native OS dialog use Electron’s showMessageBox of dialog. Simple example:

const dialog = require('electron').remote.dialog;
window.addEventListener('beforeunload', function handleWindowClose(event) {
	if (dialog.showMessageBox({type: "question", buttons: ["Quit", "Cancel"], defaultId: 0, message: "Do you really want to quit?"})) {
		event.preventDefault();
		event.returnValue = '';
	}
});