app.js 43 KB

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