app.js 23 KB

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