import { i18n } from '@kbn/i18n' ; import fs from 'fs' ; import { promisify } from 'util' ; import apm from 'elastic-apm-node' ; import type { Logger } from 'src/core/server' ; import type { HeadlessChromiumDriver } from '../browsers' ; import { Layout } from '../layouts' ; import { CONTEXT_INJECTCSS } from './constants' ; const fsp = { readFile : promisify ( fs . readFile ) } ; export const injectCustomCss = async ( browser , logger , layout ) : Promise < void > => { const span = apm . startSpan ( 'inject_css' , 'correction' ) ; logger . debug ( i18n . translate ( 'xpack.screenshotting.screencapture.injectingCss' , { defaultMessage : 'injecting custom css' , } ) ) ; const filePath = layout . getCssOverridesPath ( ) ; if ( ! filePath ) { return ; } const buffer = await fsp . readFile ( filePath ) ; try { await browser . evaluate ( { fn : ( css ) => { const node = document . createElement ( 'style' ) ; node . type = 'text/css' ; node . innerHTML = css ; document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( node ) ; } , args : [ buffer . toString ( ) ] , } , { context : CONTEXT_INJECTCSS } , logger ) ; } catch ( err ) { logger . error ( err ) ; throw new Error ( i18n . translate ( 'xpack.screenshotting.screencapture.injectCss' , { defaultMessage : `An error occurred when trying to update Kibana CSS for reporting. {error}` , values : { error : err } , } ) ) ; } span ?. end ( ) ; } ;