app.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // OnClick attaches a listener to the elements that match the selector.
  2. function onClick(selector, callback, noPreventDefault) {
  3. let elements = document.querySelectorAll(selector);
  4. elements.forEach((element) => {
  5. element.onclick = (event) => {
  6. if (!noPreventDefault) {
  7. event.preventDefault();
  8. }
  9. callback(event);
  10. };
  11. });
  12. }
  13. function onAuxClick(selector, callback, noPreventDefault) {
  14. let elements = document.querySelectorAll(selector);
  15. elements.forEach((element) => {
  16. element.onauxclick = (event) => {
  17. if (!noPreventDefault) {
  18. event.preventDefault();
  19. }
  20. callback(event);
  21. };
  22. });
  23. }
  24. // make logo element as button on mobile layout
  25. function checkMenuToggleModeByLayout() {
  26. const logoElement = document.querySelector(".logo");
  27. const homePageLinkElement = document.querySelector(".logo > a")
  28. if (!logoElement) return
  29. const logoToggleButtonLabel = logoElement.getAttribute("data-toggle-button-label")
  30. const navMenuElement = document.getElementById("header-menu");
  31. const navMenuElementIsExpanded = navMenuElement.classList.contains("js-menu-show")
  32. if (document.documentElement.clientWidth < 620) {
  33. logoElement.setAttribute("role", "button");
  34. logoElement.setAttribute("tabindex", "0");
  35. logoElement.setAttribute("aria-label", logoToggleButtonLabel)
  36. if (navMenuElementIsExpanded) {
  37. logoElement.setAttribute("aria-expanded", "true")
  38. } else {
  39. logoElement.setAttribute("aria-expanded", "false")
  40. }
  41. homePageLinkElement.setAttribute("tabindex", "-1")
  42. } else {
  43. logoElement.removeAttribute("role");
  44. logoElement.removeAttribute("tabindex");
  45. logoElement.removeAttribute("aria-expanded");
  46. logoElement.removeAttribute("aria-label")
  47. homePageLinkElement.removeAttribute("tabindex");
  48. }
  49. }
  50. function fixVoiceOverDetailsSummaryBug() {
  51. const detailsElements = document.querySelectorAll("details")
  52. detailsElements.forEach((details) => {
  53. const summaryElement = details.querySelector("summary")
  54. summaryElement.setAttribute("role", "button")
  55. setSummaryAriaExpandedByDetails(details, summaryElement)
  56. details.addEventListener("toggle", () => {
  57. setSummaryAriaExpandedByDetails(details, summaryElement)
  58. })
  59. })
  60. function setSummaryAriaExpandedByDetails(details, summary) {
  61. if (details.open) {
  62. summary.setAttribute("aria-expanded", "true")
  63. } else {
  64. summary.setAttribute("aria-expanded", "false")
  65. }
  66. }
  67. }
  68. // Show and hide the main menu on mobile devices.
  69. function toggleMainMenu(event) {
  70. if (event.type === "keydown" && !(event.key === "Enter" || event.key === " ")) {
  71. return
  72. }
  73. if (event.currentTarget.getAttribute("role")) {
  74. event.preventDefault()
  75. }
  76. let menu = document.querySelector(".header nav ul");
  77. let menuToggleButton = document.querySelector(".logo");
  78. if (menu.classList.contains("js-menu-show")) {
  79. menu.classList.remove("js-menu-show")
  80. menuToggleButton.setAttribute("aria-expanded", false)
  81. } else {
  82. menu.classList.add("js-menu-show")
  83. menuToggleButton.setAttribute("aria-expanded", true)
  84. }
  85. }
  86. // Handle click events for the main menu (<li> and <a>).
  87. function onClickMainMenuListItem(event) {
  88. let element = event.target;
  89. if (element.tagName === "A") {
  90. window.location.href = element.getAttribute("href");
  91. } else {
  92. window.location.href = element.querySelector("a").getAttribute("href");
  93. }
  94. }
  95. // Change the button label when the page is loading.
  96. function handleSubmitButtons() {
  97. let elements = document.querySelectorAll("form");
  98. elements.forEach((element) => {
  99. element.onsubmit = () => {
  100. let button = element.querySelector("button");
  101. if (button) {
  102. button.innerHTML = button.dataset.labelLoading;
  103. button.disabled = true;
  104. }
  105. };
  106. });
  107. }
  108. // Show modal dialog with the list of keyboard shortcuts.
  109. function showKeyboardShortcuts() {
  110. let template = document.getElementById("keyboard-shortcuts");
  111. if (template !== null) {
  112. ModalHandler.open(template.content, "dialog-title");
  113. }
  114. }
  115. // Mark as read visible items of the current page.
  116. function markPageAsRead() {
  117. let items = DomHelper.getVisibleElements(".items .item");
  118. let entryIDs = [];
  119. items.forEach((element) => {
  120. element.classList.add("item-status-read");
  121. entryIDs.push(parseInt(element.dataset.id, 10));
  122. });
  123. if (entryIDs.length > 0) {
  124. updateEntriesStatus(entryIDs, "read", () => {
  125. // Make sure the Ajax request reach the server before we reload the page.
  126. let element = document.querySelector(":is(a, button)[data-action=markPageAsRead]");
  127. let showOnlyUnread = false;
  128. if (element) {
  129. showOnlyUnread = element.dataset.showOnlyUnread || false;
  130. }
  131. if (showOnlyUnread) {
  132. window.location.href = window.location.href;
  133. } else {
  134. goToPage("next", true);
  135. }
  136. });
  137. }
  138. }
  139. /**
  140. * Handle entry status changes from the list view and entry view.
  141. * Focus the next or the previous entry if it exists.
  142. * @param {string} item Item to focus: "previous" or "next".
  143. * @param {Element} element
  144. * @param {boolean} setToRead
  145. */
  146. function handleEntryStatus(item, element, setToRead) {
  147. let toasting = !element;
  148. let currentEntry = findEntry(element);
  149. if (currentEntry) {
  150. if (!setToRead || currentEntry.querySelector(":is(a, button)[data-toggle-status]").dataset.value == "unread") {
  151. toggleEntryStatus(currentEntry, toasting);
  152. }
  153. if (isListView() && currentEntry.classList.contains('current-item')) {
  154. switch (item) {
  155. case "previous":
  156. goToListItem(-1);
  157. break;
  158. case "next":
  159. goToListItem(1);
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. // Change the entry status to the opposite value.
  166. function toggleEntryStatus(element, toasting) {
  167. let entryID = parseInt(element.dataset.id, 10);
  168. let link = element.querySelector(":is(a, button)[data-toggle-status]");
  169. let currentStatus = link.dataset.value;
  170. let newStatus = currentStatus === "read" ? "unread" : "read";
  171. link.querySelector("span").innerHTML = link.dataset.labelLoading;
  172. updateEntriesStatus([entryID], newStatus, () => {
  173. let iconElement, label;
  174. if (currentStatus === "read") {
  175. iconElement = document.querySelector("template#icon-read");
  176. label = link.dataset.labelRead;
  177. if (toasting) {
  178. showToast(link.dataset.toastUnread, iconElement);
  179. }
  180. } else {
  181. iconElement = document.querySelector("template#icon-unread");
  182. label = link.dataset.labelUnread;
  183. if (toasting) {
  184. showToast(link.dataset.toastRead, iconElement);
  185. }
  186. }
  187. link.innerHTML = iconElement.innerHTML + '<span class="icon-label">' + label + '</span>';
  188. link.dataset.value = newStatus;
  189. if (element.classList.contains("item-status-" + currentStatus)) {
  190. element.classList.remove("item-status-" + currentStatus);
  191. element.classList.add("item-status-" + newStatus);
  192. }
  193. });
  194. }
  195. // Mark a single entry as read.
  196. function markEntryAsRead(element) {
  197. if (element.classList.contains("item-status-unread")) {
  198. element.classList.remove("item-status-unread");
  199. element.classList.add("item-status-read");
  200. let entryID = parseInt(element.dataset.id, 10);
  201. updateEntriesStatus([entryID], "read");
  202. }
  203. }
  204. // Send the Ajax request to refresh all feeds in the background
  205. function handleRefreshAllFeeds() {
  206. let url = document.body.dataset.refreshAllFeedsUrl;
  207. if (url) {
  208. window.location.href = url;
  209. }
  210. }
  211. // Send the Ajax request to change entries statuses.
  212. function updateEntriesStatus(entryIDs, status, callback) {
  213. let url = document.body.dataset.entriesStatusUrl;
  214. let request = new RequestBuilder(url);
  215. request.withBody({entry_ids: entryIDs, status: status});
  216. request.withCallback((resp) => {
  217. resp.json().then(count => {
  218. if (callback) {
  219. callback(resp);
  220. }
  221. if (status === "read") {
  222. decrementUnreadCounter(count);
  223. } else {
  224. incrementUnreadCounter(count);
  225. }
  226. });
  227. });
  228. request.execute();
  229. }
  230. // Handle save entry from list view and entry view.
  231. function handleSaveEntry(element) {
  232. let toasting = !element;
  233. let currentEntry = findEntry(element);
  234. if (currentEntry) {
  235. saveEntry(currentEntry.querySelector(":is(a, button)[data-save-entry]"), toasting);
  236. }
  237. }
  238. // Send the Ajax request to save an entry.
  239. function saveEntry(element, toasting) {
  240. if (!element) {
  241. return;
  242. }
  243. if (element.dataset.completed) {
  244. return;
  245. }
  246. element.innerHTML = '<span class="icon-label">' + element.dataset.labelLoading + '</span>';
  247. let request = new RequestBuilder(element.dataset.saveUrl);
  248. request.withCallback(() => {
  249. element.innerHTML = '<span class="icon-label">' + element.dataset.labelDone + '</span>';
  250. element.dataset.completed = true;
  251. if (toasting) {
  252. let iconElement = document.querySelector("template#icon-save");
  253. showToast(element.dataset.toastDone, iconElement);
  254. }
  255. });
  256. request.execute();
  257. }
  258. // Handle bookmark from the list view and entry view.
  259. function handleBookmark(element) {
  260. let toasting = !element;
  261. let currentEntry = findEntry(element);
  262. if (currentEntry) {
  263. toggleBookmark(currentEntry, toasting);
  264. }
  265. }
  266. // Send the Ajax request and change the icon when bookmarking an entry.
  267. function toggleBookmark(parentElement, toasting) {
  268. let element = parentElement.querySelector(":is(a, button)[data-toggle-bookmark]");
  269. if (!element) {
  270. return;
  271. }
  272. element.innerHTML = '<span class="icon-label">' + element.dataset.labelLoading + '</span>';
  273. let request = new RequestBuilder(element.dataset.bookmarkUrl);
  274. request.withCallback(() => {
  275. let currentStarStatus = element.dataset.value;
  276. let newStarStatus = currentStarStatus === "star" ? "unstar" : "star";
  277. let iconElement, label;
  278. if (currentStarStatus === "star") {
  279. iconElement = document.querySelector("template#icon-star");
  280. label = element.dataset.labelStar;
  281. if (toasting) {
  282. showToast(element.dataset.toastUnstar, iconElement);
  283. }
  284. } else {
  285. iconElement = document.querySelector("template#icon-unstar");
  286. label = element.dataset.labelUnstar;
  287. if (toasting) {
  288. showToast(element.dataset.toastStar, iconElement);
  289. }
  290. }
  291. element.innerHTML = iconElement.innerHTML + '<span class="icon-label">' + label + '</span>';
  292. element.dataset.value = newStarStatus;
  293. });
  294. request.execute();
  295. }
  296. // Send the Ajax request to download the original web page.
  297. function handleFetchOriginalContent() {
  298. if (isListView()) {
  299. return;
  300. }
  301. let element = document.querySelector(":is(a, button)[data-fetch-content-entry]");
  302. if (!element) {
  303. return;
  304. }
  305. let previousInnerHTML = element.innerHTML;
  306. element.innerHTML = '<span class="icon-label">' + element.dataset.labelLoading + '</span>';
  307. let request = new RequestBuilder(element.dataset.fetchContentUrl);
  308. request.withCallback((response) => {
  309. element.innerHTML = previousInnerHTML;
  310. response.json().then((data) => {
  311. if (data.hasOwnProperty("content") && data.hasOwnProperty("reading_time")) {
  312. document.querySelector(".entry-content").innerHTML = data.content;
  313. let entryReadingtimeElement = document.querySelector(".entry-reading-time");
  314. if (entryReadingtimeElement) {
  315. entryReadingtimeElement.innerHTML = data.reading_time;
  316. }
  317. }
  318. });
  319. });
  320. request.execute();
  321. }
  322. function openOriginalLink(openLinkInCurrentTab) {
  323. let entryLink = document.querySelector(".entry h1 a");
  324. if (entryLink !== null) {
  325. if (openLinkInCurrentTab) {
  326. window.location.href = entryLink.getAttribute("href");
  327. } else {
  328. DomHelper.openNewTab(entryLink.getAttribute("href"));
  329. }
  330. return;
  331. }
  332. let currentItemOriginalLink = document.querySelector(".current-item :is(a, button)[data-original-link]");
  333. if (currentItemOriginalLink !== null) {
  334. DomHelper.openNewTab(currentItemOriginalLink.getAttribute("href"));
  335. let currentItem = document.querySelector(".current-item");
  336. // If we are not on the list of starred items, move to the next item
  337. if (document.location.href != document.querySelector(':is(a, button)[data-page=starred]').href) {
  338. goToListItem(1);
  339. }
  340. markEntryAsRead(currentItem);
  341. }
  342. }
  343. function openCommentLink(openLinkInCurrentTab) {
  344. if (!isListView()) {
  345. let entryLink = document.querySelector(":is(a, button)[data-comments-link]");
  346. if (entryLink !== null) {
  347. if (openLinkInCurrentTab) {
  348. window.location.href = entryLink.getAttribute("href");
  349. } else {
  350. DomHelper.openNewTab(entryLink.getAttribute("href"));
  351. }
  352. return;
  353. }
  354. } else {
  355. let currentItemCommentsLink = document.querySelector(".current-item :is(a, button)[data-comments-link]");
  356. if (currentItemCommentsLink !== null) {
  357. DomHelper.openNewTab(currentItemCommentsLink.getAttribute("href"));
  358. }
  359. }
  360. }
  361. function openSelectedItem() {
  362. let currentItemLink = document.querySelector(".current-item .item-title a");
  363. if (currentItemLink !== null) {
  364. window.location.href = currentItemLink.getAttribute("href");
  365. }
  366. }
  367. function unsubscribeFromFeed() {
  368. let unsubscribeLinks = document.querySelectorAll("[data-action=remove-feed]");
  369. if (unsubscribeLinks.length === 1) {
  370. let unsubscribeLink = unsubscribeLinks[0];
  371. let request = new RequestBuilder(unsubscribeLink.dataset.url);
  372. request.withCallback(() => {
  373. if (unsubscribeLink.dataset.redirectUrl) {
  374. window.location.href = unsubscribeLink.dataset.redirectUrl;
  375. } else {
  376. window.location.reload();
  377. }
  378. });
  379. request.execute();
  380. }
  381. }
  382. /**
  383. * @param {string} page Page to redirect to.
  384. * @param {boolean} fallbackSelf Refresh actual page if the page is not found.
  385. */
  386. function goToPage(page, fallbackSelf) {
  387. let element = document.querySelector(":is(a, button)[data-page=" + page + "]");
  388. if (element) {
  389. document.location.href = element.href;
  390. } else if (fallbackSelf) {
  391. window.location.reload();
  392. }
  393. }
  394. function goToPrevious() {
  395. if (isListView()) {
  396. goToListItem(-1);
  397. } else {
  398. goToPage("previous");
  399. }
  400. }
  401. function goToNext() {
  402. if (isListView()) {
  403. goToListItem(1);
  404. } else {
  405. goToPage("next");
  406. }
  407. }
  408. function goToFeedOrFeeds() {
  409. if (isEntry()) {
  410. goToFeed();
  411. } else {
  412. goToPage('feeds');
  413. }
  414. }
  415. function goToFeed() {
  416. if (isEntry()) {
  417. let feedAnchor = document.querySelector("span.entry-website a");
  418. if (feedAnchor !== null) {
  419. window.location.href = feedAnchor.href;
  420. }
  421. } else {
  422. let currentItemFeed = document.querySelector(".current-item :is(a, button)[data-feed-link]");
  423. if (currentItemFeed !== null) {
  424. window.location.href = currentItemFeed.getAttribute("href");
  425. }
  426. }
  427. }
  428. /**
  429. * @param {number} offset How many items to jump for focus.
  430. */
  431. function goToListItem(offset) {
  432. let items = DomHelper.getVisibleElements(".items .item");
  433. if (items.length === 0) {
  434. return;
  435. }
  436. if (document.querySelector(".current-item") === null) {
  437. items[0].classList.add("current-item");
  438. items[0].focus();
  439. return;
  440. }
  441. for (let i = 0; i < items.length; i++) {
  442. if (items[i].classList.contains("current-item")) {
  443. items[i].classList.remove("current-item");
  444. let item = items[(i + offset + items.length) % items.length];
  445. item.classList.add("current-item");
  446. DomHelper.scrollPageTo(item);
  447. item.focus();
  448. break;
  449. }
  450. }
  451. }
  452. function scrollToCurrentItem() {
  453. let currentItem = document.querySelector(".current-item");
  454. if (currentItem !== null) {
  455. DomHelper.scrollPageTo(currentItem, true);
  456. }
  457. }
  458. function decrementUnreadCounter(n) {
  459. updateUnreadCounterValue((current) => {
  460. return current - n;
  461. });
  462. }
  463. function incrementUnreadCounter(n) {
  464. updateUnreadCounterValue((current) => {
  465. return current + n;
  466. });
  467. }
  468. function updateUnreadCounterValue(callback) {
  469. let counterElements = document.querySelectorAll("span.unread-counter");
  470. counterElements.forEach((element) => {
  471. let oldValue = parseInt(element.textContent, 10);
  472. element.innerHTML = callback(oldValue);
  473. });
  474. if (window.location.href.endsWith('/unread')) {
  475. let oldValue = parseInt(document.title.split('(')[1], 10);
  476. let newValue = callback(oldValue);
  477. document.title = document.title.replace(
  478. /(.*?)\(\d+\)(.*?)/,
  479. function (match, prefix, suffix, offset, string) {
  480. return prefix + '(' + newValue + ')' + suffix;
  481. }
  482. );
  483. }
  484. }
  485. function isEntry() {
  486. return document.querySelector("section.entry") !== null;
  487. }
  488. function isListView() {
  489. return document.querySelector(".items") !== null;
  490. }
  491. function findEntry(element) {
  492. if (isListView()) {
  493. if (element) {
  494. return DomHelper.findParent(element, "item");
  495. } else {
  496. return document.querySelector(".current-item");
  497. }
  498. } else {
  499. return document.querySelector(".entry");
  500. }
  501. }
  502. function handleConfirmationMessage(linkElement, callback) {
  503. if (linkElement.tagName != 'A' && linkElement.tagName != "BUTTON") {
  504. linkElement = linkElement.parentNode;
  505. }
  506. linkElement.style.display = "none";
  507. let containerElement = linkElement.parentNode;
  508. let questionElement = document.createElement("span");
  509. function createLoadingElement() {
  510. let loadingElement = document.createElement("span");
  511. loadingElement.className = "loading";
  512. loadingElement.appendChild(document.createTextNode(linkElement.dataset.labelLoading));
  513. questionElement.remove();
  514. containerElement.appendChild(loadingElement);
  515. }
  516. let yesElement = document.createElement("button");
  517. yesElement.appendChild(document.createTextNode(linkElement.dataset.labelYes));
  518. yesElement.onclick = (event) => {
  519. event.preventDefault();
  520. createLoadingElement();
  521. callback(linkElement.dataset.url, linkElement.dataset.redirectUrl);
  522. };
  523. let noElement = document.createElement("button");
  524. noElement.appendChild(document.createTextNode(linkElement.dataset.labelNo));
  525. noElement.onclick = (event) => {
  526. event.preventDefault();
  527. const noActionUrl = linkElement.dataset.noActionUrl;
  528. if (noActionUrl) {
  529. createLoadingElement();
  530. callback(noActionUrl, linkElement.dataset.redirectUrl);
  531. } else {
  532. linkElement.style.display = "inline";
  533. questionElement.remove();
  534. }
  535. };
  536. questionElement.className = "confirm";
  537. questionElement.appendChild(document.createTextNode(linkElement.dataset.labelQuestion + " "));
  538. questionElement.appendChild(yesElement);
  539. questionElement.appendChild(document.createTextNode(", "));
  540. questionElement.appendChild(noElement);
  541. containerElement.appendChild(questionElement);
  542. }
  543. function showToast(label, iconElement) {
  544. if (!label || !iconElement) {
  545. return;
  546. }
  547. const toastMsgElement = document.getElementById("toast-msg");
  548. if (toastMsgElement) {
  549. toastMsgElement.innerHTML = iconElement.innerHTML + '<span class="icon-label">' + label + '</span>';
  550. const toastElementWrapper = document.getElementById("toast-wrapper");
  551. if (toastElementWrapper) {
  552. toastElementWrapper.classList.remove('toast-animate');
  553. setTimeout(function () {
  554. toastElementWrapper.classList.add('toast-animate');
  555. }, 100);
  556. }
  557. }
  558. }
  559. /** Navigate to the new subscription page. */
  560. function goToAddSubscription() {
  561. window.location.href = document.body.dataset.addSubscriptionUrl;
  562. }
  563. /**
  564. * save player position to allow to resume playback later
  565. * @param {Element} playerElement
  566. */
  567. function handlePlayerProgressionSave(playerElement) {
  568. const currentPositionInSeconds = Math.floor(playerElement.currentTime); // we do not need a precise value
  569. const lastKnownPositionInSeconds = parseInt(playerElement.dataset.lastPosition, 10);
  570. const recordInterval = 10;
  571. // we limit the number of update to only one by interval. Otherwise, we would have multiple update per seconds
  572. if (currentPositionInSeconds >= (lastKnownPositionInSeconds + recordInterval) ||
  573. currentPositionInSeconds <= (lastKnownPositionInSeconds - recordInterval)
  574. ) {
  575. playerElement.dataset.lastPosition = currentPositionInSeconds.toString();
  576. let request = new RequestBuilder(playerElement.dataset.saveUrl);
  577. request.withBody({progression: currentPositionInSeconds});
  578. request.execute();
  579. }
  580. }
  581. /**
  582. * handle new share entires and already shared entries
  583. */
  584. function handleShare() {
  585. let link = document.querySelector(':is(a, button)[data-share-status]');
  586. let title = document.querySelector("body > main > section > header > h1 > a");
  587. if (link.dataset.shareStatus === "shared") {
  588. checkShareAPI(title, link.href);
  589. }
  590. if (link.dataset.shareStatus === "share") {
  591. let request = new RequestBuilder(link.href);
  592. request.withCallback((r) => {
  593. checkShareAPI(title, r.url);
  594. });
  595. request.withHttpMethod("GET");
  596. request.execute();
  597. }
  598. }
  599. /**
  600. * wrapper for Web Share API
  601. */
  602. function checkShareAPI(title, url) {
  603. if (!navigator.canShare) {
  604. console.error("Your browser doesn't support the Web Share API.");
  605. window.location = url;
  606. return;
  607. }
  608. try {
  609. navigator.share({
  610. title: title,
  611. url: url
  612. });
  613. window.location.reload();
  614. } catch (err) {
  615. console.error(err);
  616. window.location.reload();
  617. }
  618. }
  619. function getCsrfToken() {
  620. let element = document.querySelector("body[data-csrf-token]");
  621. if (element !== null) {
  622. return element.dataset.csrfToken;
  623. }
  624. return "";
  625. }