Skip to content

Commit 5c13a6f

Browse files
committed
0 parents  commit 5c13a6f

13 files changed

Lines changed: 2459 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Bill Tsui
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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()`.

package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "chrome-inspector",
3+
"version": "0.0.0",
4+
"description": "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.",
5+
"license": "MIT",
6+
"files": [
7+
"dist/",
8+
"README.md"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "git@github.com:devtoolcss/chrome-inspector.git"
13+
},
14+
"homepage": "https://github.com/devtoolcss/chrome-inspector#readme",
15+
"keywords": [
16+
"chrome",
17+
"inspector",
18+
"devtools",
19+
"devtools-protocol",
20+
"puppeteer",
21+
"playwright",
22+
"chrome-extension"
23+
],
24+
"publishConfig": {
25+
"provenance": true
26+
},
27+
"type": "module",
28+
"exports": {
29+
".": {
30+
"import": "./dist/index.js",
31+
"types": "./dist/index.d.ts"
32+
}
33+
},
34+
"module": "dist/index.js",
35+
"scripts": {
36+
"build": "tsc",
37+
"prepare": "pnpm build"
38+
},
39+
"dependencies": {
40+
"@devtoolcss/parser": "^1.0.0",
41+
"jsdom": "^27.0.1"
42+
},
43+
"devDependencies": {
44+
"@types/chrome": "^0.1.24",
45+
"@types/jsdom": "^21.1.7",
46+
"devtools-protocol": "^0.0.1532728",
47+
"puppeteer-core": "^24.25.0",
48+
"typescript": "^5.9.3"
49+
}
50+
}

0 commit comments

Comments
 (0)