@@ -326,46 +326,84 @@ export const InteractiveBlockComponent = ({
326
326
const placeholderLinkRef = useRef < HTMLAnchorElement > ( null ) ;
327
327
const [ loaded , setLoaded ] = useState ( false ) ;
328
328
const { darkModeAvailable } = useConfig ( ) ;
329
+
330
+ // Define some one-time flags
331
+ const isDatawrapperGraphic =
332
+ url && url . includes ( 'interactive.guim.co.uk/datawrapper' )
333
+ ? true
334
+ : false ;
335
+
336
+ const isUploaderEmbedPath =
337
+ url && url . includes ( 'interactive.guim.co.uk/uploader/embed/' )
338
+ ? true
339
+ : false ;
340
+
341
+ const scriptUrlIsBoot =
342
+ scriptUrl &&
343
+ scriptUrl . includes (
344
+ 'interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js' ,
345
+ )
346
+ ? true
347
+ : false ;
348
+
329
349
useOnce ( ( ) => {
330
350
// We've brought the behavior from boot.js into this file to avoid loading 2 extra scripts
331
- if (
332
- scriptUrl ===
333
- 'https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js' &&
334
- url &&
335
- placeholderLinkRef . current
336
- ) {
351
+
352
+ // Define additional one-time flags - these depend on window/document objects
353
+ const isRunningInWebEnvironment =
354
+ ! document . querySelector ( '.ios' ) &&
355
+ ! document . querySelector ( '.android' )
356
+ ? true
357
+ : false ;
358
+
359
+ const prefersDarkScheme = window . matchMedia (
360
+ '(prefers-color-scheme: dark)' ,
361
+ ) . matches ;
362
+ const requiresDarkMode =
363
+ darkModeAvailable && prefersDarkScheme ? true : false ;
364
+
365
+ if ( url && scriptUrlIsBoot && placeholderLinkRef . current ) {
366
+ // Prepare for graphic url dynamic updates
367
+ const graphicUrl = new URL ( url ) ;
368
+
369
+ // Begin creating new iframe element
337
370
const iframe = document . createElement ( 'iframe' ) ;
338
371
iframe . style . width = '100%' ;
339
372
iframe . style . border = 'none' ;
340
373
iframe . height = decideHeight ( role ) . toString ( ) ;
341
374
iframe . title = caption ?? alt ?? 'Interactive Content' ;
342
- if ( url . startsWith ( 'http:' ) ) {
343
- iframe . src = url . replace ( 'http:' , 'https:' ) ;
344
- } else {
345
- iframe . src = url ;
346
- }
347
375
348
- // Datawrapper-specific fix to suppress scrollbars appearing
349
- if ( url . includes ( 'datawrapper' ) ) {
376
+ // Fix for Datawrapper graphic
377
+ if ( isDatawrapperGraphic ) {
350
378
iframe . scrolling = 'no' ;
351
- // Turn off dark mode for Datawrapper embeds on web
352
- // This should be removed if/when dark mode is implements on the website
353
- if (
354
- ! document . querySelector ( '.ios' ) &&
355
- ! document . querySelector ( '.android' )
356
- ) {
357
- const prefersDarkScheme = window . matchMedia (
358
- '(prefers-color-scheme: dark)' ,
359
- ) . matches ;
360
- const darkMode = darkModeAvailable && prefersDarkScheme ;
361
- if ( ! darkMode ) {
362
- iframe . src +=
363
- ( iframe . src . includes ( '?' ) ? '&' : '?' ) +
364
- 'dark=false' ;
379
+ }
380
+
381
+ // Fix darkmode for web environment
382
+ if ( isRunningInWebEnvironment && ! requiresDarkMode ) {
383
+ if ( isDatawrapperGraphic || isUploaderEmbedPath ) {
384
+ // Add the 'dark=false' search param
385
+ if ( graphicUrl . search . length ) {
386
+ graphicUrl . search += '&dark=false' ;
387
+ } else {
388
+ // Embed URLs without a trailing slash are redirected and the
389
+ // search param is lost so we need to add it to the pathname
390
+ const hasTrailingSlash = graphicUrl . pathname . endsWith (
391
+ '/' ,
392
+ )
393
+ ? true
394
+ : false ;
395
+ graphicUrl . pathname += hasTrailingSlash ? '' : '/' ;
396
+ graphicUrl . search += '?dark=false' ;
365
397
}
366
398
}
367
399
}
368
400
401
+ // Always serve graphic over https, not http
402
+ graphicUrl . protocol = 'https:' ;
403
+
404
+ // Finalise new iframe element
405
+ iframe . src = graphicUrl . href ;
406
+
369
407
setupWindowListeners ( iframe ) ;
370
408
371
409
wrapperRef . current ?. prepend ( iframe ) ;
@@ -397,13 +435,6 @@ export const InteractiveBlockComponent = ({
397
435
}
398
436
} , [ loaded ] ) ;
399
437
400
- const isDatawrapperGraphic =
401
- url == undefined
402
- ? false
403
- : / ^ h t t p s ? : \/ \/ i n t e r a c t i v e \. g u i m \. c o \. u k \/ d a t a w r a p p e r ( - t e s t ) ? \/ e m b e d / . test (
404
- url ,
405
- ) ;
406
-
407
438
return (
408
439
< >
409
440
< figure
0 commit comments