app.js 23 KB

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