Let Me Out | TM Edition

Prevents actions (like alerts) when navigating away from a page.

  1. // ==UserScript==
  2. // @name Let Me Out | TM Edition
  3. // @namespace tedbigham.blogspot.com
  4. // @version 1.3
  5. // @description Prevents actions (like alerts) when navigating away from a page.
  6. // @author Ted Bigham
  7. // @license MIT
  8. // @match *://*/*
  9. // @icon https://lh3.googleusercontent.com/UQFgp7s3-HgSxCENa2xNzGQAJhbA6r4VkY1iLAuZrtTBkEQ3cidC5CrkzpyjwYyPK9sXTAECWz1ab_ZJJyLnc96YoKQ=s60
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Before injection
  17. var beforeScript = document.createElement('script');
  18. beforeScript.textContent = `
  19. Window.prototype.addEventListener2 = Window.prototype.addEventListener;
  20. Window.prototype.addEventListener = function(type, listener, useCapture) {
  21. if (type != "beforeunload") {
  22. addEventListener2(type, listener, useCapture);
  23. }
  24. }
  25. `;
  26. (document.head||document.documentElement).insertBefore(beforeScript, (document.head||document.documentElement).firstChild);
  27. beforeScript.onload = function() {
  28. this.parentNode.removeChild(this);
  29. };
  30.  
  31. // After injection
  32. var afterScript = document.createElement('script');
  33. afterScript.textContent = `
  34. function letmeout() {
  35. var all = document.getElementsByTagName("*");
  36. for (var i=0, max=all.length; i < max; i++) {
  37. if(all[i].getAttribute("onbeforeunload")) {
  38. all[i].setAttribute("onbeforeunload", null);
  39. }
  40. }
  41. window.onbeforeunload = null;
  42. }
  43. letmeout();
  44. setInterval(letmeout, 500);
  45. `;
  46. (document.head||document.documentElement).appendChild(afterScript);
  47. afterScript.onload = function() {
  48. this.parentNode.removeChild(this);
  49. };
  50.  
  51. })();