templates/components/profile_ctr.js.twig line 1

Open in your IDE?
  1. <script>
  2.     window.increase_shows = function ({profiles, route = '{{ app.request.attributes.get('_route') }}', forceSend = false}) {
  3.         const data = {
  4.             'profiles': profiles,
  5.             'source': route,
  6.             'forceSend': forceSend
  7.         };
  8.         const endpoint = '{{ path("profile_ctr.increase_shows") }}';
  9.         // При закрытии страницы используем sendBeacon для надежной доставки
  10.         if (forceSend && navigator.sendBeacon) {
  11.             const blob = new Blob([JSON.stringify(data)], {type: 'application/json'});
  12.             try {
  13.                 navigator.sendBeacon(endpoint, blob);
  14.                 return Promise.resolve();
  15.             } catch (error) {
  16.                 console.error('Error sendBeacon, falling back to fetch', error);
  17.                 // Fallback to fetch if sendBeacon fails
  18.             }
  19.         }
  20.         // Обычная асинхронная отправка через fetch
  21.         return fetch(endpoint, {
  22.             method: 'POST',
  23.             headers: {
  24.                 "X-Requested-With": "XMLHttpRequest"
  25.             },
  26.             body: JSON.stringify(data)
  27.         }).catch(error => {
  28.             console.error('Error increase_shows', error);
  29.         })
  30.     }
  31.     window.increase_views = function (profileId) {
  32.         fetch('{{ path("profile_ctr.increase_views") }}', {
  33.             method: 'POST',
  34.             headers: {
  35.                 "X-Requested-With": "XMLHttpRequest"
  36.             },
  37.             body: JSON.stringify({
  38.                 'profile': profileId,
  39.             })
  40.         }).catch(error => {
  41.             console.error('Error increase_views', error);
  42.         })
  43.     }
  44. </script>