GithubHelp home page GithubHelp logo

kaliiiiiiiiii-vinyzu / cdp-patches Goto Github PK

View Code? Open in Web Editor NEW
53.0 5.0 2.0 251 KB

Patching CDP (Chrome DevTools Protocol) leaks on OS level. Easy to use with Playwright, Selenium, and other web automation tools.

Home Page: https://vinyzu.gitbook.io/cdp-patches-documentation

License: GNU General Public License v3.0

JavaScript 1.32% Python 95.13% HTML 3.55%
automation botting browser cdp chrome chromium leak leaks linux patch

cdp-patches's Introduction

CDP-Patches v1.0

PyPI PyPI

Install it from PyPI

pip install cdp-patches
Or for Full Linting

(Includes: playwright, botright, selenium, selenium_driverless)

pip install cdp-patches[automation_linting]

Leak Patches

Input Package

Concept: Input Domain Leaks

Bypass CDP Leaks in Input domains

Brotector Banner

For an interaction event e, the page coordinates won't ever equal the screen coordinates, unless Chrome is in fullscreen. However, all CDP input commands just set it the same by default (see crbug#1477537).

var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX)
if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen
    is_bot = false
}

Furthermore, CDP can't dispatch CoalescedEvent's (demo).

As we don't want to patch Chromium itsself, let's just dispatch this event at OS-level!


Usage

from cdp_patches.input import SyncInput

sync_input = SyncInput(pid=pid)
# Or
sync_input = SyncInput(browser=browser)

# Dispatch Inputs
sync_input.click("left", 100, 100)  # Left click at (100, 100)
sync_input.double_click("left", 100, 100)  # Left double-click at (100, 100)
sync_input.down("left", 100, 100)  # Left mouse button down at (100, 100)
sync_input.up("left", 100, 100)  # Left mouse button up at (100, 100)
sync_input.move(100, 100)  # Move mouse to (100, 100)
sync_input.scroll("down", 10)  # Scroll down by 10 lines
sync_input.type("Hello World!")  # Type "Hello World!"

Async Usage

import asyncio

from cdp_patches.input import AsyncInput

async def main():
    async_input = await AsyncInput(pid=pid)
    # Or
    async_input = await AsyncInput(browser=browser)
    
    # Dispatch Inputs
    await async_input.click("left", 100, 100)  # Left click at (100, 100)
    await async_input.double_click("left", 100, 100)  # Left double-click at (100, 100)
    await async_input.down("left", 100, 100)  # Left mouse button down at (100, 100)
    await async_input.up("left", 100, 100)  # Left mouse button up at (100, 100)
    await async_input.move(100, 100)  # Move mouse to (100, 100)
    await async_input.scroll("down", 10)  # Scroll down by 10 lines
    await async_input.type("Hello World!")  # Type "Hello World!"

if __name__ == '__main__':
    asyncio.run(main())

TODO

  • Improve mouse movement timings.
  • Implement extensive testing.

Owner: Vinyzu

Co-Maintainer: Kaliiiiiiiiii

Important

By the nature of OS-level events (which can only impact actionable windows), this package can only be used with headful browsers.

Warning

Pressing SHIFT or CAPSLOCK manually on Windows affects input.type(text) as well.

Warning

Because Chrome does not recognize Input Events to specific tabs, these methods can only be used on the active tab. Chrome Tabs do have their own process with a process id (pid), but these can not be controlled using Input Events as they´re just engines.

Read the Documentation


Development

Read the CONTRIBUTING.md file.


Copyright and License

© Vinyzu

GNU GPL

(Commercial Usage is allowed, but source, license and copyright has to made available. Botright does not provide and Liability or Warranty)


Authors

Vinyzu, Kaliiiiiiiiii

cdp-patches's People

Contributors

botright avatar kaliiiiiiiiii avatar vinyzu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cdp-patches's Issues

[Feature request] add remote server support

Add some cdp_patches.Server().start() and cdp_patches.Client().connect() functionality to make it easier integrable on an remote instance

& ofc handle all the networking//IO over some WebSocket and//or REST API

[Feature request] support wayland

