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 did not observe
null. Must be ReactJS doing its weird stuff.There is a parent
div[data-target="react-app.reactRoot"], you could put an Observer on it, as well as on the BODY, then on each removal/re-addition of child sub-tree register your listener again. Quite an overkill. If you are interested how and why, check comments in my "add size column to github".But why? Maybe even set the setTimeout to 20 sec and call it a feature.
No one can write much worth saving in 20 sec. And if they try to close the Tab as soon, probably they typed something by mistake or changed their mind.
A suggestion to README.md: