1. Introduction: Turning Your Browser into a Puppet
cdpred is like an invisible earpiece, plugged into the back of Chrome, listening to its heartbeat in real-time.
With single-page routing, asynchronous interfaces, and lazy loading on scroll, it can deliver JSON notes to you.
Make yourself a cup of water, read it in 7 minutes, and you can make the webpage “speak” on its own.
2. Let the Browser Speak First
To make Chrome your insider, you need to send it a secret signal. The command line <span>--remote-debugging-port=9222</span> is the key to the door. Once cdpred detects the port, it immediately shakes hands, and the browser becomes a “puppet”.
import cdpred, asyncio
async def main():
browser = await cdpred.connect("127.0.0.1", 9222)
page = await browser.page()
await page.goto("https://httpbin.org/delay/2")
print("Title:", await page.title())
asyncio.run(main())
Run it, and the browser pops up as if it has been “possessed”.
3. Capturing Packets is Like Petting a Cat
XHR is hidden in the Network tab, and cdpred helps you pluck them one by one. Listen for <span>responseReceived</span>, and the URL and status line up neatly.
async def log_network(page):
async with page.listen("Network.responseReceived") as L:
async for ev in L:
r = ev["response"]
print(r["status"], r["url"])
Refresh the page, and swoosh— all the API addresses are caught.
4. Taking Screenshots to Avoid Blame
Does your boss want “seeing is believing”?<span>Page.captureScreenshot</span> captures it all, saving it directly as a PNG.
async def snap(page, name="demo.png"):
img = await page.send("Page.captureScreenshot")
with open(name, "wb") as f:
f.write(img.data)
Just two lines of code, and the entire page’s pixels are packed away.
5. Taking the Page’s Pulse
Performance metrics are hidden in <span>Performance.metrics</span>. Capture <span>firstPaint</span>, and the white screen time is immediately revealed.
async def perf(page):
await page.send("Performance.enable")
m = await page.send("Performance.getMetrics")
for i in m["metrics"]:
if i["name"] == "firstPaint":
print("First Paint:", i["value"] * 1000, "ms")
Numbers pop up, and you have a target for optimizing KPIs.
6. Injecting Hooks to Capture Tokens
<span>addScriptToEvaluateOnNewDocument</span> injects once and is useful for life. It pulls out <span>localStorage.token</span> for external use.
async def hook(page):
await page.send("Page.addScriptToEvaluateOnNewDocument",
{"source": 'window._t=localStorage.getItem("token")'})
tok = await page.eval("window._t")
print("Token:", tok)
The script stays resident, not lost on refresh, keeping the login state stable.
7. Advantages and Disadvantages
It is lighter than Playwright, with zero driver installation.
It is simpler than PyChromeDevTools, natively using asyncio.
Disadvantages: Documentation relies on source code, and upgrades must follow Chromium.
It is recommended to lock the Chrome version and write a version guard in the code.
8. Conclusion and Discussion
Creating a backdoor for the browser is that simple.
Go ahead and try it; share your first paint time and the interesting screenshots in the comments, and let’s see who has the slowest!
Recommended Reading:
- • lookfor, an extremely flexible Python library!
- • lpsim, a clearly visible Python library!
- • fnc, a core Python library!
- • kevinsr, a Python whiz!