app.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 submits a real form POST to the URL specified in the data-refresh-all-feeds-url
  587. * attribute of the body element, so the browser follows the redirect once and renders the
  588. * server-side flash message, matching the behavior of the menu button.
  589. */
  590. function handleRefreshAllFeedsAction() {
  591. const refreshAllFeedsUrl = document.body.dataset.refreshAllFeedsUrl;
  592. if (!refreshAllFeedsUrl) {
  593. return;
  594. }
  595. const form = document.createElement("form");
  596. form.method = "post";
  597. form.action = refreshAllFeedsUrl;
  598. const csrfField = document.createElement("input");
  599. csrfField.type = "hidden";
  600. csrfField.name = "csrf";
  601. csrfField.value = document.body.dataset.csrfToken || "";
  602. form.appendChild(csrfField);
  603. document.body.appendChild(form);
  604. form.submit();
  605. }
  606. /**
  607. * Update the status of multiple entries.
  608. *
  609. * @param {Array<number>} entryIDs - The IDs of the entries to update.
  610. * @param {string} status - The new status to set for the entries (e.g., "read", "unread").
  611. */
  612. function updateEntriesStatus(entryIDs, status, callback) {
  613. const url = document.body.dataset.entriesStatusUrl;
  614. sendPOSTRequest(url, { entry_ids: entryIDs, status: status }).then((resp) => {
  615. resp.json().then(count => {
  616. if (callback) {
  617. callback(resp);
  618. }
  619. updateUnreadCounterValue(status === "read" ? -count : count);
  620. });
  621. });
  622. }
  623. /**
  624. * Handle save entry from list view and entry view.
  625. *
  626. * @param {Element|null} element - The element that triggered the save action (optional).
  627. */
  628. function handleSaveEntryAction(element = null) {
  629. const currentEntry = findEntry(element);
  630. if (!currentEntry) return;
  631. const buttonElement = currentEntry.querySelector(":is(a, button)[data-save-entry]");
  632. if (!buttonElement || buttonElement.dataset.completed) return;
  633. setButtonToLoadingState(buttonElement);
  634. sendPOSTRequest(buttonElement.dataset.saveUrl).then(() => {
  635. setButtonToSavedState(buttonElement);
  636. if (isEntryView()) {
  637. showToastNotification("save", buttonElement.dataset.toastDone);
  638. }
  639. });
  640. }
  641. /**
  642. * Handle starring an entry.
  643. *
  644. * @param {Element} element - The element that triggered the star action.
  645. */
  646. function handleStarAction(element) {
  647. const currentEntry = findEntry(element);
  648. if (!currentEntry) return;
  649. const buttonElement = currentEntry.querySelector(":is(a, button)[data-toggle-starred]");
  650. if (!buttonElement) return;
  651. setButtonToLoadingState(buttonElement);
  652. sendPOSTRequest(buttonElement.dataset.starUrl).then(() => {
  653. const currentState = buttonElement.dataset.value;
  654. const isStarred = currentState === "star";
  655. const newStarStatus = isStarred ? "unstar" : "star";
  656. setStarredButtonState(buttonElement, newStarStatus);
  657. if (isEntryView()) {
  658. showToastNotification(currentState, buttonElement.dataset[isStarred ? "toastUnstar" : "toastStar"]);
  659. }
  660. });
  661. }
  662. /**
  663. * Handle fetching the original content of an entry.
  664. *
  665. * @returns {void}
  666. */
  667. function handleFetchOriginalContentAction() {
  668. if (isListView()) return;
  669. const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]");
  670. if (!buttonElement) return;
  671. const originalButtonElement = setButtonToLoadingState(buttonElement);
  672. sendPOSTRequest(buttonElement.dataset.fetchContentUrl).then((response) => {
  673. restoreButtonState(buttonElement, originalButtonElement);
  674. response.json().then((data) => {
  675. if (data.content && data.reading_time) {
  676. const ttpolicy = trustedTypes.createPolicy('html', {createHTML: html => html});
  677. document.querySelector(".entry-content").innerHTML = ttpolicy.createHTML(data.content);
  678. const entryReadingtimeElement = document.querySelector(".entry-reading-time");
  679. if (entryReadingtimeElement) {
  680. entryReadingtimeElement.textContent = data.reading_time;
  681. }
  682. }
  683. });
  684. });
  685. }
  686. /**
  687. * Open the original link of an entry.
  688. *
  689. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  690. * @returns {void}
  691. */
  692. function openOriginalLinkAction(openLinkInCurrentTab) {
  693. if (isEntryView()) {
  694. openOriginalLinkFromEntryView(openLinkInCurrentTab);
  695. } else if (isListView()) {
  696. openOriginalLinkFromListView();
  697. }
  698. }
  699. /**
  700. * Open the original link from entry view.
  701. *
  702. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  703. * @returns {void}
  704. */
  705. function openOriginalLinkFromEntryView(openLinkInCurrentTab) {
  706. const entryLink = document.querySelector(".entry h1 a");
  707. if (!entryLink) return;
  708. const url = entryLink.getAttribute("href");
  709. if (openLinkInCurrentTab) {
  710. window.location.href = url;
  711. } else {
  712. openNewTab(url);
  713. }
  714. }
  715. /**
  716. * Open the original link from list view.
  717. *
  718. * @returns {void}
  719. */
  720. function openOriginalLinkFromListView() {
  721. const currentItem = document.querySelector(".current-item");
  722. const originalLink = currentItem?.querySelector(":is(a, button)[data-original-link]");
  723. if (!currentItem || !originalLink) return;
  724. // Open the link
  725. openNewTab(originalLink.getAttribute("href"));
  726. // Don't navigate or mark as read on starred page
  727. const isStarredPage = document.location.href === document.querySelector(':is(a, button)[data-page=starred]').href;
  728. if (isStarredPage) return;
  729. // Navigate to next item
  730. goToListItem(1);
  731. // Mark as read if currently unread
  732. if (currentItem.classList.replace("item-status-unread", "item-status-read")) {
  733. const entryID = parseInt(currentItem.dataset.id, 10);
  734. updateEntriesStatus([entryID], "read");
  735. }
  736. }
  737. /**
  738. * Open the comments link of an entry.
  739. *
  740. * @param {boolean} openLinkInCurrentTab - Whether to open the link in the current tab.
  741. * @returns {void}
  742. */
  743. function openCommentLinkAction(openLinkInCurrentTab) {
  744. const entryLink = document.querySelector(isListView() ? ".current-item :is(a, button)[data-comments-link]" : ":is(a, button)[data-comments-link]");
  745. if (entryLink) {
  746. if (openLinkInCurrentTab) {
  747. window.location.href = entryLink.getAttribute("href");
  748. } else {
  749. openNewTab(entryLink.getAttribute("href"));
  750. }
  751. }
  752. }
  753. /**
  754. * Open the selected item in the current view.
  755. *
  756. * If the current view is a list view, it will navigate to the link of the currently selected item.
  757. * If the current view is an entry view, it will navigate to the link of the entry.
  758. */
  759. function openSelectedItemAction() {
  760. const currentItemLink = document.querySelector(".current-item .item-title a");
  761. if (currentItemLink) {
  762. window.location.href = currentItemLink.getAttribute("href");
  763. }
  764. }
  765. /**
  766. * Unsubscribe from the feed of the currently selected item.
  767. */
  768. function handleRemoveFeedAction() {
  769. const unsubscribeLink = document.querySelector("[data-action=remove-feed]");
  770. if (unsubscribeLink) {
  771. sendPOSTRequest(unsubscribeLink.dataset.url).then(() => {
  772. window.location.href = unsubscribeLink.dataset.redirectUrl || window.location.href;
  773. });
  774. }
  775. }
  776. /**
  777. * Scroll the page to the currently selected item.
  778. */
  779. function scrollToCurrentItemAction() {
  780. const currentItem = document.querySelector(".current-item");
  781. if (currentItem) {
  782. scrollPageTo(currentItem, true);
  783. }
  784. }
  785. /**
  786. * Update the unread counter value.
  787. *
  788. * @param {number} delta - The amount to change the counter by.
  789. */
  790. function updateUnreadCounterValue(delta) {
  791. document.querySelectorAll("span.unread-counter").forEach((element) => {
  792. const oldValue = parseInt(element.textContent, 10);
  793. element.textContent = oldValue + delta;
  794. });
  795. if (window.location.href.endsWith('/unread')) {
  796. const oldValue = parseInt(document.title.split('(')[1], 10);
  797. const newValue = oldValue + delta;
  798. document.title = document.title.replace(/\(\d+\)/, `(${newValue})`);
  799. }
  800. }
  801. /**
  802. * Handle confirmation messages for actions that require user confirmation.
  803. *
  804. * This function modifies the link element to show a confirmation question with "Yes" and "No" buttons.
  805. * If the user clicks "Yes", it calls the provided callback with the URL and redirect URL.
  806. * If the user clicks "No", it either redirects to a no-action URL or restores the link element.
  807. *
  808. * @param {Element} linkElement - The link or button element that triggered the confirmation.
  809. * @param {function} callback - The callback function to execute if the user confirms the action.
  810. * @returns {void}
  811. */
  812. function handleConfirmationMessage(linkElement, callback) {
  813. if (linkElement.tagName !== 'A' && linkElement.tagName !== "BUTTON") {
  814. linkElement = linkElement.parentNode;
  815. }
  816. linkElement.style.display = "none";
  817. const containerElement = linkElement.parentNode;
  818. const questionElement = document.createElement("span");
  819. function createLoadingElement() {
  820. const loadingElement = document.createElement("span");
  821. loadingElement.className = "loading";
  822. loadingElement.appendChild(document.createTextNode(linkElement.dataset.labelLoading));
  823. questionElement.remove();
  824. containerElement.appendChild(loadingElement);
  825. }
  826. const yesElement = document.createElement("button");
  827. yesElement.appendChild(document.createTextNode(linkElement.dataset.labelYes));
  828. yesElement.onclick = (event) => {
  829. event.preventDefault();
  830. createLoadingElement();
  831. callback(linkElement.dataset.url, linkElement.dataset.redirectUrl);
  832. };
  833. const noElement = document.createElement("button");
  834. noElement.appendChild(document.createTextNode(linkElement.dataset.labelNo));
  835. noElement.onclick = (event) => {
  836. event.preventDefault();
  837. const noActionUrl = linkElement.dataset.noActionUrl;
  838. if (noActionUrl) {
  839. createLoadingElement();
  840. callback(noActionUrl, linkElement.dataset.redirectUrl);
  841. } else {
  842. linkElement.style.display = "inline";
  843. questionElement.remove();
  844. }
  845. };
  846. questionElement.className = "confirm";
  847. questionElement.appendChild(document.createTextNode(`${linkElement.dataset.labelQuestion} `));
  848. questionElement.appendChild(yesElement);
  849. questionElement.appendChild(document.createTextNode(", "));
  850. questionElement.appendChild(noElement);
  851. containerElement.appendChild(questionElement);
  852. }
  853. /**
  854. * Check if the player is actually playing a media
  855. *
  856. * @param mediaElement the player element itself
  857. * @returns {boolean}
  858. */
  859. function isPlayerPlaying(mediaElement) {
  860. return mediaElement &&
  861. mediaElement.currentTime > 0 &&
  862. !mediaElement.paused &&
  863. !mediaElement.ended &&
  864. mediaElement.readyState > 2; // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState
  865. }
  866. /**
  867. * Handle player progression save and mark as read on completion.
  868. *
  869. * This function is triggered on the `timeupdate` event of the media player.
  870. * It saves the current playback position and marks the entry as read if the completion percentage is reached.
  871. *
  872. * @param {Element} playerElement The media player element (audio or video).
  873. */
  874. function handlePlayerProgressionSaveAndMarkAsReadOnCompletion(playerElement) {
  875. if (!isPlayerPlaying(playerElement)) {
  876. return;
  877. }
  878. const currentPositionInSeconds = Math.floor(playerElement.currentTime);
  879. const lastKnownPositionInSeconds = parseInt(playerElement.dataset.lastPosition, 10);
  880. const markAsReadOnCompletion = parseFloat(playerElement.dataset.markReadOnCompletion);
  881. const recordInterval = 10;
  882. // We limit the number of update to only one by interval. Otherwise, we would have multiple update per seconds
  883. if (currentPositionInSeconds >= (lastKnownPositionInSeconds + recordInterval) ||
  884. currentPositionInSeconds <= (lastKnownPositionInSeconds - recordInterval)
  885. ) {
  886. playerElement.dataset.lastPosition = currentPositionInSeconds.toString();
  887. sendPOSTRequest(playerElement.dataset.saveUrl, { progression: currentPositionInSeconds });
  888. // Handle the mark as read on completion
  889. if (markAsReadOnCompletion >= 0 && playerElement.duration > 0) {
  890. const completion = currentPositionInSeconds / playerElement.duration;
  891. if (completion >= markAsReadOnCompletion) {
  892. handleEntryStatus("none", document.querySelector(":is(a, button)[data-toggle-status]"), true);
  893. }
  894. }
  895. }
  896. }
  897. /**
  898. * Handle media control actions like seeking and changing playback speed.
  899. *
  900. * This function is triggered by clicking on media control buttons.
  901. * It adjusts the playback position or speed of media elements with the same enclosure ID.
  902. *
  903. * @param {Element} mediaPlayerButtonElement
  904. */
  905. function handleMediaControlButtonClick(mediaPlayerButtonElement) {
  906. const actionType = mediaPlayerButtonElement.dataset.enclosureAction;
  907. const actionValue = parseFloat(mediaPlayerButtonElement.dataset.actionValue);
  908. const enclosureID = mediaPlayerButtonElement.dataset.enclosureId;
  909. const mediaElements = document.querySelectorAll(`audio[data-enclosure-id="${enclosureID}"],video[data-enclosure-id="${enclosureID}"]`);
  910. const speedIndicatorElements = document.querySelectorAll(`span.speed-indicator[data-enclosure-id="${enclosureID}"]`);
  911. mediaElements.forEach((mediaElement) => {
  912. switch (actionType) {
  913. case "seek":
  914. mediaElement.currentTime = Math.max(mediaElement.currentTime + actionValue, 0);
  915. break;
  916. case "speed":
  917. // 0.25 was chosen because it will allow to get back to 1x in two "faster" clicks.
  918. // A lower value would result in a playback rate of 0, effectively pausing playback.
  919. mediaElement.playbackRate = Math.max(0.25, mediaElement.playbackRate + actionValue);
  920. speedIndicatorElements.forEach((speedIndicatorElement) => {
  921. speedIndicatorElement.innerText = `${mediaElement.playbackRate.toFixed(2)}x`;
  922. });
  923. break;
  924. case "speed-reset":
  925. mediaElement.playbackRate = actionValue ;
  926. speedIndicatorElements.forEach((speedIndicatorElement) => {
  927. // 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.
  928. // The trick only works on rates less than 10, but it feels an acceptable trade-off considering the feature
  929. speedIndicatorElement.innerText = `${mediaElement.playbackRate.toFixed(2)}x`;
  930. });
  931. break;
  932. }
  933. });
  934. }
  935. /**
  936. * Initialize media player event handlers.
  937. */
  938. function initializeMediaPlayerHandlers() {
  939. document.querySelectorAll("button[data-enclosure-action]").forEach((element) => {
  940. element.addEventListener("click", () => handleMediaControlButtonClick(element));
  941. });
  942. // Set playback from the last position if available
  943. document.querySelectorAll("audio[data-last-position],video[data-last-position]").forEach((element) => {
  944. if (element.dataset.lastPosition) {
  945. element.currentTime = element.dataset.lastPosition;
  946. }
  947. element.ontimeupdate = () => handlePlayerProgressionSaveAndMarkAsReadOnCompletion(element);
  948. });
  949. // Set playback speed from the data attribute if available
  950. document.querySelectorAll("audio[data-playback-rate],video[data-playback-rate]").forEach((element) => {
  951. if (element.dataset.playbackRate) {
  952. element.playbackRate = element.dataset.playbackRate;
  953. if (element.dataset.enclosureId) {
  954. document.querySelectorAll(`span.speed-indicator[data-enclosure-id="${element.dataset.enclosureId}"]`).forEach((speedIndicatorElement) => {
  955. speedIndicatorElement.innerText = `${parseFloat(element.dataset.playbackRate).toFixed(2)}x`;
  956. });
  957. }
  958. }
  959. });
  960. }
  961. /**
  962. * Initialize the service worker and PWA installation prompt.
  963. */
  964. function initializeServiceWorker() {
  965. // Register service worker if supported
  966. if ("serviceWorker" in navigator) {
  967. const serviceWorkerURL = document.body.dataset.serviceWorkerUrl;
  968. if (serviceWorkerURL) {
  969. const ttpolicy = trustedTypes.createPolicy('url', {createScriptURL: src => src});
  970. navigator.serviceWorker.register(ttpolicy.createScriptURL(serviceWorkerURL), {
  971. type: "module"
  972. }).catch((error) => {
  973. console.error("Service Worker registration failed:", error);
  974. });
  975. }
  976. }
  977. // PWA installation prompt handling
  978. window.addEventListener("beforeinstallprompt", (event) => {
  979. let deferredPrompt = event;
  980. const promptHomeScreen = document.getElementById("prompt-home-screen");
  981. const btnAddToHomeScreen = document.getElementById("btn-add-to-home-screen");
  982. if (!promptHomeScreen || !btnAddToHomeScreen) return;
  983. promptHomeScreen.style.display = "block";
  984. btnAddToHomeScreen.addEventListener("click", (event) => {
  985. event.preventDefault();
  986. deferredPrompt.prompt();
  987. deferredPrompt.userChoice.then(() => {
  988. deferredPrompt = null;
  989. promptHomeScreen.style.display = "none";
  990. });
  991. });
  992. });
  993. }
  994. /**
  995. * Initialize WebAuthn handlers if supported.
  996. */
  997. function initializeWebAuthn() {
  998. if (typeof WebAuthnHandler !== 'function') return;
  999. if (!WebAuthnHandler.isWebAuthnSupported()) return;
  1000. const webauthnHandler = new WebAuthnHandler();
  1001. // Setup delete credentials handler
  1002. onClick("#webauthn-delete", () => { webauthnHandler.removeAllCredentials(); });
  1003. // Setup registration
  1004. const registerButton = document.getElementById("webauthn-register");
  1005. if (registerButton) {
  1006. registerButton.disabled = false;
  1007. onClick("#webauthn-register", () => {
  1008. webauthnHandler.register().catch((err) => WebAuthnHandler.showErrorMessage(err));
  1009. });
  1010. }
  1011. // Setup login
  1012. const loginButton = document.getElementById("webauthn-login");
  1013. const usernameField = document.getElementById("form-username");
  1014. if (loginButton && usernameField) {
  1015. const abortController = new AbortController();
  1016. loginButton.disabled = false;
  1017. onClick("#webauthn-login", () => {
  1018. abortController.abort();
  1019. webauthnHandler.login().catch(err => WebAuthnHandler.showErrorMessage(err));
  1020. });
  1021. webauthnHandler.conditionalLogin(abortController).catch(err => WebAuthnHandler.showErrorMessage(err));
  1022. }
  1023. }
  1024. /**
  1025. * Initialize keyboard shortcuts for navigation and actions.
  1026. */
  1027. function initializeKeyboardShortcuts() {
  1028. if (document.querySelector("body[data-disable-keyboard-shortcuts=true]")) return;
  1029. const keyboardHandler = new KeyboardHandler();
  1030. // Navigation shortcuts
  1031. keyboardHandler.on("g u", () => goToPage("unread"));
  1032. keyboardHandler.on("g b", () => goToPage("starred"));
  1033. keyboardHandler.on("g h", () => goToPage("history"));
  1034. keyboardHandler.on("g f", goToFeedOrFeedsPage);
  1035. keyboardHandler.on("g c", () => goToPage("categories"));
  1036. keyboardHandler.on("g s", () => goToPage("settings"));
  1037. keyboardHandler.on("g g", () => goToPreviousPage(TOP));
  1038. keyboardHandler.on("G", () => goToNextPage(BOTTOM));
  1039. keyboardHandler.on("/", () => goToPage("search"));
  1040. // Item navigation
  1041. keyboardHandler.on("ArrowLeft", goToPreviousPage);
  1042. keyboardHandler.on("ArrowRight", goToNextPage);
  1043. keyboardHandler.on("k", goToPreviousPage);
  1044. keyboardHandler.on("p", goToPreviousPage);
  1045. keyboardHandler.on("j", goToNextPage);
  1046. keyboardHandler.on("n", goToNextPage);
  1047. keyboardHandler.on("h", () => goToPage("previous"));
  1048. keyboardHandler.on("l", () => goToPage("next"));
  1049. keyboardHandler.on("z t", scrollToCurrentItemAction);
  1050. // Item actions
  1051. keyboardHandler.on("o", openSelectedItemAction);
  1052. keyboardHandler.on("Enter", () => openSelectedItemAction());
  1053. keyboardHandler.on("v", () => openOriginalLinkAction(false));
  1054. keyboardHandler.on("V", () => openOriginalLinkAction(true));
  1055. keyboardHandler.on("c", () => openCommentLinkAction(false));
  1056. keyboardHandler.on("C", () => openCommentLinkAction(true));
  1057. // Entry management
  1058. keyboardHandler.on("m", () => handleEntryStatus("next"));
  1059. keyboardHandler.on("M", () => handleEntryStatus("previous"));
  1060. keyboardHandler.on("A", markPageAsReadAction);
  1061. keyboardHandler.on("s", () => handleSaveEntryAction());
  1062. keyboardHandler.on("d", handleFetchOriginalContentAction);
  1063. keyboardHandler.on("f", () => handleStarAction());
  1064. // Feed actions
  1065. keyboardHandler.on("F", goToFeedPage);
  1066. keyboardHandler.on("R", handleRefreshAllFeedsAction);
  1067. keyboardHandler.on("+", goToAddSubscriptionPage);
  1068. keyboardHandler.on("#", handleRemoveFeedAction);
  1069. // UI actions
  1070. keyboardHandler.on("?", showKeyboardShortcutsAction);
  1071. keyboardHandler.on("a", () => {
  1072. const enclosureElement = document.querySelector('.entry-enclosures');
  1073. if (enclosureElement) {
  1074. enclosureElement.toggleAttribute('open');
  1075. }
  1076. });
  1077. keyboardHandler.listen();
  1078. }
  1079. /**
  1080. * Initialize touch handler for mobile devices.
  1081. */
  1082. function initializeTouchHandler() {
  1083. if ( "ontouchstart" in window || navigator.maxTouchPoints > 0) {
  1084. const touchHandler = new TouchHandler();
  1085. touchHandler.listen();
  1086. }
  1087. }
  1088. /**
  1089. * Initialize click handlers for various UI elements.
  1090. */
  1091. function initializeClickHandlers() {
  1092. // Entry actions
  1093. onClick(":is(a, button)[data-save-entry]", (event) => handleSaveEntryAction(event.target));
  1094. onClick(":is(a, button)[data-toggle-starred]", (event) => handleStarAction(event.target));
  1095. onClick(":is(a, button)[data-toggle-status]", (event) => handleEntryStatus("next", event.target));
  1096. onClick(":is(a, button)[data-fetch-content-entry]", handleFetchOriginalContentAction);
  1097. onClick(":is(a, button)[data-share-status]", handleEntryShareAction);
  1098. // Page actions with confirmation
  1099. onClick(":is(a, button)[data-action=markPageAsRead]", (event) => handleConfirmationMessage(event.target, markPageAsReadAction));
  1100. // Generic confirmation handler
  1101. onClick(":is(a, button)[data-confirm]", (event) => {
  1102. handleConfirmationMessage(event.target, (url, redirectURL) => {
  1103. sendPOSTRequest(url).then((response) => {
  1104. if (redirectURL) {
  1105. window.location.href = redirectURL;
  1106. } else if (response?.redirected && response.url) {
  1107. window.location.href = response.url;
  1108. } else {
  1109. window.location.reload();
  1110. }
  1111. });
  1112. });
  1113. });
  1114. // Original link handlers (both click and middle-click)
  1115. const handleOriginalLink = (event) => handleEntryStatus("next", event.target, true);
  1116. onClick("a[data-original-link='true']", handleOriginalLink, true);
  1117. onAuxClick("a[data-original-link='true']", (event) => {
  1118. if (event.button === 1) {
  1119. handleOriginalLink(event);
  1120. }
  1121. }, true);
  1122. }
  1123. // Initialize application handlers
  1124. initializeMainMenuHandlers();
  1125. initializeFormHandlers();
  1126. initializeMediaPlayerHandlers();
  1127. initializeWebAuthn();
  1128. initializeKeyboardShortcuts();
  1129. initializeTouchHandler();
  1130. initializeClickHandlers();
  1131. initializeServiceWorker();
  1132. // Reload the page if it was restored from the back-forward cache and mark entries as read is enabled.
  1133. window.addEventListener("pageshow", (event) => {
  1134. if (event.persisted && document.body.dataset.markAsReadOnView === "true") {
  1135. location.reload();
  1136. }
  1137. });