@@ -178,6 +178,15 @@ class PomodoroTimer extends DataroomElement {
178178 }
179179
180180 loadRandomVideo ( container ) {
181+ this . loadVideoWithFallback ( container , 0 ) ;
182+ }
183+
184+ loadVideoWithFallback ( container , attemptCount ) {
185+ if ( attemptCount >= 5 ) {
186+ container . innerHTML = '<div class="video-placeholder">Unable to load video after multiple attempts</div>' ;
187+ return ;
188+ }
189+
181190 const playlist = this . getRandomPlaylist ( ) ;
182191 if ( ! playlist ) {
183192 container . innerHTML = '<div class="video-placeholder">Loading relaxation video...</div>' ;
@@ -198,6 +207,32 @@ class PomodoroTimer extends DataroomElement {
198207 iframe . allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' ;
199208 iframe . allowFullscreen = true ;
200209 iframe . className = 'youtube-player' ;
210+
211+ // Handle iframe load errors (age restrictions, unavailable videos)
212+ iframe . addEventListener ( 'error' , ( ) => {
213+ console . log ( 'Video failed to load, trying another...' ) ;
214+ this . loadVideoWithFallback ( container , attemptCount + 1 ) ;
215+ } ) ;
216+
217+ // Check for age restriction after load
218+ iframe . addEventListener ( 'load' , ( ) => {
219+ setTimeout ( ( ) => {
220+ try {
221+ // If iframe content is blocked or shows age restriction, try another video
222+ const iframeDoc = iframe . contentDocument || iframe . contentWindow . document ;
223+ if ( ! iframeDoc || iframeDoc . title . includes ( 'restricted' ) || iframeDoc . title . includes ( 'unavailable' ) ) {
224+ console . log ( 'Video appears to be restricted, trying another...' ) ;
225+ this . loadVideoWithFallback ( container , attemptCount + 1 ) ;
226+ return ;
227+ }
228+ } catch ( e ) {
229+ // Cross-origin restrictions prevent access, assume video is working
230+ console . log ( 'Video loaded successfully' ) ;
231+ }
232+ } , 2000 ) ;
233+ } ) ;
234+
235+ container . innerHTML = '' ;
201236 container . appendChild ( iframe ) ;
202237 }
203238
0 commit comments