You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.1 KiB

4 years ago
  1. /*!
  2. * Copyright (C) 2019 Josh Habdas <jhabdas@protonmail.com>
  3. *
  4. * This file is part of After Dark.
  5. *
  6. * After Dark is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * After Dark is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. (function (window, document, undefined) {
  20. 'use strict';
  21. const form = document.forms.generator;
  22. form.reset();
  23. const isOnlineHelp = document.URL.includes('localhost:1414');
  24. if (!isOnlineHelp) return;
  25. form.querySelector('.js-useonlinehelp').style.display = 'none';
  26. if (window.navigator.onLine) {
  27. form.querySelector('.js-disconnect').style.display = 'block';
  28. }
  29. const activate = () => {
  30. form.querySelector('.js-disconnect').style.display = 'block';
  31. form.generate.disabled = true;
  32. form.address.disabled = true;
  33. form.address.value = '';
  34. form.generate.classList.add('muted');
  35. };
  36. const deactivate = () => {
  37. form.querySelector('.js-disconnect').style.display = 'none';
  38. form.generate.disabled = false;
  39. form.address.disabled = false;
  40. form.generate.classList.remove('muted');
  41. };
  42. window.addEventListener('online', activate);
  43. window.addEventListener('offline', deactivate);
  44. if (!window.navigator.onLine) deactivate();
  45. form.addEventListener('submit', evt => {
  46. evt.preventDefault();
  47. const isLocal = document.location.host.includes('localhost');
  48. const hasSameOrigin = form.action.includes(document.location.origin);
  49. if (isLocal && hasSameOrigin) {
  50. evt.target.submit();
  51. } else {
  52. form.querySelector('.js-usesameorigin').style.display = 'block';
  53. deactivate();
  54. }
  55. })
  56. })(window, document);