app.js 43 KB

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