2024-10-09 20:52:28 +03:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import Layout from '@theme-original/Layout';
|
|
|
|
import Head from '@docusaurus/Head';
|
2024-10-10 09:15:58 +03:00
|
|
|
import { useLocation } from '@docusaurus/router';
|
2024-10-09 20:52:28 +03:00
|
|
|
|
|
|
|
export default function CustomLayout(props) {
|
2024-10-10 09:15:58 +03:00
|
|
|
const location = useLocation();
|
2024-10-09 20:52:28 +03:00
|
|
|
|
2024-10-10 09:15:58 +03:00
|
|
|
useEffect(() => {
|
|
|
|
// Проверяем, находимся ли мы на странице документации
|
2024-10-09 22:58:48 +03:00
|
|
|
if (!location.pathname.startsWith('/docs/')) {
|
2024-10-10 09:15:58 +03:00
|
|
|
return; // Если нет, не выполняем код для рекламы
|
2024-10-09 22:58:48 +03:00
|
|
|
}
|
|
|
|
|
2024-10-10 09:15:58 +03:00
|
|
|
// Функция для рендеринга рекламы
|
|
|
|
const renderAds = () => {
|
|
|
|
// Удаляем предыдущий скрипт рендеринга, если он существует
|
|
|
|
const existingScript = document.getElementById('yandex-ad-script');
|
|
|
|
if (existingScript) {
|
|
|
|
existingScript.remove();
|
|
|
|
}
|
|
|
|
|
2024-10-10 09:36:36 +03:00
|
|
|
// Удаляем основной скрипт Yandex, если он есть
|
|
|
|
const existingYandexScript = document.getElementById('yandex-ads-context-script');
|
|
|
|
if (existingYandexScript) {
|
|
|
|
existingYandexScript.remove();
|
|
|
|
}
|
2024-10-10 09:15:58 +03:00
|
|
|
|
2024-10-10 09:36:36 +03:00
|
|
|
// Создаем новый скрипт для основного Yandex Ads
|
|
|
|
const yandexScript = document.createElement('script');
|
|
|
|
yandexScript.id = 'yandex-ads-context-script';
|
|
|
|
yandexScript.src = `https://yandex.ru/ads/system/context.js?timestamp=${Date.now()}`;
|
|
|
|
yandexScript.async = true;
|
|
|
|
document.head.appendChild(yandexScript);
|
|
|
|
|
|
|
|
// Ждем, пока основной скрипт загрузится, чтобы выполнить рендеринг рекламы
|
|
|
|
yandexScript.onload = () => {
|
|
|
|
// Создаем новый скрипт для рендеринга рекламы
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.id = 'yandex-ad-script';
|
|
|
|
script.innerHTML = `
|
|
|
|
window.yaContextCb.push(() => {
|
|
|
|
Ya.Context.AdvManager.render({
|
|
|
|
blockId: "R-A-12294791-1",
|
|
|
|
renderTo: "yandex_rtb_R-A-12294791-1",
|
|
|
|
type: "feed"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
document.body.appendChild(script);
|
|
|
|
};
|
2024-10-10 09:15:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Рендерим рекламные блоки при загрузке страницы или изменении маршрута
|
|
|
|
renderAds();
|
|
|
|
|
2024-10-10 09:36:36 +03:00
|
|
|
// Удаляем скрипт при размонтировании или смене страницы
|
2024-10-09 20:52:28 +03:00
|
|
|
return () => {
|
2024-10-10 09:15:58 +03:00
|
|
|
const existingScript = document.getElementById('yandex-ad-script');
|
2024-10-10 09:36:36 +03:00
|
|
|
const existingYandexScript = document.getElementById('yandex-ads-context-script');
|
2024-10-10 09:15:58 +03:00
|
|
|
if (existingScript) existingScript.remove();
|
2024-10-10 09:36:36 +03:00
|
|
|
if (existingYandexScript) existingYandexScript.remove();
|
2024-10-09 20:52:28 +03:00
|
|
|
};
|
2024-10-10 09:15:58 +03:00
|
|
|
}, [location.pathname]); // Следим за изменением пути
|
2024-10-09 20:52:28 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Head>
|
|
|
|
<script>
|
|
|
|
window.yaContextCb = window.yaContextCb || [];
|
|
|
|
</script>
|
|
|
|
</Head>
|
|
|
|
<Layout {...props} />
|
|
|
|
</>
|
|
|
|
);
|
2024-10-10 09:36:36 +03:00
|
|
|
}
|