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.
Fix a typo and improve confirmation message.
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).
Implemented some @paponius suggestions:
- Changed the title and body selectors so that they do not depend on unstable IDs
- Changed the extension to
.user.jsfor 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)
- Changed title and body ID from
issue_titleandissue_bodyto:r1:and:r6:.
I updated the script with your last implementation. I raised the URL monitor interval to 60s to limit resources even more. I think that it's ok to lose (at worst) 60s of work in case of mistake (which could be recovered by unclosing the tab as you suggested anyway).
There are always edge cases, but I think that's better than nothing.
I've found another issue though: on some repos (such as https://github.com/ActiveState/code/issues), the "New issue" button opens a popup rather than a new page, so the URL does not change. But supporting that would make the script more complex (as it would need to check for elements rather than simply the URL).