@@ -270,13 +270,30 @@ const domUtils = {
270270 // Batch DOM reads and writes
271271 batchDOMOperations ( reads = [ ] , writes = [ ] ) {
272272 return new Promise ( resolve => {
273- requestIdleCallback ( ( ) => {
273+ // Fallback for browsers without requestIdleCallback
274+ const idleCallback = window . requestIdleCallback ||
275+ ( ( cb ) => setTimeout ( cb , 16 ) ) ;
276+
277+ idleCallback ( ( ) => {
274278 // Batch all reads first
275- const readResults = reads . map ( read => read ( ) ) ;
279+ const readResults = reads . map ( read => {
280+ try {
281+ return read ( ) ;
282+ } catch ( error ) {
283+ console . warn ( 'DOM read operation failed:' , error ) ;
284+ return null ;
285+ }
286+ } ) ;
276287
277288 // Then batch all writes
278289 requestAnimationFrame ( ( ) => {
279- writes . forEach ( write => write ( ) ) ;
290+ writes . forEach ( write => {
291+ try {
292+ write ( ) ;
293+ } catch ( error ) {
294+ console . warn ( 'DOM write operation failed:' , error ) ;
295+ }
296+ } ) ;
280297 resolve ( readResults ) ;
281298 } ) ;
282299 } ) ;
@@ -287,59 +304,39 @@ const domUtils = {
287304 isElementVisible ( element , threshold = 0 ) {
288305 if ( ! element || ! element . getBoundingClientRect ) return false ;
289306
290- const rect = element . getBoundingClientRect ( ) ;
291- const windowHeight = window . innerHeight || document . documentElement . clientHeight ;
292- const windowWidth = window . innerWidth || document . documentElement . clientWidth ;
293-
294- return (
295- rect . bottom >= threshold &&
296- rect . right >= threshold &&
297- rect . top <= windowHeight - threshold &&
298- rect . left <= windowWidth - threshold
299- ) ;
307+ try {
308+ const rect = element . getBoundingClientRect ( ) ;
309+ const windowHeight = window . innerHeight || document . documentElement . clientHeight ;
310+ const windowWidth = window . innerWidth || document . documentElement . clientWidth ;
311+
312+ return (
313+ rect . bottom >= threshold &&
314+ rect . right >= threshold &&
315+ rect . top <= windowHeight - threshold &&
316+ rect . left <= windowWidth - threshold
317+ ) ;
318+ } catch ( error ) {
319+ console . warn ( 'Visibility check failed:' , error ) ;
320+ return false ;
321+ }
300322 } ,
301323
302- // Intersection Observer with idle callback
303- createIdleObserver ( callback , options = { } ) {
304- const idleOptions = {
305- threshold : options . threshold || 0.1 ,
306- rootMargin : options . rootMargin || '50px'
307- } ;
324+ // Intersection Observer with idle callback and fallback
325+ createIntersectionObserver ( callback , options = { } ) {
326+ if ( ! window . IntersectionObserver ) {
327+ // Fallback for older browsers
328+ console . warn ( 'IntersectionObserver not supported, using fallback' ) ;
329+ return {
330+ observe : ( ) => { } ,
331+ unobserve : ( ) => { } ,
332+ disconnect : ( ) => { }
333+ } ;
334+ }
308335
309- return new IntersectionObserver ( ( entries ) => {
310- requestIdleCallback ( ( ) => {
311- callback ( entries ) ;
312- } ) ;
313- } , idleOptions ) ;
314- } ,
315-
316- // Wait for element to exist
317- waitForElement ( selector , timeout = 5000 ) {
318- return new Promise ( ( resolve , reject ) => {
319- const element = document . querySelector ( selector ) ;
320- if ( element ) {
321- resolve ( element ) ;
322- return ;
323- }
324-
325- const observer = new MutationObserver ( ( ) => {
326- const element = document . querySelector ( selector ) ;
327- if ( element ) {
328- observer . disconnect ( ) ;
329- clearTimeout ( timeoutId ) ;
330- resolve ( element ) ;
331- }
332- } ) ;
333-
334- const timeoutId = setTimeout ( ( ) => {
335- observer . disconnect ( ) ;
336- reject ( new Error ( `Element ${ selector } not found within ${ timeout } ms` ) ) ;
337- } , timeout ) ;
338-
339- observer . observe ( document . body , {
340- childList : true ,
341- subtree : true
342- } ) ;
336+ return new IntersectionObserver ( callback , {
337+ threshold : 0.1 ,
338+ rootMargin : '50px' ,
339+ ...options
343340 } ) ;
344341 }
345342} ;
@@ -474,3 +471,6 @@ if (typeof performance !== 'undefined') {
474471if ( typeof module !== 'undefined' && module . exports ) {
475472 module . exports = window . PKBUtils ;
476473}
474+ if ( typeof module !== 'undefined' && module . exports ) {
475+ module . exports = window . PKBUtils ;
476+ }
0 commit comments