[PTP] Catch unsaved comments/posts

Catch unsaved comments/posts

  1. // ==UserScript==
  2. // @name [PTP] Catch unsaved comments/posts
  3. // @namespace kannibalox
  4. // @match https://passthepopcorn.me/*
  5. // @grant none
  6. // @version 1.0
  7. // @author kannibalox
  8. // @license GNU GPLv3
  9. // @description Catch unsaved comments/posts
  10. // ==/UserScript==
  11. "use strict";
  12. const beforeUnloadListener = (e) => {
  13. e.preventDefault();
  14. return e.returnValue = '';
  15. };
  16.  
  17.  
  18. function main() {
  19. var textInput = document.querySelector("#quickpost");
  20. var submitInput = document.querySelector("#quickpostform");
  21. if (textInput !== null && submitInput !== null) {
  22. textInput.addEventListener("input", (event) => {
  23. if (event.target.value !== "") {
  24. addEventListener("beforeunload", beforeUnloadListener, {capture: true});
  25. } else {
  26. removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
  27. }
  28. });
  29. submitInput.addEventListener("submit", (event) => {
  30. removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
  31. });
  32. }
  33. }
  34.  
  35. window.addEventListener('load', main(), false);