Skip to content

Instantly share code, notes, and snippets.

@leleogere
Last active March 5, 2025 21:28
Show Gist options
  • Select an option

  • Save leleogere/0d021d26bde8ff5fba3006c1d0c309fb to your computer and use it in GitHub Desktop.

Select an option

Save leleogere/0d021d26bde8ff5fba3006c1d0c309fb to your computer and use it in GitHub Desktop.
Warn before closing a GitHub new issue tab

GitHub issue close confirmation dialog

Description

This GreaseMonkey/TamperMonkey/ViolentMonkey script aims at solving the issue raised in https://github.com/orgs/community/discussions/42581, i.e. closing an unsubmitted GitHub issue, and losing quite a lot of work.

This will display a confirmation popup before closing a tab with an unsubmitted issue, so that you don't close it by mistake.

I've tested it with Firefox (the custom message does not work due to this, but I left it as it might work for other browsers) and ViolentMonkey.

You should be able to use this link to install the script, provided that you have a UserScript manager such as GreaseMonkey/TamperMonkey/ViolentMonkey.

IMPORTANTE NOTE: The way GitHub navigates between pages makes this script more complex than it should be (see version history and discution with @paponius below), that's why now the script constantly monitors if the URL matches the new issue URL template rather than relying on browser for notifying a page change.

Changelog

v1.6

Fix a typo and improve confirmation message.

v1.5

Changed implementation to be more robust with how GitHub manages page changes (thanks @paponius). Now, the script query the URL every 60 seconds on the whole GitHub website, and only triggers when it detects that it matches the URL of a new issue. Note that this has the downside of not displaying the popup if you close the tab before the first check (in the worst case 60 seconds). I guess that no one would have the time to write anything that valuable in 60 seconds, and I wanted to keep this interval high to avoid consumming too much ressources (as it runs on any GitHub page).

v1.4

Implemented some @paponius suggestions:

  • Changed the title and body selectors so that they do not depend on unstable IDs
  • Changed the extension to .user.js for better integration with UserScript managers
  • Added a 1s delay before getting the elements as sometimes they would not be loaded yet when the script starts (and the selectors would return null)

v1.2

  • Changed title and body ID from issue_title and issue_body to :r1: and :r6:.
// ==UserScript==
// @name Confirm before closing unsubmitted GitHub issues
// @author leleogere
// @description Warn before closing a tab with an unsubmitted GitHub issue
// @version 1.6
// @match https://github.com/*
// ==/UserScript==
// check the URL every 60s to see if it matches the new issue URL
setInterval( () => {
'use strict';
// wait 3s for "page change" to settle
setTimeout( () => {
// early exit
if (!window.location.pathname.includes('/issues/new')) { return; };
let hasFormBeenModified = false;
// Selectors for input fields on the issue form
const titleSelector = 'input[class*="TextInput"]';
const bodySelector = 'textarea[class*="Textarea"]';
// Monitor input fields for changes
const monitorChanges = () => {
const titleField = document.querySelector(titleSelector);
const bodyField = document.querySelector(bodySelector);
// console.log(titleField)
// console.log(bodyField)
if (titleField && bodyField) {
titleField.addEventListener('input', () => {
hasFormBeenModified = true;
});
bodyField.addEventListener('input', () => {
hasFormBeenModified = true;
});
}
};
// Warn user when trying to close the tab
window.addEventListener('beforeunload', (event) => {
if (hasFormBeenModified) {
const message = 'You have a saved draft of an unsubmitted Issue, which you can still recover using "New Issue" button (in this tab). Closing this tab will remove the draft.';
event.returnValue = message; // Standard for older browsers
return message; // For modern browsers
}
});
monitorChanges();
}, 3000);
}, 60000);
@leleogere
Copy link
Author

I fixed the typo and modified the message. However, note that on some browsers (such as Firefox), that message won't be displayed, and a generic confirmation dialog will pop up instead (see this discussion).

@paponius
Copy link

paponius commented Mar 5, 2025

yes, no browser will show it today. Not just Firefox.
I meant two different messages, based on the page the user is on when trying to close, but it's irrelevant as it's ignored.
I tried to inject the message to DOM, using two events, but that's also not possible.

I believe this is the best that can be done using beforeunload event. I am leaving it at that. Still better than nothing. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment