app.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // Sentinel values for specific list navigation.
  2. const TOP = 9999;
  3. const BOTTOM = -9999;
  4. // Simple Polyfill for browsers that don't support Trusted Types
  5. // See https://caniuse.com/?search=trusted%20types
  6. if (!window.trustedTypes || !trustedTypes.createPolicy) {
  7. window.trustedTypes = {
  8. createPolicy: (name, policy) => ({
  9. createScriptURL: src => src,
  10. createHTML: html => html,
  11. })
  12. };
  13. }
  14. /**
  15. * Send a POST request to the specified URL with the given body.
  16. *
  17. * @param {string} url - The URL to send the request to.
  18. * @param {Object} [body] - The body of the request (optional).
  19. * @returns {Promise<Response>} The response from the fetch request.
  20. */
  21. function sendPOSTRequest(url, body = null) {
  22. const options = {
  23. method: "POST",
  24. headers: {
  25. "X-Csrf-Token": document.body.dataset.csrfToken || ""
  26. }
  27. };
  28. if (body !== null) {
  29. options.headers["Content-Type"] = "application/json";
  30. options.body = JSON.stringify(body);
  31. }
  32. return fetch(url, options);
  33. }
  34. /**
  35. * Open a new tab with the given URL.
  36. *
  37. * @param {string} url
  38. */
  39. function openNewTab(url) {
  40. const win = window.open(url, "_blank", "noreferrer");
  41. if (win) win.focus();
  42. }
  43. /**
  44. * Scroll the page to the given element.
  45. *
  46. * @param {Element} element
  47. * @param {boolean} evenIfOnScreen
  48. */
  49. function scrollPageTo(element, evenIfOnScreen) {
  50. const windowScrollPosition = window.scrollY;
  51. const windowHeight = document.documentElement.clientHeight;
  52. const viewportPosition = windowScrollPosition + windowHeight;
  53. const itemBottomPosition = element.offsetTop + element.offsetHeight;
  54. if (evenIfOnScreen || viewportPosition - itemBottomPosition < 0 || viewportPosition - element.offsetTop > windowHeight) {
  55. window.scrollTo(0, element.offsetTop - 10);
  56. }
  57. }
  58. /**
  59. * Attach a click event listener to elements matching the selector.
  60. *
  61. * @param {string} selector
  62. * @param {function} callback
  63. * @param {boolean} noPreventDefault
  64. */
  65. function onClick(selector, callback, noPreventDefault) {
  66. document.querySelectorAll(selector).forEach((element) => {
  67. element.onclick = (event) => {
  68. if (!noPreventDefault) {
  69. event.preventDefault();
  70. }
  71. callback(event);
  72. };
  73. });
  74. }
  75. /**
  76. * Attach an auxiliary click event listener to elements matching the selector.
  77. *
  78. * @param {string} selector
  79. * @param {function} callback
  80. * @param {boolean} noPreventDefault
  81. */
  82. function onAuxClick(selector, callback, noPreventDefault) {
  83. document.querySelectorAll(selector).forEach((element) => {
  84. element.onauxclick = (event) => {
  85. if (!noPreventDefault) {
  86. event.preventDefault();
  87. }
  88. callback(event);
  89. };
  90. });
  91. }
  92. /**
  93. * Filter visible elements based on the selector.
  94. *
  95. * @param {string} selector
  96. * @returns {Array<Element>}
  97. */
  98. function getVisibleElements(selector) {
  99. const elements = document.querySelectorAll(selector);
  100. return [...elements].filter((element) => element.offsetParent !== null);
  101. }
  102. /**
  103. * Get all visible entries on the current page.
  104. *
  105. * @return {Array<Element>}
  106. */
  107. function getVisibleEntries() {
  108. return getVisibleElements(".items .item");
  109. }
  110. /**
  111. * Check if the current view is a list view.
  112. *
  113. * @returns {boolean}
  114. */
  115. function isListView() {
  116. return document.querySelector(".items") !== null;
  117. }
  118. /**
  119. * Check if the current view is an entry view.
  120. *
  121. * @return {boolean}
  122. */
  123. function isEntryView() {
  124. return document.querySelector("section.entry") !== null;
  125. }
  126. /**
  127. * Find the entry element for the given element.
  128. *
  129. * @returns {Element|null}
  130. */
  131. function findEntry(element) {
  132. if (isListView()) {
  133. if (element) {
  134. return element.closest(".item");
  135. }
  136. return document.querySelector(".current-item");
  137. }
  138. return document.querySelector(".entry");
  139. }
  140. /**
  141. * Create an icon label element with the given text.
  142. *
  143. * @param {string} labelText - The text to display in the icon label.
  144. * @returns {Element} The created icon label element.
  145. */
  146. function createIconLabelElement(labelText) {
  147. const labelElement = document.createElement("span");
  148. labelElement.classList.add("icon-label");
  149. labelElement.textContent = labelText;
  150. return labelElement;
  151. }
  152. /**
  153. * Set the icon and label element in the parent element.
  154. *
  155. * @param {Element} parentElement - The parent element to insert the icon and label into.
  156. * @param {string} iconName - The name of the icon to display.
  157. * @param {string} labelText - The text to display in the label.
  158. */
  159. function setIconAndLabelElement(parentElement, iconName, labelText) {
  160. const iconElement = document.querySelector(`template#icon-${iconName}`);
  161. if (iconElement) {
  162. const iconClone = iconElement.content.cloneNode(true);
  163. parentElement.textContent = ""; // Clear existing content
  164. parentElement.appendChild(iconClone);
  165. }
  166. if (labelText) {
  167. const labelElement = createIconLabelElement(labelText);
  168. parentElement.appendChild(labelElement);
  169. }
  170. }
  171. /**
  172. * Set the button to a loading state and return a clone of the original button element.
  173. *
  174. * @param {Element} buttonElement - The button element to set to loading state.
  175. * @return {Element} The original button element cloned before modification.
  176. */
  177. function setButtonToLoadingState(buttonElement) {
  178. const originalButtonElement = buttonElement.cloneNode(true);
  179. buttonElement.textContent = "";
  180. buttonElement.appendChild(createIconLabelElement(buttonElement.dataset.labelLoading));
  181. return originalButtonElement;
  182. }
  183. /**
  184. * Restore the button to its original state.
  185. *
  186. * @param {Element} buttonElement The button element to restore.
  187. * @param {Element} originalButtonElement The original button element to restore from.
  188. * @returns {void}
  189. */
  190. function restoreButtonState(buttonElement, originalButtonElement) {
  191. buttonElement.textContent = "";
  192. buttonElement.appendChild(originalButtonElement);
  193. }
  194. /**
  195. * Set the button to a saved state.
  196. *
  197. * @param {Element} buttonElement The button element to set to saved state.
  198. */
  199. function setButtonToSavedState(buttonElement) {
  200. buttonElement.dataset.completed = "true";
  201. setIconAndLabelElement(buttonElement, "save", buttonElement.dataset.labelDone);
  202. }
  203. /**
  204. * Set the star button state.
  205. *
  206. * @param {Element} buttonElement - The button element to update.
  207. * @param {string} newState - The new state to set ("star" or "unstar").
  208. */
  209. function setStarredButtonState(buttonElement, newState) {
  210. buttonElement.dataset.value = newState;
  211. const iconType = newState === "star" ? "unstar" : "star";
  212. setIconAndLabelElement(buttonElement, iconType, buttonElement.dataset[newState === "star" ? "labelUnstar" : "labelStar"]);
  213. }
  214. /**
  215. * Set the read status button state.
  216. *
  217. * @param {Element} buttonElement - The button element to update.
  218. * @param {string} newState - The new state to set ("read" or "unread").
  219. */
  220. function setReadStatusButtonState(buttonElement, newState) {
  221. buttonElement.dataset.value = newState;
  222. const iconType = newState === "read" ? "unread" : "read";
  223. setIconAndLabelElement(buttonElement, iconType, buttonElement.dataset[newState === "read" ? "labelUnread" : "labelRead"]);
  224. }
  225. /**
  226. * Show a toast notification.
  227. *
  228. * @param {string} iconType - The type of icon to display.
  229. * @param {string} notificationMessage - The message to display in the toast.
  230. * @returns {void}
  231. */
  232. function showToastNotification(iconType, notificationMessage) {
  233. const toastMsgElement = document.createElement("span");
  234. toastMsgElement.id = "toast-msg";
  235. setIconAndLabelElement(toastMsgElement, iconType, notificationMessage);
  236. const toastElementWrapper = document.createElement("div");
  237. toastElementWrapper.id = "toast-wrapper";
  238. toastElementWrapper.setAttribute("role", "alert");
  239. toastElementWrapper.setAttribute("aria-live", "assertive");
  240. toastElementWrapper.setAttribute("aria-atomic", "true");
  241. toastElementWrapper.appendChild(toastMsgElement);
  242. toastElementWrapper.addEventListener("animationend", () => {
  243. toastElementWrapper.remove();
  244. });
  245. document.body.appendChild(toastElementWrapper);
  246. setTimeout(() => toastElementWrapper.classList.add("toast-animate"), 100);
  247. }
  248. /**
  249. * Navigate to a specific page.
  250. *
  251. * @param {string} page - The page to navigate to.
  252. * @param {boolean} reloadOnFail - If true, reload the current page if the target page is not found.
  253. */
  254. function goToPage(page, reloadOnFail = false) {
  255. const element = document.querySelector(`:is(a, button)[data-page=${page}]`);
  256. if (element) {
  257. document.location.href = element.href;
  258. } else if (reloadOnFail) {
  259. window.location.reload();
  260. }
  261. }
  262. /**
  263. * Navigate to the previous page.
  264. *
  265. * If the offset is a KeyboardEvent, it will navigate to the previous item in the list.
  266. * If the offset is a number, it will jump that many items in the list.
  267. * If the offset is TOP, it will jump to the first item in the list.
  268. * If the offset is BOTTOM, it will jump to the last item in the list.
  269. * If the current view is an entry view, it will redirect to the previous page.
  270. *
  271. * @param {number|KeyboardEvent} offset - How many items to jump for focus.
  272. */
  273. function goToPreviousPage(offset) {
  274. if (offset instanceof KeyboardEvent) offset = -1;
  275. if (isListView()) {
  276. goToListItem(offset);
  277. } else {
  278. goToPage("previous");
  279. }
  280. }
  281. /**
  282. * Navigate to the next page.
  283. *
  284. * If the offset is a KeyboardEvent, it will navigate to the next item in the list.
  285. * If the offset is a number, it will jump that many items in the list.
  286. * If the offset is TOP, it will jump to the first item in the list.
  287. * If the offset is BOTTOM, it will jump to the last item in the list.
  288. * If the current view is an entry view, it will redirect to the next page.
  289. *
  290. * @param {number|KeyboardEvent} offset - How many items to jump for focus.
  291. */
  292. function goToNextPage(offset) {
  293. if (offset instanceof KeyboardEvent) offset = 1;
  294. if (isListView()) {
  295. goToListItem(offset);
  296. } else {
  297. goToPage("next");
  298. }
  299. }
  300. /**
  301. * Navigate to the individual feed or feeds page.
  302. *
  303. * If the current view is an entry view, it will redirect to the feed link of the entry.
  304. * If the current view is a list view, it will redirect to the feeds page.
  305. */
  306. function goToFeedOrFeedsPage() {
  307. if (isEntryView()) {
  308. goToFeedPage();
  309. } else {
  310. goToPage("feeds");
  311. }
  312. }
  313. /**
  314. * Navigate to the feed page of the current entry.
  315. *
  316. * If the current view is an entry view, it will redirect to the feed link of the entry.
  317. * If the current view is a list view, it will redirect to the feed link of the currently selected item.
  318. * If no feed link is available, it will do nothing.
  319. */
  320. function goToFeedPage() {
  321. if (isEntryView()) {
  322. const feedAnchor = document.querySelector("span.entry-website a");
  323. if (feedAnchor !== null) {
  324. window.location.href = feedAnchor.href;
  325. }
  326. } else {
  327. const currentItemFeed = document.querySelector(".current-item :is(a, button)[data-feed-link]");
  328. if (currentItemFeed !== null) {
  329. window.location.href = currentItemFeed.getAttribute("href");
  330. }
  331. }
  332. }
  333. /**
  334. * Navigate to the add subscription page.
  335. *
  336. * @returns {void}
  337. */
  338. function goToAddSubscriptionPage() {
  339. window.location.href = document.body.dataset.addSubscriptionUrl;
  340. }
  341. /**
  342. * Navigate to the next or previous item in the list.
  343. *
  344. * If the offset is TOP, it will jump to the first item in the list.
  345. * If the offset is BOTTOM, it will jump to the last item in the list.
  346. * If the offset is a number, it will jump that many items in the list.
  347. * If the current view is an entry view, it will redirect to the next or previous page.
  348. *
  349. * @param {number} offset - How many items to jump for focus.
  350. * @return {void}
  351. */
  352. function goToListItem(offset) {
  353. const items = getVisibleEntries();
  354. if (items.length === 0) {
  355. return;
  356. }
  357. const currentItem = document.querySelector(".current-item");
  358. // If no current item exists, select the first item
  359. if (!currentItem) {
  360. items[0].classList.add("current-item");
  361. items[0].focus();
  362. scrollPageTo(items[0]);
  363. return;
  364. }
  365. // Find the index of the current item
  366. const currentIndex = items.indexOf(currentItem);
  367. if (currentIndex === -1) {
  368. // Current item not found in visible items, select first item
  369. currentItem.classList.remove("current-item");
  370. items[0].classList.add("current-item");
  371. items[0].focus();
  372. scrollPageTo(items[0]);
  373. return;
  374. }
  375. // Calculate the new item index
  376. let newIndex;
  377. if (offset === TOP) {
  378. newIndex = 0;
  379. } else if (offset === BOTTOM) {
  380. newIndex = items.length - 1;
  381. } else {
  382. newIndex = (currentIndex + offset + items.length) % items.length;
  383. }
  384. // Update selection if moving to a different item
  385. if (newIndex !== currentIndex) {
  386. const newItem = items[newIndex];
  387. currentItem.classList.remove("current-item");
  388. newItem.classList.add("current-item");
  389. newItem.focus();
  390. scrollPageTo(newItem);
  391. }
  392. }
  393. /**
  394. * Handle the share action for the entry.
  395. *
  396. * If the share status is "shared", it will trigger the Web Share API.
  397. * If the share status is "share", it will send an Ajax request to fetch the share URL and then trigger the Web Share API.
  398. * If the Web Share API is not supported, it will redirect to the entry URL.
  399. */
  400. async function handleEntryShareAction() {
  401. const link = document.querySelector(':is(a, button)[data-share-status]');
  402. if (link.dataset.shareStatus === "shared") {
  403. const title = document.querySelector(".entry-header > h1 > a");
  404. const url = link.href;
  405. if (!navigator.canShare) {
  406. console.error("Your browser doesn't support the Web Share API.");
  407. window.location = url;
  408. return;
  409. }
  410. try {
  411. await navigator.share({
  412. title: title ? title.textContent : url,
  413. url: url
  414. });
  415. } catch (err) {
  416. console.error(err);
  417. }
  418. }
  419. }
  420. /**
  421. * Toggle the ARIA attributes on the main menu based on the viewport width.
  422. */
  423. function toggleAriaAttributesOnMainMenu() {
  424. const logoElement = document.querySelector(".logo");
  425. const homePageLinkElement = document.querySelector(".logo > a");
  426. if (!logoElement || !homePageLinkElement) return;
  427. const isMobile = document.documentElement.clientWidth < 650;
  428. if (isMobile) {
  429. const navMenuElement = document.getElementById("header-menu");
  430. const isExpanded = navMenuElement?.classList.contains("js-menu-show") ?? false;
  431. const toggleButtonLabel = logoElement.getAttribute("data-toggle-button-label");
  432. // Set mobile menu button attributes
  433. Object.assign(logoElement, {
  434. role: "button",
  435. tabIndex: 0,
  436. ariaLabel: toggleButtonLabel,
  437. ariaExpanded: isExpanded.toString()
  438. });
  439. homePageLinkElement.tabIndex = -1;
  440. } else {
  441. // Remove mobile menu button attributes
  442. ["role", "tabindex", "aria-expanded", "aria-label"].forEach(attr => {
  443. logoElement.removeAttribute(attr);
  444. });
  445. homePageLinkElement.removeAttribute("tabindex");
  446. }
  447. }
  448. /**
  449. * Toggle the main menu dropdown.
  450. *
  451. * @param {Event} event - The event object.
  452. */
  453. function toggleMainMenuDropdown(event) {
  454. // Only handle Enter, Space, or click events
  455. if (event.type === "keydown" && !["Enter", " "].includes(event.key)) {
  456. return;
  457. }
  458. // Prevent default only if element has role attribute (mobile menu button)
  459. if (event.currentTarget.getAttribute("role")) {
  460. event.preventDefault();
  461. }
  462. const navigationMenu = document.querySelector(".header nav ul");
  463. const menuToggleButton = document.querySelector(".logo");
  464. if (!navigationMenu || !menuToggleButton) {
  465. return;
  466. }
  467. const isShowing = navigationMenu.classList.toggle("js-menu-show");
  468. menuToggleButton.setAttribute("aria-expanded", isShowing.toString());
  469. }
  470. /**
  471. * Initialize the main menu handlers.
  472. */
  473. function initializeMainMenuHandlers() {
  474. toggleAriaAttributesOnMainMenu();
  475. window.addEventListener("resize", toggleAriaAttributesOnMainMenu, { passive: true });
  476. const logoElement = document.querySelector(".logo");
  477. if (logoElement) {
  478. logoElement.addEventListener("click", toggleMainMenuDropdown);
  479. logoElement.addEventListener("keydown", toggleMainMenuDropdown);
  480. }
  481. onClick(".header nav li", (event) => {
  482. const linkElement = event.target.closest("a") || event.target.querySelector("a");
  483. if (linkElement && !event.ctrlKey && !event.shiftKey && !event.metaKey) {
  484. event.preventDefault();
  485. window.location.href = linkElement.getAttribute("href");
  486. }
  487. }, true);
  488. }
  489. /**
  490. * This function changes the button label to the loading state and disables the button.
  491. *
  492. * @returns {void}
  493. */
  494. function initializeFormHandlers() {
  495. document.querySelectorAll("form").forEach((element) => {
  496. element.onsubmit = () => {
  497. const buttons = element.querySelectorAll("button[type=submit]");
  498. buttons.forEach((button) => {
  499. if (button.dataset.labelLoading) {
  500. button.textContent = button.dataset.labelLoading;
  501. }
  502. button.disabled = true;
  503. });
  504. };
  505. });
  506. }
  507. /**
  508. * Show the keyboard shortcuts modal.
  509. */
  510. function showKeyboardShortcutsAction() {
  511. document.getElementById("keyboard-shortcuts-modal").showModal();
  512. }
  513. /**
  514. * Mark all visible entries on the current page as read.
  515. */
  516. function markPageAsReadAction() {
  517. const items = getVisibleEntries();
  518. if (items.length === 0) return;
  519. const entryIDs = items.map((element) => parseInt(element.dataset.id, 10));
  520. // Batch DOM writes after all reads
  521. items.forEach((element) => element.classList.add("item-status-read"));
  522. updateEntriesStatus(entryIDs, "read", () => {
  523. const element = document.querySelector(":is(a, button)[data-action=markPageAsRead]");
  524. const showOnlyUnread = element?.dataset.showOnlyUnread || false;
  525. if (showOnlyUnread) {
  526. window.location.reload();
  527. } else {
  528. goToPage("next", true);
  529. }
  530. });
  531. }
  532. /**
  533. * Handle entry status changes from the list view and entry view.
  534. * Focus the next or the previous entry if it exists.
  535. *
  536. * @param {string} navigationDirection Navigation direction: "previous" or "next".
  537. * @param {Element} element Element that triggered the action.
  538. * @param {boolean} setToRead If true, set the entry to read instead of toggling the status.
  539. * @returns {void}
  540. */
  541. function handleEntryStatus(navigationDirection, element, setToRead) {
  542. const currentEntry = findEntry(element);
  543. if (currentEntry) {
  544. if (!setToRead || currentEntry.querySelector(":is(a, button)[data-toggle-status]").dataset.value === "unread") {
  545. toggleEntryStatus(currentEntry, isEntryView());
  546. }
  547. if (isListView() && currentEntry.classList.contains('current-item')) {
  548. switch (navigationDirection) {
  549. case "previous":
  550. goToListItem(-1);
  551. break;
  552. case "next":
  553. goToListItem(1);
  554. break;
  555. }
  556. }
  557. }
  558. }
  559. /**
  560. * Toggle the entry status between "read" and "unread".
  561. *
  562. * @param {Element} element The entry element to toggle the status for.
  563. * @param {boolean} toasting If true, show a toast notification after toggling the status.
  564. */
  565. function toggleEntryStatus(element, toasting) {
  566. const entryID = parseInt(element.dataset.id, 10);
  567. const buttonElement = element.querySelector(":is(a, button)[data-toggle-status]");
  568. if (!buttonElement) return;
  569. const currentStatus = buttonElement.dataset.value;
  570. const newStatus = currentStatus === "read" ? "unread" : "read";
  571. setButtonToLoadingState(buttonElement);
  572. updateEntriesStatus([entryID], newStatus, () => {
  573. setReadStatusButtonState(buttonElement, newStatus);
  574. if (toasting) {
  575. showToastNotification(newStatus, currentStatus === "read" ? buttonElement.dataset.toastUnread : buttonElement.dataset.toastRead);
  576. }
  577. element.classList.replace(`item-status-${currentStatus}`, `item-status-${newStatus}`);
  578. if (isListView() && getVisibleEntries().length === 0) {
  579. window.location.reload();
  580. }
  581. });
  582. }
  583. /**
  584. * Handle the refresh of all feeds.
  585. *
  586. * This function POSTs to the URL specified in the data-refresh-all-feeds-url attribute of the body element.
  587. */
  588. function handleRefreshAllFeedsAction() {
  589. const refreshAllFeedsUrl = document.body.dataset.refreshAllFeedsUrl;
  590. if (refreshAllFeedsUrl) {
  591. sendPOSTRequest(refreshAllFeedsUrl).then((response) => {
  592. if (response?.redirected && response.url) {
  593. window.location.href = response.url;
  594. } else {
  595. window.location.reload();
  596. }
  597. });
  598. }
  599. }
  600. /**
  601. * Update the status of multiple entries.
  602. *
  603. * @param {Array<number>} entryIDs - The IDs of the entries to update.
  604. * @param {string} status - The new status to set for the entries (e.g., "read", "unread").
  605. */
  606. function updateEntriesStatus(entryIDs, status, callback) {
  607. const url = document.body.dataset.entriesStatusUrl;
  608. sendPOSTRequest(url, { entry_ids: entryIDs, status: status }).then((resp) => {
  609. resp.json().then(count => {
  610. if (callback) {
  611. callback(resp);
  612. }
  613. updateUnreadCounterValue(status === "read" ? -count : count);
  614. });
  615. });
  616. }
  617. /**
  618. * Handle save entry from list view and entry view.
  619. *
  620. * @param {Element|null} element - The element that triggered the save action (optional).
  621. */
  622. function handleSaveEntryAction(element = null) {
  623. const currentEntry = findEntry(element);
  624. if (!currentEntry) return;
  625. const buttonElement = currentEntry.querySelector(":is(a, button)[data-save-entry]");
  626. if (!buttonElement || buttonElement.dataset.completed) return;
  627. setButtonToLoadingState(buttonElement);
  628. sendPOSTRequest(buttonElement.dataset.saveUrl).then(() => {
  629. setButtonToSavedState(buttonElement);
  630. if (isEntryView()) {
  631. showToastNotification("save", buttonElement.dataset.toastDone);
  632. }
  633. });
  634. }
  635. /**
  636. * Handle starring an entry.
  637. *
  638. * @param {Element} element - The element that triggered the star action.
  639. */
  640. function handleStarAction(element) {
  641. const currentEntry = findEntry(element);
  642. if (!currentEntry) return;
  643. const buttonElement = currentEntry.querySelector(":is(a, button)[data-toggle-starred]");
  644. if (!buttonElement) return;
  645. setButtonToLoadingState(buttonElement);
  646. sendPOSTRequest(buttonElement.dataset.starUrl).then(() => {
  647. const currentState = buttonElement.dataset.value;
  648. const isStarred = currentState === "star";
  649. const newStarStatus = isStarred ? "unstar" : "star";
  650. setStarredButtonState(buttonElement, newStarStatus);
  651. if (isEntryView()) {
  652. showToastNotification(currentState, buttonElement.dataset[isStarred ? "toastUnstar" : "toastStar"]);
  653. }
  654. });
  655. }
  656. /**
  657. * Handle fetching the original content of an entry.
  658. *
  659. * @returns {void}
  660. */
  661. function handleFetchOriginalContentAction() {
  662. if (isListView()) return;
  663. const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]");
  664. if (!buttonElement) return;
  665. const originalButtonElement = setButtonToLoadingState(buttonElement);
  666. sendPOSTRequest(buttonElement.dataset.fetchContentUrl).then((response) => {
  667. restoreButtonState(buttonElement, originalButtonElement);
  668. response.json().then((data) => {
  669. if (data.content && data.reading_time) {
  670. const ttpolicy = trustedTypes.createPolicy('html', {createHTML: html => html});
  671. document.querySelector(".entry-content").innerHTML = ttpolicy.createHTML(data.content);
  672. const entryReadingtimeElement = document.querySelector(".entry-reading-time");
  673. if (entryReadingtimeElement) {
  674. entryReadingtimeElement.textContent = data.reading_time;
  675. }
  676. }
  677. });
  678. });
  679. }
  680. /**
  681. * Open the original link of an entry.
  682. *
  683. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  684. * @returns {void}
  685. */
  686. function openOriginalLinkAction(openLinkInCurrentTab) {
  687. if (isEntryView()) {
  688. openOriginalLinkFromEntryView(openLinkInCurrentTab);
  689. } else if (isListView()) {
  690. openOriginalLinkFromListView();
  691. }
  692. }
  693. /**
  694. * Open the original link from entry view.
  695. *
  696. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  697. * @returns {void}
  698. */
  699. function openOriginalLinkFromEntryView(openLinkInCurrentTab) {
  700. const entryLink = document.querySelector(".entry h1 a");
  701. if (!entryLink) return;
  702. const url = entryLink.getAttribute("href");
  703. if (openLinkInCurrentTab) {
  704. window.location.href = url;
  705. } else {
  706. openNewTab(url);
  707. }
  708. }
  709. /**
  710. * Open the original link from list view.
  711. *
  712. * @returns {void}
  713. */
  714. function openOriginalLinkFromListView() {
  715. const currentItem = document.querySelector(".current-item");
  716. const originalLink = currentItem?.querySelector(":is(a, button)[data-original-link]");
  717. if (!currentItem || !originalLink) return;
  718. // Open the link
  719. openNewTab(originalLink.getAttribute("href"));
  720. // Don't navigate or mark as read on starred page
  721. const isStarredPage = document.location.href === document.querySelector(':is(a, button)[data-page=starred]').href;
  722. if (isStarredPage) return;
  723. // Navigate to next item
  724. goToListItem(1);
  725. // Mark as read if currently unread
  726. if (currentItem.classList.replace("item-status-unread", "item-status-read")) {
  727. const entryID = parseInt(currentItem.dataset.id, 10);
  728. updateEntriesStatus([entryID], "read");
  729. }
  730. }
  731. /**
  732. * Open the comments link of an entry.
  733. *
  734. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  735. * @returns {void}
  736. */
  737. function openCommentLinkAction(openLinkInCurrentTab) {
  738. const entryLink = document.querySelector(isListView() ? ".current-item :is(a, button)[data-comments-link]" : ":is(a, button)[data-comments-link]");
  739. if (entryLink) {
  740. if (openLinkInCurrentTab) {
  741. window.location.href = entryLink.getAttribute("href");
  742. } else {
  743. openNewTab(entryLink.getAttribute("href"));
  744. }
  745. }
  746. }
  747. /**
  748. * Open the selected item in the current view.
  749. *
  750. * If the current view is a list view, it will navigate to the link of the currently selected item.
  751. * If the current view is an entry view, it will navigate to the link of the entry.
  752. */
  753. function openSelectedItemAction() {
  754. const currentItemLink = document.querySelector(".current-item .item-title a");
  755. if (currentItemLink) {
  756. window.location.href = currentItemLink.getAttribute("href");
  757. }
  758. }
  759. /**
  760. * Unsubscribe from the feed of the currently selected item.
  761. */
  762. function handleRemoveFeedAction() {
  763. const unsubscribeLink = document.querySelector("[data-action=remove-feed]");
  764. if (unsubscribeLink) {
  765. sendPOSTRequest(unsubscribeLink.dataset.url).then(() => {
  766. window.location.href = unsubscribeLink.dataset.redirectUrl || window.location.href;
  767. });
  768. }
  769. }
  770. /**
  771. * Scroll the page to the currently selected item.
  772. */
  773. function scrollToCurrentItemAction() {
  774. const currentItem = document.querySelector(".current-item");
  775. if (currentItem) {
  776. scrollPageTo(currentItem, true);
  777. }
  778. }
  779. /**
  780. * Update the unread counter value.
  781. *
  782. * @param {number} delta - The amount to change the counter by.
  783. */
  784. function updateUnreadCounterValue(delta) {
  785. document.querySelectorAll("span.unread-counter").forEach((element) => {
  786. const oldValue = parseInt(element.textContent, 10);
  787. element.textContent = oldValue + delta;
  788. });
  789. if (window.location.href.endsWith('/unread')) {
  790. const oldValue = parseInt(document.title.split('(')[1], 10);
  791. const newValue = oldValue + delta;
  792. document.title = document.title.replace(/\(\d+\)/, `(${newValue})`);
  793. }
  794. }
  795. /**
  796. * Handle confirmation messages for actions that require user confirmation.
  797. *
  798. * This function modifies the link element to show a confirmation question with "Yes" and "No" buttons.
  799. * If the user clicks "Yes", it calls the provided callback with the URL and redirect URL.
  800. * If the user clicks "No", it either redirects to a no-action URL or restores the link element.
  801. *
  802. * @param {Element} linkElement - The link or button element that triggered the confirmation.
  803. * @param {function} callback - The callback function to execute if the user confirms the action.
  804. * @returns {void}
  805. */
  806. function handleConfirmationMessage(linkElement, callback) {
  807. if (linkElement.tagName !== 'A' && linkElement.tagName !== "BUTTON") {
  808. linkElement = linkElement.parentNode;
  809. }
  810. linkElement.style.display = "none";
  811. const containerElement = linkElement.parentNode;
  812. const questionElement = document.createElement("span");
  813. function createLoadingElement() {
  814. const loadingElement = document.createElement("span");
  815. loadingElement.className = "loading";
  816. loadingElement.appendChild(document.createTextNode(linkElement.dataset.labelLoading));
  817. questionElement.remove();
  818. containerElement.appendChild(loadingElement);
  819. }
  820. const yesElement = document.createElement("button");
  821. yesElement.appendChild(document.createTextNode(linkElement.dataset.labelYes));
  822. yesElement.onclick = (event) => {
  823. event.preventDefault();
  824. createLoadingElement();
  825. callback(linkElement.dataset.url, linkElement.dataset.redirectUrl);
  826. };
  827. const noElement = document.createElement("button");
  828. noElement.appendChild(document.createTextNode(linkElement.dataset.labelNo));
  829. noElement.onclick = (event) => {
  830. event.preventDefault();
  831. const noActionUrl = linkElement.dataset.noActionUrl;
  832. if (noActionUrl) {
  833. createLoadingElement();
  834. callback(noActionUrl, linkElement.dataset.redirectUrl);
  835. } else {
  836. linkElement.style.display = "inline";
  837. questionElement.remove();
  838. }
  839. };
  840. questionElement.className = "confirm";
  841. questionElement.appendChild(document.createTextNode(`${linkElement.dataset.labelQuestion} `));
  842. questionElement.appendChild(yesElement);
  843. questionElement.appendChild(document.createTextNode(", "));
  844. questionElement.appendChild(noElement);
  845. containerElement.appendChild(questionElement);
  846. }
  847. /**
  848. * Check if the player is actually playing a media
  849. *
  850. * @param mediaElement the player element itself
  851. * @returns {boolean}
  852. */
  853. function isPlayerPlaying(mediaElement) {
  854. return mediaElement &&
  855. mediaElement.currentTime > 0 &&
  856. !mediaElement.paused &&
  857. !mediaElement.ended &&
  858. mediaElement.readyState > 2; // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState
  859. }
  860. /**
  861. * Handle player progression save and mark as read on completion.
  862. *
  863. * This function is triggered on the `timeupdate` event of the media player.
  864. * It saves the current playback position and marks the entry as read if the completion percentage is reached.
  865. *
  866. * @param {Element} playerElement The media player element (audio or video).
  867. */
  868. function handlePlayerProgressionSaveAndMarkAsReadOnCompletion(playerElement) {
  869. if (!isPlayerPlaying(playerElement)) {
  870. return;
  871. }
  872. const currentPositionInSeconds = Math.floor(playerElement.currentTime);
  873. const lastKnownPositionInSeconds = parseInt(playerElement.dataset.lastPosition, 10);
  874. const markAsReadOnCompletion = parseFloat(playerElement.dataset.markReadOnCompletion);
  875. const recordInterval = 10;
  876. // We limit the number of update to only one by interval. Otherwise, we would have multiple update per seconds
  877. if (currentPositionInSeconds >= (lastKnownPositionInSeconds + recordInterval) ||
  878. currentPositionInSeconds <= (lastKnownPositionInSeconds - recordInterval)
  879. ) {
  880. playerElement.dataset.lastPosition = currentPositionInSeconds.toString();
  881. sendPOSTRequest(playerElement.dataset.saveUrl, { progression: currentPositionInSeconds });
  882. // Handle the mark as read on completion
  883. if (markAsReadOnCompletion >= 0 && playerElement.duration > 0) {
  884. const completion = currentPositionInSeconds / playerElement.duration;
  885. if (completion >= markAsReadOnCompletion) {
  886. handleEntryStatus("none", document.querySelector(":is(a, button)[data-toggle-status]"), true);
  887. }
  888. }
  889. }
  890. }
  891. /**
  892. * Handle media control actions like seeking and changing playback speed.
  893. *
  894. * This function is triggered by clicking on media control buttons.
  895. * It adjusts the playback position or speed of media elements with the same enclosure ID.
  896. *
  897. * @param {Element} mediaPlayerButtonElement
  898. */
  899. function handleMediaControlButtonClick(mediaPlayerButtonElement) {
  900. const actionType = mediaPlayerButtonElement.dataset.enclosureAction;
  901. const actionValue = parseFloat(mediaPlayerButtonElement.dataset.actionValue);
  902. const enclosureID = mediaPlayerButtonElement.dataset.enclosureId;
  903. const mediaElements = document.querySelectorAll(`audio[data-enclosure-id="${enclosureID}"],video[data-enclosure-id="${enclosureID}"]`);
  904. const speedIndicatorElements = document.querySelectorAll(`span.speed-indicator[data-enclosure-id="${enclosureID}"]`);
  905. mediaElements.forEach((mediaElement) => {
  906. switch (actionType) {
  907. case "seek":
  908. mediaElement.currentTime = Math.max(mediaElement.currentTime + actionValue, 0);
  909. break;
  910. case "speed":
  911. // 0.25 was chosen because it will allow to get back to 1x in two "faster" clicks.
  912. // A lower value would result in a playback rate of 0, effectively pausing playback.
  913. mediaElement.playbackRate = Math.max(0.25, mediaElement.playbackRate + actionValue);
  914. speedIndicatorElements.forEach((speedIndicatorElement) => {
  915. speedIndicatorElement.innerText = `${mediaElement.playbackRate.toFixed(2)}x`;
  916. });
  917. break;
  918. case "speed-reset":
  919. mediaElement.playbackRate = actionValue ;
  920. speedIndicatorElements.forEach((speedIndicatorElement) => {
  921. // Two digit precision to ensure we always have the same number of characters (4) to avoid controls moving when clicking buttons because of more or less characters.
  922. // The trick only works on rates less than 10, but it feels an acceptable trade-off considering the feature
  923. speedIndicatorElement.innerText = `${mediaElement.playbackRate.toFixed(2)}x`;
  924. });
  925. break;
  926. }
  927. });
  928. }
  929. /**
  930. * Initialize media player event handlers.
  931. */
  932. function initializeMediaPlayerHandlers() {
  933. document.querySelectorAll("button[data-enclosure-action]").forEach((element) => {
  934. element.addEventListener("click", () => handleMediaControlButtonClick(element));
  935. });
  936. // Set playback from the last position if available
  937. document.querySelectorAll("audio[data-last-position],video[data-last-position]").forEach((element) => {
  938. if (element.dataset.lastPosition) {
  939. element.currentTime = element.dataset.lastPosition;
  940. }
  941. element.ontimeupdate = () => handlePlayerProgressionSaveAndMarkAsReadOnCompletion(element);
  942. });
  943. // Set playback speed from the data attribute if available
  944. document.querySelectorAll("audio[data-playback-rate],video[data-playback-rate]").forEach((element) => {
  945. if (element.dataset.playbackRate) {
  946. element.playbackRate = element.dataset.playbackRate;
  947. if (element.dataset.enclosureId) {
  948. document.querySelectorAll(`span.speed-indicator[data-enclosure-id="${element.dataset.enclosureId}"]`).forEach((speedIndicatorElement) => {
  949. speedIndicatorElement.innerText = `${parseFloat(element.dataset.playbackRate).toFixed(2)}x`;
  950. });
  951. }
  952. }
  953. });
  954. }
  955. /**
  956. * Initialize the service worker and PWA installation prompt.
  957. */
  958. function initializeServiceWorker() {
  959. // Register service worker if supported
  960. if ("serviceWorker" in navigator) {
  961. const serviceWorkerURL = document.body.dataset.serviceWorkerUrl;
  962. if (serviceWorkerURL) {
  963. const ttpolicy = trustedTypes.createPolicy('url', {createScriptURL: src => src});
  964. navigator.serviceWorker.register(ttpolicy.createScriptURL(serviceWorkerURL), {
  965. type: "module"
  966. }).catch((error) => {
  967. console.error("Service Worker registration failed:", error);
  968. });
  969. }
  970. }
  971. // PWA installation prompt handling
  972. window.addEventListener("beforeinstallprompt", (event) => {
  973. let deferredPrompt = event;
  974. const promptHomeScreen = document.getElementById("prompt-home-screen");
  975. const btnAddToHomeScreen = document.getElementById("btn-add-to-home-screen");
  976. if (!promptHomeScreen || !btnAddToHomeScreen) return;
  977. promptHomeScreen.style.display = "block";
  978. btnAddToHomeScreen.addEventListener("click", (event) => {
  979. event.preventDefault();
  980. deferredPrompt.prompt();
  981. deferredPrompt.userChoice.then(() => {
  982. deferredPrompt = null;
  983. promptHomeScreen.style.display = "none";
  984. });
  985. });
  986. });
  987. }
  988. /**
  989. * Initialize WebAuthn handlers if supported.
  990. */
  991. function initializeWebAuthn() {
  992. if (typeof WebAuthnHandler !== 'function') return;
  993. if (!WebAuthnHandler.isWebAuthnSupported()) return;
  994. const webauthnHandler = new WebAuthnHandler();
  995. // Setup delete credentials handler
  996. onClick("#webauthn-delete", () => { webauthnHandler.removeAllCredentials(); });
  997. // Setup registration
  998. const registerButton = document.getElementById("webauthn-register");
  999. if (registerButton) {
  1000. registerButton.disabled = false;
  1001. onClick("#webauthn-register", () => {
  1002. webauthnHandler.register().catch((err) => WebAuthnHandler.showErrorMessage(err));
  1003. });
  1004. }
  1005. // Setup login
  1006. const loginButton = document.getElementById("webauthn-login");
  1007. const usernameField = document.getElementById("form-username");
  1008. if (loginButton && usernameField) {
  1009. const abortController = new AbortController();
  1010. loginButton.disabled = false;
  1011. onClick("#webauthn-login", () => {
  1012. abortController.abort();
  1013. webauthnHandler.login().catch(err => WebAuthnHandler.showErrorMessage(err));
  1014. });
  1015. webauthnHandler.conditionalLogin(abortController).catch(err => WebAuthnHandler.showErrorMessage(err));
  1016. }
  1017. }
  1018. /**
  1019. * Initialize keyboard shortcuts for navigation and actions.
  1020. */
  1021. function initializeKeyboardShortcuts() {
  1022. if (document.querySelector("body[data-disable-keyboard-shortcuts=true]")) return;
  1023. const keyboardHandler = new KeyboardHandler();
  1024. // Navigation shortcuts
  1025. keyboardHandler.on("g u", () => goToPage("unread"));
  1026. keyboardHandler.on("g b", () => goToPage("starred"));
  1027. keyboardHandler.on("g h", () => goToPage("history"));
  1028. keyboardHandler.on("g f", goToFeedOrFeedsPage);
  1029. keyboardHandler.on("g c", () => goToPage("categories"));
  1030. keyboardHandler.on("g s", () => goToPage("settings"));
  1031. keyboardHandler.on("g g", () => goToPreviousPage(TOP));
  1032. keyboardHandler.on("G", () => goToNextPage(BOTTOM));
  1033. keyboardHandler.on("/", () => goToPage("search"));
  1034. // Item navigation
  1035. keyboardHandler.on("ArrowLeft", goToPreviousPage);
  1036. keyboardHandler.on("ArrowRight", goToNextPage);
  1037. keyboardHandler.on("k", goToPreviousPage);
  1038. keyboardHandler.on("p", goToPreviousPage);
  1039. keyboardHandler.on("j", goToNextPage);
  1040. keyboardHandler.on("n", goToNextPage);
  1041. keyboardHandler.on("h", () => goToPage("previous"));
  1042. keyboardHandler.on("l", () => goToPage("next"));
  1043. keyboardHandler.on("z t", scrollToCurrentItemAction);
  1044. // Item actions
  1045. keyboardHandler.on("o", openSelectedItemAction);
  1046. keyboardHandler.on("Enter", () => openSelectedItemAction());
  1047. keyboardHandler.on("v", () => openOriginalLinkAction(false));
  1048. keyboardHandler.on("V", () => openOriginalLinkAction(true));
  1049. keyboardHandler.on("c", () => openCommentLinkAction(false));
  1050. keyboardHandler.on("C", () => openCommentLinkAction(true));
  1051. // Entry management
  1052. keyboardHandler.on("m", () => handleEntryStatus("next"));
  1053. keyboardHandler.on("M", () => handleEntryStatus("previous"));
  1054. keyboardHandler.on("A", markPageAsReadAction);
  1055. keyboardHandler.on("s", () => handleSaveEntryAction());
  1056. keyboardHandler.on("d", handleFetchOriginalContentAction);
  1057. keyboardHandler.on("f", () => handleStarAction());
  1058. // Feed actions
  1059. keyboardHandler.on("F", goToFeedPage);
  1060. keyboardHandler.on("R", handleRefreshAllFeedsAction);
  1061. keyboardHandler.on("+", goToAddSubscriptionPage);
  1062. keyboardHandler.on("#", handleRemoveFeedAction);
  1063. // UI actions
  1064. keyboardHandler.on("?", showKeyboardShortcutsAction);
  1065. keyboardHandler.on("a", () => {
  1066. const enclosureElement = document.querySelector('.entry-enclosures');
  1067. if (enclosureElement) {
  1068. enclosureElement.toggleAttribute('open');
  1069. }
  1070. });
  1071. keyboardHandler.listen();
  1072. }
  1073. /**
  1074. * Initialize touch handler for mobile devices.
  1075. */
  1076. function initializeTouchHandler() {
  1077. if ( "ontouchstart" in window || navigator.maxTouchPoints > 0) {
  1078. const touchHandler = new TouchHandler();
  1079. touchHandler.listen();
  1080. }
  1081. }
  1082. /**
  1083. * Initialize click handlers for various UI elements.
  1084. */
  1085. function initializeClickHandlers() {
  1086. // Entry actions
  1087. onClick(":is(a, button)[data-save-entry]", (event) => handleSaveEntryAction(event.target));
  1088. onClick(":is(a, button)[data-toggle-starred]", (event) => handleStarAction(event.target));
  1089. onClick(":is(a, button)[data-toggle-status]", (event) => handleEntryStatus("next", event.target));
  1090. onClick(":is(a, button)[data-fetch-content-entry]", handleFetchOriginalContentAction);
  1091. onClick(":is(a, button)[data-share-status]", handleEntryShareAction);
  1092. // Page actions with confirmation
  1093. onClick(":is(a, button)[data-action=markPageAsRead]", (event) => handleConfirmationMessage(event.target, markPageAsReadAction));
  1094. // Generic confirmation handler
  1095. onClick(":is(a, button)[data-confirm]", (event) => {
  1096. handleConfirmationMessage(event.target, (url, redirectURL) => {
  1097. sendPOSTRequest(url).then((response) => {
  1098. if (redirectURL) {
  1099. window.location.href = redirectURL;
  1100. } else if (response?.redirected && response.url) {
  1101. window.location.href = response.url;
  1102. } else {
  1103. window.location.reload();
  1104. }
  1105. });
  1106. });
  1107. });
  1108. // Original link handlers (both click and middle-click)
  1109. const handleOriginalLink = (event) => handleEntryStatus("next", event.target, true);
  1110. onClick("a[data-original-link='true']", handleOriginalLink, true);
  1111. onAuxClick("a[data-original-link='true']", (event) => {
  1112. if (event.button === 1) {
  1113. handleOriginalLink(event);
  1114. }
  1115. }, true);
  1116. }
  1117. // Initialize application handlers
  1118. initializeMainMenuHandlers();
  1119. initializeFormHandlers();
  1120. initializeMediaPlayerHandlers();
  1121. initializeWebAuthn();
  1122. initializeKeyboardShortcuts();
  1123. initializeTouchHandler();
  1124. initializeClickHandlers();
  1125. initializeServiceWorker();
  1126. // Reload the page if it was restored from the back-forward cache and mark entries as read is enabled.
  1127. window.addEventListener("pageshow", (event) => {
  1128. if (event.persisted && document.body.dataset.markAsReadOnView === "true") {
  1129. location.reload();
  1130. }
  1131. });