🏠 Home 

[PTP] Catch unsaved comments/posts

Catch unsaved comments/posts


Install this script?
  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. function main() {
  17. var textInput = document.querySelector("#quickpost");
  18. var submitInput = document.querySelector("#quickpostform");
  19. if (textInput !== null && submitInput !== null) {
  20. textInput.addEventListener("input", (event) => {
  21. if (event.target.value !== "") {
  22. addEventListener("beforeunload", beforeUnloadListener, {capture: true});
  23. } else {
  24. removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
  25. }
  26. });
  27. submitInput.addEventListener("submit", (event) => {
  28. removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
  29. });
  30. }
  31. }
  32. window.addEventListener('load', main(), false);