Is there a way to run javascript before rendering in cdt?
page.addScriptToEvaluateOnLoad("(function() { alert("testing")})()") page.addScriptToEvaluateOnLoad executed on page load and not after the dom is completed
Is there a way to run javascript before rendering in cdt?
page.addScriptToEvaluateOnLoad("(function() { alert("testing")})()") page.addScriptToEvaluateOnLoad executed on page load and not after the dom is completed
Need to make use of com.github.kklisura.cdt.protocol.commands.Runtime In the below example method, I will remove the sticky, fixed content from a webpage before converting it into a pdf
private void convertToPdf(Long masterSourceId, String url, String outputFilePath, Double screenshotLoadTime) {
ExecutorService mainExecutor = Executors.newSingleThreadExecutor();
try (ChromeLauncher launcher = new ChromeLauncher()) {
Callable<Boolean> pdfTask = () -> {
Map<String, Object> additionalChromeArguments = new HashMap<>();
additionalChromeArguments.put("no-sandbox", true);
ChromeArguments chromeArguments = ChromeArguments.builder().noFirstRun().noDefaultBrowserCheck()
.disableBackgroundNetworking().disableBackgroundTimerThrottling()
.disableClientSidePhishingDetection().disableDefaultApps().disableExtensions()
.disableHangMonitor().disablePopupBlocking().disablePromptOnRepost().disableSync()
.disableTranslate().metricsRecordingOnly().safebrowsingDisableAutoUpdate().headless(true)
.disableGpu(true).hideScrollbars(true).muteAudio(true)
.additionalArguments(additionalChromeArguments).build();
additionalChromeArguments.put("no-sandbox", true);
ChromeService chromeService = launcher.launch(chromeArguments);
ChromeTab tab = chromeService.createTab();
ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
devToolsService.getEmulation().setScriptExecutionDisabled(false);
devToolsService.getEmulation().setDocumentCookieDisabled(true);
Page page = devToolsService.getPage();
page.enable();
page.navigate(url);
page.onLoadEventFired(loadEventFired -> {
/*
* This below script will make sure that any floating, sticky cookie popups in
* the web Page will not cover the content when the pdf is being taken from the
* HTML THe script will return the dom elements after javascript has modified
* the values
*/
Runtime runtime = devToolsService.getRuntime();
runtime.evaluate(
"(function(){x=document.querySelectorAll('*');for(i=0;i<x.length;i++){elementStyle=getComputedStyle(x[i]);if(elementStyle.position=='fixed'||elementStyle.position=='sticky'){x[i].remove();}}}())");
runtime.evaluate(
"(function() {l = document.getElementsByTagName(\"a\"); for (var i =0; i<l.length; i++) {l[i].href = \"\"; } }())");
dump(masterSourceId, outputFilePath, page.printToPDF(false, false, true, null, 11.7, 16.5, null,
null, null, null, null, null, null, null, true, null).getData());
devToolsService.close();
});
devToolsService.waitUntilClosed();
chromeService.closeTab(tab);
return true;
};
Future<Boolean> exeFuture = mainExecutor.submit(pdfTask);
exeFuture.get(10, TimeUnit.MINUTES);
} catch (Exception ex) {
log.error(masterSourceId + " : ERROR OCCURED IN HTML TO PDF CHROME CONVERTER : " + ex.getMessage());
} finally {
mainExecutor.shutdownNow();
}
}