Disable page close confirmation

Disable page close confirmation (onBeforeUnload)

As of 2019-09-09. See the latest version.

// ==UserScript==
// @name        Disable page close confirmation
// @description Disable page close confirmation (onBeforeUnload)
// @namespace   http://nags.must.die
// @version     1.2
// @grant       none
// @run-at      document-start
// @match       *://*/*
// ==/UserScript==

(function () {
  function clearUnload() {
    window.onbeforeunload = null;
    window.onunload = null;
  }

  addEventListener('load', function _() {
    removeEventListener('load', _);
    clearUnload();
  });

  addEventListener('beforeunload', function(e) {
    e.stopImmediatePropagation();
    clearUnload();
  }, true);
})();