javascript:(function() {
const AMBIENT_NAMESPACE = 'ytAmbient'; // Unique namespace for global properties
const AMBIENT_CONTAINER_ID = AMBIENT_NAMESPACE + 'Container';
const AMBIENT_LAYER_CLASS = AMBIENT_NAMESPACE + 'Layer';
const AMBIENT_CLONED_VIDEO_CLASS = AMBIENT_NAMESPACE + 'ClonedVideo';
const AMBIENT_STYLES_ID = AMBIENT_NAMESPACE + 'Styles';
// Global state for activation status and elements
if (window[AMBIENT_NAMESPACE] === undefined) {
window[AMBIENT_NAMESPACE] = {
isActive: false,
mainVideo: null,
mainPlayer: null,
ambientContainer: null,
clonedVideos: [],
syncIntervals: [], // Store intervals for cleanup
originalPlayerStyles: new Map(), // Store original styles to revert
originalVideoStyles: new Map()
};
}
const state = window[AMBIENT_NAMESPACE];
const applyStyles = (element, styles) => {
for (const prop in styles) {
element.style[prop] = styles[prop];
}
};
const getPlayerAndVideo = () => {
let player = document.getElementById('movie_player');
if (!player) {
console.warn('YouTube ambient effect: Could not find #movie_player.');
return [null, null];
}
let video = player.querySelector('video.html5-main-video');
if (!video) {
video = player.querySelector('video'); // Fallback
}
if (!video) {
console.warn('YouTube ambient effect: Could not find main video element.');
return [null, null];
}
return [player, video];
};
const addAmbientStyles = () => {
if (document.getElementById(AMBIENT_STYLES_ID)) return;
const style = document.createElement('style');
style.id = AMBIENT_STYLES_ID;
style.textContent = `
#${AMBIENT_CONTAINER_ID} {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
pointer-events: none;
z-index: -1; /* Ensures it's behind the main video */
display: flex; /* For centering content */
justify-content: center;
align-items: center;
contain: layout style paint; /* Optimize rendering */
}
.${AMBIENT_LAYER_CLASS} {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
pointer-events: none;
overflow: hidden; /* Clips scaled video clones */
will-change: transform, filter, opacity; /* Performance hint */
contain: layout style paint;
}
.${AMBIENT_CLONED_VIDEO_CLASS} {
display: block;
object-fit: cover; /* Stretches video to fill layer */
transform-origin: center center;
width: 100%;
height: 100%;
pointer-events: none;
}
`;
document.head.appendChild(style);
};
const removeAmbientStyles = () => {
const style = document.getElementById(AMBIENT_STYLES_ID);
if (style) {
style.remove();
}
};
const activateEffect = () => {
if (state.isActive) return;
[state.mainPlayer, state.mainVideo] = getPlayerAndVideo();
if (!state.mainPlayer || !state.mainVideo) {
console.error('Ambient effect activation failed: Player or video not found.');
return;
}
addAmbientStyles();
state.ambientContainer = document.createElement('div');
state.ambientContainer.id = AMBIENT_CONTAINER_ID;
// Prepend to mainPlayer so it appears behind existing content
state.mainPlayer.prepend(state.ambientContainer);
const numLayers = 3; // Number of blurred layers for smoother glow
const baseBlur = 40; // Base blur in pixels
const baseScale = 2.0; // How much larger the first cloned video is
// Store and set player background to transparent
state.originalPlayerStyles.set('backgroundColor', state.mainPlayer.style.backgroundColor);
state.mainPlayer.style.backgroundColor = 'transparent';
// Store and set main video object-fit if it's explicitly set via style
state.originalVideoStyles.set('objectFit', state.mainVideo.style.objectFit);
// We let the main video keep its object-fit: contain (which creates black bars)
// The cloned videos will use object-fit: cover to fill the background.
for (let i = 0; i < numLayers; i++) {
const layer = document.createElement('div');
layer.className = AMBIENT_LAYER_CLASS;
const clonedVideo = state.mainVideo.cloneNode(true); // Deep clone
clonedVideo.removeAttribute('id'); // Avoid ID conflicts
clonedVideo.className = AMBIENT_CLONED_VIDEO_CLASS; // Apply our class
// Apply specific styles for each cloned video layer
applyStyles(clonedVideo, {
filter: `blur(${baseBlur * (i + 1)}px)`,
opacity: `${0.8 - (i * 0.2)}`, // Fade out outer layers
transform: `scale(${baseScale + (i * 0.2)})`, // Scale more for outer layers
});
// Synchronize cloned video with main video
const syncVideo = () => {
// Ensure correct playback state
if (!state.mainVideo.paused && clonedVideo.paused) {
clonedVideo.play().catch(e => console.log('Cloned video play failed:', e)); // Handle autoplay policies
} else if (state.mainVideo.paused && !clonedVideo.paused) {
clonedVideo.pause();
}
// Sync current time if they drift significantly
if (Math.abs(state.mainVideo.currentTime - clonedVideo.currentTime) > 0.5) {
clonedVideo.currentTime = state.mainVideo.currentTime;
}
};
// Initial sync
syncVideo();
// Add event listeners for direct sync
state.mainVideo.addEventListener('play', syncVideo);
state.mainVideo.addEventListener('pause', syncVideo);
state.mainVideo.addEventListener('seeked', syncVideo);
// Periodic sync for robustness
const intervalId = setInterval(syncVideo, 1000); // Check and sync every second
state.syncIntervals.push(intervalId);
layer.appendChild(clonedVideo);
state.ambientContainer.appendChild(layer);
state.clonedVideos.push(clonedVideo);
}
state.isActive = true;
console.log('YouTube ambient effect activated.');
};
const deactivateEffect = () => {
if (!state.isActive) return;
// Clear all synchronization intervals
state.syncIntervals.forEach(intervalId => clearInterval(intervalId));
state.syncIntervals = [];
// Remove the ambient container and all its children
if (state.ambientContainer && state.ambientContainer.parentNode) {
state.ambientContainer.parentNode.removeChild(state.ambientContainer);
state.ambientContainer = null;
}
// Restore original player and video styles
if (state.mainPlayer) {
applyStyles(state.mainPlayer, Object.fromEntries(state.originalPlayerStyles));
state.originalPlayerStyles.clear();
}
if (state.mainVideo) {
applyStyles(state.mainVideo, Object.fromEntries(state.originalVideoStyles));
state.originalVideoStyles.clear();
}
removeAmbientStyles();
// Reset state variables
state.mainVideo = null;
state.mainPlayer = null;
state.clonedVideos = [];
state.isActive = false;
console.log('YouTube ambient effect deactivated.');
};
// Main toggle logic
if (state.isActive) {
deactivateEffect();
} else {
activateEffect();
}
})();