Tropers / Ham-peas

Hi there.

I 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.

// ==UserScript==
// @name  TV Tropes editbox dirty warning
// @namespace http://ham-peas.nowhere/userscripts/tvtropes-editbox-dirty-warning
// @include*?action=edit
// @version  1.0.20150825180000
// @grant  none
// ==/UserScript==

var editbox = document.querySelector('textarea#editarea');
var submit = document.querySelector('form#inputform > input[type=submit]');
var warnBound = false;
var submitted = false;

function bindWarning() {
 editbox.addEventListener('keydown', function() {
 var warning = 'You have unsaved edits. Throw them away?';

 if (!warnBound) {
  window.addEventListener('beforeunload', function(event) {
  if (!submitted) {
   event.returnValue = warning; // Gecko, Trident (and standard)
   return warning;       // Gecko, Webkit (and non-standard)
  return undefined;

  submit.addEventListener('click', function() {
  submitted = true;

  warnBound = true;

window.addEventListener('DOMContentLoaded', bindWarning);

I 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.

It 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.

In any case, if you PM me about a problem with this script, tell me:
  • what browser you're using
  • which version it is
  • which version of the user script you have installed
  • what error message (if any) you're seeing in the browser console.

That'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.)

Before 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.

Ideally, this functionality would be part of TV Tropes' own code, so that it works for everybody instead of just me and anyone who happens to stumble over it here. I don't know where to make that suggestion. If you do know where, I encourage you to do so.