Follow TV Tropes

Following

Context Tropers / Ham-peas

Go To

1Hi there.
2
3I use Emacs and Firefox. In Emacs, Control-R is the "search backwards" keystroke. In Firefox, of course, it's "reload this page". This has occasionally cost me large and complex edits to trope pages. Eventually that annoyed me enough to do something about it. That something is this user script. Install and enable it, and it will make your browser warn you before you throw away edits.
4
5@@[=
6// ==UserScript==
7// @name TV Tropes editbox dirty warning
8// @namespace http://ham-peas.nowhere/userscripts/tvtropes-editbox-dirty-warning
9// @include http://tvtropes.org/pmwiki/pmwiki.php/*?action=edit
10// @version 1.0.20150825180000
11// @grant none
12// ==/UserScript==
13
14var editbox = document.querySelector('textarea#editarea');
15var submit = document.querySelector('form#inputform > input[type=submit]');
16var warnBound = false;
17var submitted = false;
18
19function bindWarning() {
20 editbox.addEventListener('keydown', function() {
21 var warning = 'You have unsaved edits. Throw them away?';
22
23 if (!warnBound) {
24 window.addEventListener('beforeunload', function(event) {
25 if (!submitted) {
26 event.returnValue = warning; // Gecko, Trident (and standard)
27 return warning; // Gecko, Webkit (and non-standard)
28 }
29 return undefined;
30 });
31
32 submit.addEventListener('click', function() {
33 submitted = true;
34 });
35
36 warnBound = true;
37 }
38 });
39}
40
41window.addEventListener('DOMContentLoaded', bindWarning);
42=]@@
43
44I know it works in Firefox. It will almost certainly work in Chrome, Safari, or any other Webkit-based browser that isn't totally broken or ancient. It should probably work in Internet Explorer, but I never use that browser and I don't even know if it supports user scripts. If you find it's broken in your browser, PM me and I'll try to do something about that.
45
46It may or may not work in a mobile browser, depending on how that browser feels about standards, but if you're editing this site in a mobile browser, you almost certainly have bigger problems than accidentally throwing away the occasional edit. If it doesn't work in a mobile browser, you can PM me about that if you like, but I almost certainly won't be able to do anything about it.
47
48In any case, if you PM me about a problem with this script, tell me:
49* what browser you're using
50* which version it is
51* which version of the user script you have installed
52* what error message (if any) you're seeing in the browser console.
53
54That's the minimum information I'll need to be able to investigate the problem at all. If you don't include it, I will delete your PM without a response and go on about my day. (Please don't PM me asking how to find that information, or how to install a user script. That's what Google, and your browser's documentation, are for.)
55
56Before you PM me, check that your installed version of the user script is up to date with the version published above. If you're running an older version, I probably updated it to fix whatever bug you're seeing. Install the new version and try again. If you still have a problem after the update, PM me.
57

Top