1
0
mirror of https://github.com/Bayselonarrend/OpenIntegrations.git synced 2025-06-08 23:56:33 +02:00

Update Layout.js

This commit is contained in:
Anton Titovets 2024-10-10 09:57:45 +03:00
parent bb58824666
commit b54ff728ed

View File

@ -1,67 +1,61 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import Layout from '@theme-original/Layout'; import Layout from '@theme-original/Layout';
import Head from '@docusaurus/Head'; import Head from '@docusaurus/Head';
import { useLocation } from '@docusaurus/router';
export default function CustomLayout(props) { export default function CustomLayout(props) {
const location = useLocation();
useEffect(() => { useEffect(() => {
// Проверяем, находимся ли мы на странице документации
if (!location.pathname.startsWith('/docs/')) { if (!location.pathname.startsWith('/docs/')) {
return; // Если нет, не выполняем код для рекламы return; // Если нет, не добавляем блок
} }
// Функция для рендеринга рекламы // Создаем контейнер для второго рекламного блока
const renderAds = () => { const adContainer = document.createElement('div');
// Удаляем предыдущий скрипт рендеринга, если он существует adContainer.id = 'R-A-12294791-5';
const existingScript = document.getElementById('yandex-ad-script'); adContainer.style.marginTop = '20px'; // Добавим отступ для красоты
if (existingScript) {
existingScript.remove(); // Находим элемент кнопок "Previous" и "Next" и вставляем перед ним
const paginationElement = document.querySelector('.pagination-nav');
if (paginationElement) {
paginationElement.parentNode.insertBefore(adContainer, paginationElement);
} else {
// Если кнопок нет, добавляем контейнер в конец body
document.body.appendChild(adContainer);
} }
// Удаляем основной скрипт Yandex, если он есть
const existingYandexScript = document.getElementById('yandex-ads-context-script');
if (existingYandexScript) {
existingYandexScript.remove();
}
// Создаем новый скрипт для основного 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'); const script = document.createElement('script');
script.id = 'yandex-ad-script';
script.innerHTML = ` script.innerHTML = `
window.yaContextCb.push(() => { window.yaContextCb.push(() => {
Ya.Context.AdvManager.render({ Ya.Context.AdvManager.render({
blockId: "R-A-12294791-1", "blockId": "R-A-12294791-3",
renderTo: "yandex_rtb_R-A-12294791-1", "type": "floorAd",
type: "feed" "platform": "touch"
}); });
Ya.Context.AdvManager.render({
"blockId": "R-A-12294791-4",
"type": "floorAd",
"platform": "desktop"
}); });
Ya.Context.AdvManager.render({
"blockId": "R-A-12294791-5",
"renderTo": "yandex_rtb_R-A-12294791-5"
})
});
})
`; `;
document.body.appendChild(script); document.body.appendChild(script);
};
};
// Рендерим рекламные блоки при загрузке страницы или изменении маршрута // Удаляем элементы при размонтировании компонента
renderAds();
// Удаляем скрипт при размонтировании или смене страницы
return () => { return () => {
const existingScript = document.getElementById('yandex-ad-script'); script.remove();
const existingYandexScript = document.getElementById('yandex-ads-context-script'); adContainer.remove();
if (existingScript) existingScript.remove();
if (existingYandexScript) existingYandexScript.remove();
}; };
}, [location.pathname]); // Следим за изменением пути }, []);
return ( return (
<> <>
@ -69,6 +63,7 @@ export default function CustomLayout(props) {
<script> <script>
window.yaContextCb = window.yaContextCb || []; window.yaContextCb = window.yaContextCb || [];
</script> </script>
<script src="https://yandex.ru/ads/system/context.js" async></script>
</Head> </Head>
<Layout {...props} /> <Layout {...props} />
</> </>