|
| 1 | +# Chrome Inspector |
| 2 | + |
| 3 | +A lightweight interface for Chrome Inspector, providing Elements and Styles Panel information via the Chrome DevTools Protocol (CDP). Supports Puppeteer, Playwright, Chrome Extensions, and other CDP clients. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm i chrome-inspector |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```ts |
| 14 | +import { Inspector } from "chrome-inspector"; |
| 15 | + |
| 16 | +// Init backend... |
| 17 | + |
| 18 | +// Puppeteer |
| 19 | +const client = await page.createCDPSession(); |
| 20 | +const inspector = Inspector.fromCDPClient(client); |
| 21 | + |
| 22 | +// Playwright |
| 23 | +const client = await page.context().newCDPSession(page); |
| 24 | +const inspector = Inspector.fromCDPClient(client); |
| 25 | + |
| 26 | +// Chrome Extension |
| 27 | +const target = { tabId: chrome.devtools.inspectedWindow.tabId }; |
| 28 | +await chrome.debugger.attach(target, "1.3"); |
| 29 | +const inspector = Inspector.fromChromeDebugger(chrome.debugger, target.tabId); |
| 30 | + |
| 31 | +// Load a page... |
| 32 | + |
| 33 | +// Inspect element by query selector |
| 34 | +const node = await inspector.inspect("body"); |
| 35 | + |
| 36 | +console.log("Matched Rules:"); |
| 37 | +console.log(JSON.stringify(node.styles, null, 2)); |
| 38 | + |
| 39 | +console.log("Computed Styles:"); |
| 40 | +for (const key of ["background-color", "width", "margin-left"]) { |
| 41 | + console.log(`${key}:`, node.computed[key]); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Full example: |
| 46 | + |
| 47 | +Sample output for [https://example.com](https://example.com) (formatted): |
| 48 | + |
| 49 | +```json |
| 50 | +Styles: |
| 51 | +{ |
| 52 | + "inherited": [], |
| 53 | + "attributes": [], |
| 54 | + "matched": [ |
| 55 | + { |
| 56 | + "allSelectors": ["body"], |
| 57 | + "matchedSelectors": ["body"], |
| 58 | + "properties": [ |
| 59 | + {"name": "display","value": "block","important": false,"applied": true}, |
| 60 | + {"name": "margin","value": "8px","important": false,"applied": false} |
| 61 | + ], |
| 62 | + "origin": "user-agent" |
| 63 | + }, |
| 64 | + { |
| 65 | + "allSelectors": ["body"], |
| 66 | + "matchedSelectors": ["body"], |
| 67 | + "properties": [ |
| 68 | + {"name": "background","value": "#eee","important": false,"applied": true}, |
| 69 | + {"name": "width","value": "60vw","important": false,"applied": true}, |
| 70 | + {"name": "margin","value": "15vh auto","important": false,"applied": true}, |
| 71 | + {"name": "font-family","value": "system-ui,sans-serif","important": false,"applied": false} |
| 72 | + ], |
| 73 | + "origin": "regular", |
| 74 | + "cssText": "background:#eee;width:60vw;margin:15vh auto;font-family:system-ui,sans-serif" |
| 75 | + } |
| 76 | + ], |
| 77 | + "pseudoElements": [], |
| 78 | + "inline": [] |
| 79 | +} |
| 80 | +Computed: |
| 81 | +background-color: rgb(238, 238, 238) |
| 82 | +width: 480px |
| 83 | +margin-left: 160px |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +## APIs |
| 88 | + |
| 89 | +- The returned `Node` is a CDP [DOM.Node](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#type-Node) object containing DOM information. Loosely typed for compatibility. |
| 90 | + |
| 91 | +- The rules are ordered by specificity from lowest to highest. `apply: true` marks final applied properties. For more details check `@devtoolcss/parser` |
| 92 | + |
| 93 | +- The Document is native DOM in browser and JSDOM in node. To use a specific document you can pass it in. It always returns a new document instance created from `document.implementation.createHTMLDocument()`. |
0 commit comments