$ uname -a
Linux debian 6.1.0-22-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.94-1 (2024-06-21) x86_64 GNU/Linux
$ gnome-shell --version
GNOME Shell 43.9
$ echo $XDG_SESSION_TYPE
wayland

Looks the python calls like async_input.click("left", x, y) just go through without any exception, but no click happens

/etc/gdm3/daemon.conf
$ sudo cat /etc/gdm3/daemon.conf 
# GDM configuration storage
#
# See /usr/share/gdm/gdm.schemas for a list of available options.

[daemon]
AutomaticLoginEnable=true
AutomaticLogin=user
TimedLoginEnable=true
TimedLogin=user
TimedLoginDelay=5
# Uncomment the line below to force the login screen to use Xorg
#WaylandEnable=false

# Enabling automatic login
#  AutomaticLoginEnable = true
#  AutomaticLogin = user1

# Enabling timed login
#  TimedLoginEnable = true
#  TimedLogin = user1
#  TimedLoginDelay = 10

[security]

[xdmcp]

[chooser]

[debug]
# Uncomment the line below to turn on debugging
# More verbose logs
# Additionally lets the X server dump core if it crashes
#Enable=true

[BUG] TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

At

if is_windows and not isinstance(self.browser, DriverlessAsyncChrome):
and
if is_windows and not isinstance(browser, (DriverlessSyncChrome, SeleniumChrome)):

On OS:Windows and not having Selenium-Driverless installed, we get:

E       TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

due to the second argument being a string ( set at

SeleniumChrome: Type["SeleniumChrome"] = "SeleniumChrome" # type: ignore[no-redef]
try:
from selenium_driverless.sync.webdriver import Chrome as DriverlessSyncChrome
from selenium_driverless.webdriver import Chrome as DriverlessAsyncChrome
except ImportError:
DriverlessAsyncChrome: Type["DriverlessAsyncChrome"] = "DriverlessAsyncChrome" # type: ignore[no-redef]
DriverlessSyncChrome: Type["DriverlessSyncChrome"] = "DriverlessSyncChrome" # type: ignore[no-redef]
)

[BUG] undetected_playwright is not supported.

Describe the bug

undetected_playwright is not supported. Normal playwright does have import, but not undetected_playwright. Throws TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union again.

Code Sample

#undetected_playwright import
from undetected_playwright.sync_api import sync_playwright
from cdp_patches.input import SyncInput
import requests
import csv

def get_locator_pos(locator):
    bounding_box = locator.bounding_box()
    assert bounding_box

    x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height")
    assert x and y and width and height

    x, y = x + width // 2, y + height // 2
    return x, y

with sync_playwright() as p:
    args = []
    args.append("--disable-blink-features=AutomationControlled")
    
    browser = p.chromium.launch(headless=False, args=args)
    page = browser.new_page()
    sync_input = SyncInput(browser=browser)

    page.goto("any page")
    
    verify_element = page.locator('any element locator')  
    x, y = get_locator_pos(verify_element)
    sync_input.click("left", x, y)
    
    browser.close()

To Reproduce

Steps to reproduce the behavior:

  1. Just install undetected_playwright. Use it to open any website.
  2. Use cdp-patches.
  3. Run the code.
  4. See error.

Expected behavior

There is no import for undetected_playwright for sync as well as async

from playwright.sync_api import Browser as SyncBrowser

Screenshots

None

Desktop (please complete the following information):

  • OS: Windows 10

Additional context
None

Unable to run documentation example

HI, authors, this is an amazing project thank you!
I am however unable to integrate CDP-Patches with playwright.
I tried the given code at the CDP-Patches documentation but it reverted to a type error.
I do have playwright and the browsers installed.

Exception has occurred: TypeError (note: full exception trace is shown but execution is paused at: _run_module_as_main) isinstance() arg 2 must be a type, a tuple of types, or a union File "C:\Users\User\Documents\cdp-patches\pw_venv\Lib\site-packages\cdp_patches\input\browsers.py", line 163, in get_async_browser_pid if isinstance(browser, DriverlessAsyncChrome):

cdp version = 1.0.1
playwright = 1.43.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.