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 tired navigate event, with no success,
tried to mod history.replaceState and pushState, which GitHub might be using. nothing.
Then DOM observers. But the React always does something else. There are multiple points in DOM where it removes and adds stuff, during load. Even after it's done, will just remove something for a split of a second only. That's a problem as it removes Observers. Need Observers to watch and redo other Observers.
And then always something else is needed to watch, depending on which page the browsing starts. i.e. github.com/, or repo root, or on page ending .../issues, or directly /issues/new.
I gave up on following all DOM changes and just used timer, but 1s was too much for fast clicking, now 500ms seems fine.
Not sure if it covers all cases. Just tried to start (F5) on repo root and /Issues pages. And it seems to work.
Both these experiments are here
Of course solutions like permanent setInterval to check window.location, or Observer on BODY with change to all children are possible. I just wouldn't like that.