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:.
With various repos, it was always something else.
:r1l:, :r6:, :r1q
but it's not even "1" and "6" respectively. Once I observed: title:
:r1l:and comment:r1q:I did this:
add .user. to the filename, so it can be installed and auto-updated from this gist comfortably.
This would then be a stable link to click (with installed GM/TP/VM):
https://gist.github.com/leleogere/0d021d26bde8ff5fba3006c1d0c309fb/raw/confirm_before_closing_github_issue.user.js
It might happen that load event is missed. (with some versions of some UserScript managers), maybe would be safer to check
document.readyState === 'complete'(I use this)edit: TM doc says both are cached and delivered when UserScript is ready to receive them. I tested it today. It seems to be an issue of the past. Disregard the 3. point.