Поддержка RuTube

This commit is contained in:
Valentin Silytuin 2025-04-24 23:13:11 +04:00
parent 924830df6c
commit b934b2ec41
1 changed files with 64 additions and 1 deletions

View File

@ -19,6 +19,7 @@ const getVKPlayer = () => {
const playVideo = (slide) => {
const youtube = slide.querySelector('.advdominion-lg__video_youtube');
const vk = slide.querySelector('.advdominion-lg__video_vk');
const rt = slide.querySelector('.advdominion-lg__video_rt');
if (youtube) {
if (slide.YTPlayer) {
@ -64,17 +65,44 @@ const playVideo = (slide) => {
playVideo(slide);
});
}
} else if (rt) {
const videoId = /embed\/(\w+)/.exec(rt.getAttribute('src'))?.[1];
const checkInterval = setInterval(() => {
if (window.GalleryRTPlayer) {
if (window.GalleryRTPlayer.has(videoId)) {
clearInterval(checkInterval);
rt.contentWindow.postMessage(
JSON.stringify({
type: 'player:play',
data: {},
}),
'*',
);
}
} else {
clearInterval(checkInterval);
}
}, 100);
}
};
const pauseVideo = (slide) => {
const youtube = slide.querySelector('.advdominion-lg__video_youtube');
const vk = slide.querySelector('.advdominion-lg__video_vk');
const rt = slide.querySelector('.advdominion-lg__video_rt');
if (youtube && slide.YTPlayer) {
slide.YTPlayer.pauseVideo();
} else if (vk && slide.VKPlayer) {
slide.VKPlayer.pause();
} else if (rt) {
rt.contentWindow.postMessage(
JSON.stringify({
type: 'player:pause',
data: {},
}),
'*',
);
}
};
@ -106,6 +134,7 @@ const hide = async (duration = 500, easing = 'linear') => {
}).finished;
gallery.remove();
delete window.GalleryRTPlayer;
document.body.style = `
overflow: '';
@ -136,6 +165,7 @@ const init = async (items = [], options = {}, index = 0) => {
const isVideoInGallery = {
yt: false,
vk: false,
rt: false,
};
items = items.map((item) => {
if (/youtube\.com\/watch\?v=/.test(item)) {
@ -168,7 +198,28 @@ const init = async (items = [], options = {}, index = 0) => {
return `
<div class="advdominion-lg__item advdominion-lg__item_video swiper-slide">
<div class="advdominion-lg__video-wrapper">
<iframe class="advdominion-lg__video advdominion-lg__video_vk" src="https://vkvideo.ru/video_ext.php?oid=${videoId[1]}&id=${videoId[2]}&hd=4&js_api=1" allow="autoplay; encrypted-media; fullscreen; picture-in-picture; screen-wake-lock;" allowfullscreen></iframe>
<iframe class="advdominion-lg__video advdominion-lg__video_vk" src="https://vkvideo.ru/video_ext.php?oid=${videoId[1]}&id=${videoId[2]}&hd=4&js_api=1" allow="autoplay; encrypted-media; fullscreen; picture-in-picture; screen-wake-lock;"></iframe>
</div>
</div>
`;
} else if (/rutube\.ru/.test(item)) {
isVideoInGallery.rt = true;
const videoId = /video\/(\w+)/.exec(item);
if (videoId === null) {
return `
<div class="advdominion-lg__item advdominion-lg__item_video swiper-slide">
<div class="advdominion-lg__video-wrapper">
<div class="advdominion-lg__video advdominion-lg__video_rt">
Ошибка при загрузке RuTube-плеера
</div>
</div>
</div>
`;
}
return `
<div class="advdominion-lg__item advdominion-lg__item_video swiper-slide">
<div class="advdominion-lg__video-wrapper">
<iframe class="advdominion-lg__video advdominion-lg__video_rt" src="https://rutube.ru/play/embed/${videoId[1]}" allow="autoplay; encrypted-media; fullscreen; picture-in-picture; screen-wake-lock;"></iframe>
</div>
</div>
`;
@ -229,6 +280,18 @@ const init = async (items = [], options = {}, index = 0) => {
}, 100);
}
if (isVideoInGallery.rt) {
window.GalleryRTPlayer = new Set();
window.addEventListener('message', (event) => {
if (event.origin.includes('rutube.ru') && typeof event.data === 'string') {
const message = JSON.parse(event.data);
if (message.type === 'player:ready') {
window.GalleryRTPlayer.add(message.data.videoId);
}
}
});
}
document.body.insertAdjacentHTML(
'beforeend',
`