selenium-injector


Nameselenium-injector JSON
Version 2.3 PyPI version JSON
download
home_pagehttps://github.com/kaliiiiiiiiii/Selenium-Injector
Summaryinject javascript into chrome
upload_time2023-07-14 13:53:59
maintainer
docs_urlNone
authorAurin Aegerter
requires_python>=3.7
license
keywords selenium interception proxy webautomation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Selenium-Injector

* Change proxy while running (auth supported)
* remotely contoll Chrome using websockets and extensions

### Feel free to test my code!

## Getting Started

### Dependencies

* [Python >= 3.7](https://www.python.org/downloads/)
* [Chrome-Browser](https://www.google.de/chrome/) installed
* Selenium

### Installing

* [Windows] Install [Chrome-Browser](https://www.google.de/chrome/)
* ```pip install selenium_injector```


### Example scripts

#### set proxy dynamically
```python
from selenium_injector.webdriver import Chrome
driver = Chrome()

driver.injector.proxy.set_single(host="example_host.com", port=143, password="password", username="user-1")

driver.get("https://whatismyipaddress.com/")

driver.injector.proxy.clear()
driver.quit()
```
Don't forget to execute
`driver.quit()`
in the End. Else-wise your temporary folder will get flooded! and it keeps running

#### use events
```python
from selenium_injector.webdriver import Chrome
import json

driver = Chrome()

driver.get("chrome://version")

t = driver.injector.socket.js.types

event_id = driver.injector.socket.make_event_id()
user = driver.injector.any_user

driver.injector.socket.exec(t.list([
    t.set_event_id(event_id),
    t.exec(
        t.path("chrome.webRequest.onCompleted.addListener"),
        args=[t.event_callback(), t.value({"urls": ["<all_urls>"]})]
    )
]), user=user, max_depth=1)

event = driver.injector.socket.event(event_id, user=user)
for e in event:  # will block forever
    e = json.loads(e)
    data = e["result"][0]
    time = e["t"]
    print(time + "\n", data['url'])
```
warning: as `driver.quit()` isn't called in this example, it will leave files in your temp directories

#### modify network requests
note: this is only experimental yet (not included in pypi package)

example script
```python
from selenium_injector.webdriver import Chrome

driver = Chrome()

# modify headers
driver.injector.declarativeNetRequest.update_headers({"test": "test_2", "sec-ch-ua-platform": "Android"})
rules = driver.injector.declarativeNetRequest.dynamic_rules
headers = driver.injector.declarativeNetRequest._headers

driver.get("https://httpbin.org/headers")
input("press ENTER to continue")

# block images
driver.injector.declarativeNetRequest.update_block_on(resource_types=["image"])

driver.get("https://www.wikimedia.org/")

input("press ENTER to exit")
driver.quit()
```

#### use chrome-developer-protocoll
note: this is only experimental yet (not included in pypi package)

example script
```python
import json
from selenium_injector.webdriver import Chrome

driver = Chrome()

dbg = driver.injector.debugger
dbg.attach()
dbg.execute("Console.enable")

events = dbg.on_event()

driver.execute_script("console.log('Hello World!')")

for event in events:
    event = json.loads(event)
    result = event["result"]
    time = event["t"]
    if result[1] == 'Console.messageAdded':
        message_text = result[2]["message"]["text"]
        print(time, message_text)
        break

driver.quit()
```

#### execute script within tab
note: this is only experimental yet (not included in pypi package)

from string
```python
from selenium_injector.webdriver import Chrome
driver = Chrome(injector_options={"mv2":True, "mv3":True})

driver.get("https://www.wikipedia.org/")

# result only returned with mv2 extension enabled
results = driver.injector.tabs.eval_str(
            '''
            console.log(window);
            navigator.userAgent
            ''', tab_id=driver.injector.tabs.active_tab["id"])
print(results[0])

driver.quit()
```

with types, always returns
resolves promises with MV3 automatically
```python
from selenium_injector.webdriver import Chrome
driver = Chrome(injector_options={"mv3":True})

driver.get("https://www.wikipedia.org/")

t = driver.injector.socket.js.types
results = driver.injector.tabs.exec(t.exec(t.path("fetch"), args=[t.value("https://www.wikipedia.org/")]), timeout=40)
print(results["result"][0])

driver.quit()
```

#### find element
note: this is only experimental yet (not included in pypi package)
```python
from selenium_injector.webdriver import Chrome
from selenium_injector.types.by import By

driver = Chrome(injector_options={"mv2":True, "mv3":True})
driver.get("https://www.wikipedia.org/")


elem = driver.injector.find_elements(By.XPATH, '//*[@id="js-link-box-en"]')
elem[0].click()

driver.quit()
```

## Help

Please feel free to open an issue or fork!

## Todo
- [x] eval within tab scope from extension
  - [x] mv2
    - [x] return 
    - [ ] stringify obj
  - [x] mv3
    - [ ] return
    - [ ] stringify obj
- [x] add events
  - [x] make protocoll use `UUIDS`'s
  - [ ] allow response to event within scope
    - using `(...args) => {new event_handler(...args)}`
- [x] types.eval
  - [ ] for-loops
- [x] authentificaten proxies
  - [x] manage webrtc-leak
  - [x] manage location api leak
  - [ ] proxy per request
- [ ] add automation tools
  - [x] click
  - [ ] send_keys
  - [ ] find_element
    - [x] by XPATH
- [ ] undetectability
  - [x] make tab scripts private
  - [x] support base_driver argument
  - [ ] make `/files/js/utils.js` private
## Deprecated

## Authors

[Aurin Aegerter](mailto:aurinliun@gmx.ch)

## License

Shield: [![CC BY-NC-SA 4.0][cc-by-nc-sa-shield]][cc-by-nc-sa]

This work is licensed under a
[Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa].

[![CC BY-NC-SA 4.0][cc-by-nc-sa-image]][cc-by-nc-sa]

[cc-by-nc-sa]: http://creativecommons.org/licenses/by-nc-sa/4.0/
[cc-by-nc-sa-image]: https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png
[cc-by-nc-sa-shield]: https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg

## Disclaimer

I am not responsible what you use the code for!!! Also no warranty!

## Acknowledgments

Inspiration, code snippets, etc.
* [Selenium-Profiles](https://github.com/kaliiiiiiiiii/Selenium-Profiles)
* [Chrome-devtools-protocol](https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-enable)
* [cdp_event_listeners](https://stackoverflow.com/questions/66227508/selenium-4-0-0-beta-1-how-add-event-listeners-in-cdp)
* [sync websocket server](https://stackoverflow.com/questions/68939894/implement-a-python-websocket-listener-without-async-asyncio)
* [chrome-extension-docs](https://developer.chrome.com/docs/extensions/reference/)
* [PEG-parser](https://github.com/pegjs/pegjs)
* [make-SV-stayalive](https://stackoverflow.com/a/75082732/20443541)
* [stringify-obj](https://stackoverflow.com/a/58416333/20443541)
* [inject code from mv3](https://stackoverflow.com/a/70949953/20443541)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kaliiiiiiiiii/Selenium-Injector",
    "name": "selenium-injector",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "Selenium,interception,proxy,webautomation",
    "author": "Aurin Aegerter",
    "author_email": "aurinliun@gmx.ch",
    "download_url": "https://files.pythonhosted.org/packages/79/3e/1a6889bdbd12006bc711a718b9ae0ad10293c8cedee0dd6ad65ff86d37e2/selenium_injector-2.3.tar.gz",
    "platform": null,
    "description": "# Selenium-Injector\r\n\r\n* Change proxy while running (auth supported)\r\n* remotely contoll Chrome using websockets and extensions\r\n\r\n### Feel free to test my code!\r\n\r\n## Getting Started\r\n\r\n### Dependencies\r\n\r\n* [Python >= 3.7](https://www.python.org/downloads/)\r\n* [Chrome-Browser](https://www.google.de/chrome/) installed\r\n* Selenium\r\n\r\n### Installing\r\n\r\n* [Windows] Install [Chrome-Browser](https://www.google.de/chrome/)\r\n* ```pip install selenium_injector```\r\n\r\n\r\n### Example scripts\r\n\r\n#### set proxy dynamically\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\ndriver = Chrome()\r\n\r\ndriver.injector.proxy.set_single(host=\"example_host.com\", port=143, password=\"password\", username=\"user-1\")\r\n\r\ndriver.get(\"https://whatismyipaddress.com/\")\r\n\r\ndriver.injector.proxy.clear()\r\ndriver.quit()\r\n```\r\nDon't forget to execute\r\n`driver.quit()`\r\nin the End. Else-wise your temporary folder will get flooded! and it keeps running\r\n\r\n#### use events\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\nimport json\r\n\r\ndriver = Chrome()\r\n\r\ndriver.get(\"chrome://version\")\r\n\r\nt = driver.injector.socket.js.types\r\n\r\nevent_id = driver.injector.socket.make_event_id()\r\nuser = driver.injector.any_user\r\n\r\ndriver.injector.socket.exec(t.list([\r\n    t.set_event_id(event_id),\r\n    t.exec(\r\n        t.path(\"chrome.webRequest.onCompleted.addListener\"),\r\n        args=[t.event_callback(), t.value({\"urls\": [\"<all_urls>\"]})]\r\n    )\r\n]), user=user, max_depth=1)\r\n\r\nevent = driver.injector.socket.event(event_id, user=user)\r\nfor e in event:  # will block forever\r\n    e = json.loads(e)\r\n    data = e[\"result\"][0]\r\n    time = e[\"t\"]\r\n    print(time + \"\\n\", data['url'])\r\n```\r\nwarning: as `driver.quit()` isn't called in this example, it will leave files in your temp directories\r\n\r\n#### modify network requests\r\nnote: this is only experimental yet (not included in pypi package)\r\n\r\nexample script\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\n\r\ndriver = Chrome()\r\n\r\n# modify headers\r\ndriver.injector.declarativeNetRequest.update_headers({\"test\": \"test_2\", \"sec-ch-ua-platform\": \"Android\"})\r\nrules = driver.injector.declarativeNetRequest.dynamic_rules\r\nheaders = driver.injector.declarativeNetRequest._headers\r\n\r\ndriver.get(\"https://httpbin.org/headers\")\r\ninput(\"press ENTER to continue\")\r\n\r\n# block images\r\ndriver.injector.declarativeNetRequest.update_block_on(resource_types=[\"image\"])\r\n\r\ndriver.get(\"https://www.wikimedia.org/\")\r\n\r\ninput(\"press ENTER to exit\")\r\ndriver.quit()\r\n```\r\n\r\n#### use chrome-developer-protocoll\r\nnote: this is only experimental yet (not included in pypi package)\r\n\r\nexample script\r\n```python\r\nimport json\r\nfrom selenium_injector.webdriver import Chrome\r\n\r\ndriver = Chrome()\r\n\r\ndbg = driver.injector.debugger\r\ndbg.attach()\r\ndbg.execute(\"Console.enable\")\r\n\r\nevents = dbg.on_event()\r\n\r\ndriver.execute_script(\"console.log('Hello World!')\")\r\n\r\nfor event in events:\r\n    event = json.loads(event)\r\n    result = event[\"result\"]\r\n    time = event[\"t\"]\r\n    if result[1] == 'Console.messageAdded':\r\n        message_text = result[2][\"message\"][\"text\"]\r\n        print(time, message_text)\r\n        break\r\n\r\ndriver.quit()\r\n```\r\n\r\n#### execute script within tab\r\nnote: this is only experimental yet (not included in pypi package)\r\n\r\nfrom string\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\ndriver = Chrome(injector_options={\"mv2\":True, \"mv3\":True})\r\n\r\ndriver.get(\"https://www.wikipedia.org/\")\r\n\r\n# result only returned with mv2 extension enabled\r\nresults = driver.injector.tabs.eval_str(\r\n            '''\r\n            console.log(window);\r\n            navigator.userAgent\r\n            ''', tab_id=driver.injector.tabs.active_tab[\"id\"])\r\nprint(results[0])\r\n\r\ndriver.quit()\r\n```\r\n\r\nwith types, always returns\r\nresolves promises with MV3 automatically\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\ndriver = Chrome(injector_options={\"mv3\":True})\r\n\r\ndriver.get(\"https://www.wikipedia.org/\")\r\n\r\nt = driver.injector.socket.js.types\r\nresults = driver.injector.tabs.exec(t.exec(t.path(\"fetch\"), args=[t.value(\"https://www.wikipedia.org/\")]), timeout=40)\r\nprint(results[\"result\"][0])\r\n\r\ndriver.quit()\r\n```\r\n\r\n#### find element\r\nnote: this is only experimental yet (not included in pypi package)\r\n```python\r\nfrom selenium_injector.webdriver import Chrome\r\nfrom selenium_injector.types.by import By\r\n\r\ndriver = Chrome(injector_options={\"mv2\":True, \"mv3\":True})\r\ndriver.get(\"https://www.wikipedia.org/\")\r\n\r\n\r\nelem = driver.injector.find_elements(By.XPATH, '//*[@id=\"js-link-box-en\"]')\r\nelem[0].click()\r\n\r\ndriver.quit()\r\n```\r\n\r\n## Help\r\n\r\nPlease feel free to open an issue or fork!\r\n\r\n## Todo\r\n- [x] eval within tab scope from extension\r\n  - [x] mv2\r\n    - [x] return \r\n    - [ ] stringify obj\r\n  - [x] mv3\r\n    - [ ] return\r\n    - [ ] stringify obj\r\n- [x] add events\r\n  - [x] make protocoll use `UUIDS`'s\r\n  - [ ] allow response to event within scope\r\n    - using `(...args) => {new event_handler(...args)}`\r\n- [x] types.eval\r\n  - [ ] for-loops\r\n- [x] authentificaten proxies\r\n  - [x] manage webrtc-leak\r\n  - [x] manage location api leak\r\n  - [ ] proxy per request\r\n- [ ] add automation tools\r\n  - [x] click\r\n  - [ ] send_keys\r\n  - [ ] find_element\r\n    - [x] by XPATH\r\n- [ ] undetectability\r\n  - [x] make tab scripts private\r\n  - [x] support base_driver argument\r\n  - [ ] make `/files/js/utils.js` private\r\n## Deprecated\r\n\r\n## Authors\r\n\r\n[Aurin Aegerter](mailto:aurinliun@gmx.ch)\r\n\r\n## License\r\n\r\nShield: [![CC BY-NC-SA 4.0][cc-by-nc-sa-shield]][cc-by-nc-sa]\r\n\r\nThis work is licensed under a\r\n[Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa].\r\n\r\n[![CC BY-NC-SA 4.0][cc-by-nc-sa-image]][cc-by-nc-sa]\r\n\r\n[cc-by-nc-sa]: http://creativecommons.org/licenses/by-nc-sa/4.0/\r\n[cc-by-nc-sa-image]: https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png\r\n[cc-by-nc-sa-shield]: https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg\r\n\r\n## Disclaimer\r\n\r\nI am not responsible what you use the code for!!! Also no warranty!\r\n\r\n## Acknowledgments\r\n\r\nInspiration, code snippets, etc.\r\n* [Selenium-Profiles](https://github.com/kaliiiiiiiiii/Selenium-Profiles)\r\n* [Chrome-devtools-protocol](https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-enable)\r\n* [cdp_event_listeners](https://stackoverflow.com/questions/66227508/selenium-4-0-0-beta-1-how-add-event-listeners-in-cdp)\r\n* [sync websocket server](https://stackoverflow.com/questions/68939894/implement-a-python-websocket-listener-without-async-asyncio)\r\n* [chrome-extension-docs](https://developer.chrome.com/docs/extensions/reference/)\r\n* [PEG-parser](https://github.com/pegjs/pegjs)\r\n* [make-SV-stayalive](https://stackoverflow.com/a/75082732/20443541)\r\n* [stringify-obj](https://stackoverflow.com/a/58416333/20443541)\r\n* [inject code from mv3](https://stackoverflow.com/a/70949953/20443541)\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "inject javascript into chrome",
    "version": "2.3",
    "project_urls": {
        "Bug Reports": "https://github.com/kaliiiiiiiiii/Selenium-Injector/issues",
        "Documentation": "https://github.com/kaliiiiiiiiii/Selenium-Injector",
        "Homepage": "https://github.com/kaliiiiiiiiii/Selenium-Injector",
        "Source Code": "https://github.com/kaliiiiiiiiii/Selenium-Injector"
    },
    "split_keywords": [
        "selenium",
        "interception",
        "proxy",
        "webautomation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "793e1a6889bdbd12006bc711a718b9ae0ad10293c8cedee0dd6ad65ff86d37e2",
                "md5": "7fc8f99e69943dd60e9fd3a816679aac",
                "sha256": "6fb8df7da2747189d06d6babf6fcfc5bf88277ad81f5f9f33cb6961b2d4cf362"
            },
            "downloads": -1,
            "filename": "selenium_injector-2.3.tar.gz",
            "has_sig": false,
            "md5_digest": "7fc8f99e69943dd60e9fd3a816679aac",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 27235,
            "upload_time": "2023-07-14T13:53:59",
            "upload_time_iso_8601": "2023-07-14T13:53:59.948102Z",
            "url": "https://files.pythonhosted.org/packages/79/3e/1a6889bdbd12006bc711a718b9ae0ad10293c8cedee0dd6ad65ff86d37e2/selenium_injector-2.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-14 13:53:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kaliiiiiiiiii",
    "github_project": "Selenium-Injector",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "selenium-injector"
}
        
Elapsed time: 0.24672s