app.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Hello, dear curious visitor. I am not a web-guy, so please don't judge my horrible JS code.
  3. * In fact, please do tell me about all the things I did wrong and that I could improve. I've been trying
  4. * to read up on modern JS, but it's just a little much.
  5. *
  6. * Feel free to open tickets at https://github.com/binwiederhier/ntfy/issues. Thank you!
  7. */
  8. /* All the things */
  9. let currentUrl = window.location.hostname;
  10. if (window.location.port) {
  11. currentUrl += ':' + window.location.port
  12. }
  13. /* Screenshots */
  14. const lightbox = document.getElementById("lightbox");
  15. const showScreenshotOverlay = (e, el, index) => {
  16. lightbox.classList.add('show');
  17. document.addEventListener('keydown', nextScreenshotKeyboardListener);
  18. return showScreenshot(e, index);
  19. };
  20. const showScreenshot = (e, index) => {
  21. const actualIndex = resolveScreenshotIndex(index);
  22. lightbox.innerHTML = '<div class="close-lightbox"></div>' + screenshots[actualIndex].innerHTML;
  23. lightbox.querySelector('img').onclick = (e) => { return showScreenshot(e,actualIndex+1); };
  24. currentScreenshotIndex = actualIndex;
  25. e.stopPropagation();
  26. return false;
  27. };
  28. const nextScreenshot = (e) => {
  29. return showScreenshot(e, currentScreenshotIndex+1);
  30. };
  31. const previousScreenshot = (e) => {
  32. return showScreenshot(e, currentScreenshotIndex-1);
  33. };
  34. const resolveScreenshotIndex = (index) => {
  35. if (index < 0) {
  36. return screenshots.length - 1;
  37. } else if (index > screenshots.length - 1) {
  38. return 0;
  39. }
  40. return index;
  41. };
  42. const hideScreenshotOverlay = (e) => {
  43. lightbox.classList.remove('show');
  44. document.removeEventListener('keydown', nextScreenshotKeyboardListener);
  45. };
  46. const nextScreenshotKeyboardListener = (e) => {
  47. switch (e.keyCode) {
  48. case 37:
  49. previousScreenshot(e);
  50. break;
  51. case 39:
  52. nextScreenshot(e);
  53. break;
  54. }
  55. };
  56. let currentScreenshotIndex = 0;
  57. const screenshots = [...document.querySelectorAll("#screenshots a")];
  58. screenshots.forEach((el, index) => {
  59. el.onclick = (e) => { return showScreenshotOverlay(e, el, index); };
  60. });
  61. lightbox.onclick = hideScreenshotOverlay;
  62. // Add anchor links
  63. document.querySelectorAll('.anchor').forEach((el) => {
  64. if (el.hasAttribute('id')) {
  65. const id = el.getAttribute('id');
  66. const anchor = document.createElement('a');
  67. anchor.innerHTML = `<a href="#${id}" class="anchorLink">#</a>`;
  68. el.appendChild(anchor);
  69. }
  70. });
  71. // Change ntfy.sh url and protocol to match self-hosted one
  72. document.querySelectorAll('.ntfyUrl').forEach((el) => {
  73. el.innerHTML = currentUrl;
  74. });
  75. document.querySelectorAll('.ntfyProtocol').forEach((el) => {
  76. el.innerHTML = window.location.protocol + "//";
  77. });