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