|
| 1 | +package com.github.kklisura.cdt.examples; |
| 2 | + |
| 3 | +import com.github.kklisura.cdt.launch.ChromeLauncher; |
| 4 | +import com.github.kklisura.cdt.protocol.commands.*; |
| 5 | +import com.github.kklisura.cdt.protocol.types.dom.RGBA; |
| 6 | +import com.github.kklisura.cdt.protocol.types.overlay.HighlightConfig; |
| 7 | +import com.github.kklisura.cdt.services.ChromeDevToolsService; |
| 8 | +import com.github.kklisura.cdt.services.ChromeService; |
| 9 | +import com.github.kklisura.cdt.services.types.ChromeTab; |
| 10 | + |
| 11 | +/** |
| 12 | + * Highlights an elements same way the Chrome inspector does. |
| 13 | + * |
| 14 | + * @author Kenan Klisura |
| 15 | + */ |
| 16 | +public class HighlightElementExample { |
| 17 | + private static void highlightNode( |
| 18 | + ChromeDevToolsService devToolsService, HighlightConfig highlightConfig, String selector) { |
| 19 | + final DOM dom = devToolsService.getDOM(); |
| 20 | + final Overlay overlay = devToolsService.getOverlay(); |
| 21 | + |
| 22 | + final Integer nodeId = |
| 23 | + dom.querySelector(devToolsService.getDOM().getDocument().getNodeId(), selector); |
| 24 | + overlay.highlightNode(highlightConfig, nodeId, null, null, null); |
| 25 | + } |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + // Create chrome launcher. |
| 29 | + final ChromeLauncher launcher = new ChromeLauncher(); |
| 30 | + |
| 31 | + // Launch chrome either as headless (true) or regular (false). |
| 32 | + final ChromeService chromeService = launcher.launch(false); |
| 33 | + |
| 34 | + // Create empty tab ie about:blank. |
| 35 | + final ChromeTab tab = chromeService.createTab(); |
| 36 | + |
| 37 | + // Get DevTools service to this tab |
| 38 | + final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab); |
| 39 | + |
| 40 | + // Get individual commands |
| 41 | + final Page page = devToolsService.getPage(); |
| 42 | + final Network network = devToolsService.getNetwork(); |
| 43 | + final Performance performance = devToolsService.getPerformance(); |
| 44 | + |
| 45 | + devToolsService.getDOM().enable(); |
| 46 | + devToolsService.getOverlay().enable(); |
| 47 | + |
| 48 | + performance.enable(); |
| 49 | + network.onLoadingFinished( |
| 50 | + event -> { |
| 51 | + HighlightConfig highlightConfig = new HighlightConfig(); |
| 52 | + |
| 53 | + highlightConfig.setBorderColor(rgba(255, 229, 153, 0.66)); |
| 54 | + highlightConfig.setContentColor(rgba(111, 168, 220, 0.66)); |
| 55 | + highlightConfig.setCssGridColor(rgb(75, 0, 130)); |
| 56 | + highlightConfig.setEventTargetColor(rgba(255, 196, 196, 0.66)); |
| 57 | + highlightConfig.setMarginColor(rgba(246, 178, 107, 0.66)); |
| 58 | + highlightConfig.setPaddingColor(rgba(147, 196, 125, 0.55)); |
| 59 | + highlightConfig.setShapeColor(rgba(96, 82, 117, 0.8)); |
| 60 | + highlightConfig.setShapeMarginColor(rgba(96, 82, 127, 0.6)); |
| 61 | + |
| 62 | + highlightConfig.setShowExtensionLines(true); |
| 63 | + highlightConfig.setShowInfo(true); |
| 64 | + highlightConfig.setShowRulers(true); |
| 65 | + highlightConfig.setShowStyles(false); |
| 66 | + |
| 67 | + highlightNode(devToolsService, highlightConfig, "h1.h000-mktg"); |
| 68 | + |
| 69 | + sleep(); |
| 70 | + }); |
| 71 | + |
| 72 | + // Enable network events. |
| 73 | + network.enable(); |
| 74 | + |
| 75 | + // Navigate to github.com. |
| 76 | + page.navigate("https://www.github.com"); |
| 77 | + |
| 78 | + devToolsService.waitUntilClosed(); |
| 79 | + } |
| 80 | + |
| 81 | + private static void sleep() { |
| 82 | + try { |
| 83 | + Thread.sleep(100000); |
| 84 | + } catch (InterruptedException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static RGBA rgba(int r, int g, int b, double a) { |
| 90 | + RGBA result = new RGBA(); |
| 91 | + result.setR(r); |
| 92 | + result.setG(g); |
| 93 | + result.setB(b); |
| 94 | + result.setA(a); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + private static RGBA rgb(int r, int g, int b) { |
| 99 | + RGBA result = new RGBA(); |
| 100 | + result.setR(r); |
| 101 | + result.setG(g); |
| 102 | + result.setB(b); |
| 103 | + return result; |
| 104 | + } |
| 105 | +} |
0 commit comments