GithubHelp home page GithubHelp logo

tintinweb / electron-inject Goto Github PK

View Code? Open in Web Editor NEW
315.0 9.0 37.0 23 KB

Inject javascript into closed source electron applications e.g. to enable developer tools for debugging.

Python 100.00%
electron electron-app debugging-tool

electron-inject's Introduction

💉 electron-inject

You find yourself locked out of closed source electron applications with no easy way to enable developer tools? ↷ electron-inject is here to help 👲

electron-inject is an application wrapper that utilizes the remote debug console to inject javascript code into electron based applications. For example, this can be pretty handy to enable otherwise unavailable features like the built-in developer console.

slack

install

$ pip install electron-inject

or

$ python setup.py install

usage

$ python -m electron_inject --help
Usage:
    usage:
           electron_inject [options] - <electron application>

    example:
           electron_inject --enable-devtools-hotkeys - /path/to/electron/powered/application [--app-params app-args]


Options:
  -h, --help            show this help message and exit
  -d, --enable-devtools-hotkeys
                        Enable Hotkeys F12 (Toggle Developer Tools) and F5
                        (Refresh) [default: False]
  -b, --browser         Launch Devtools in default browser. [default: False]
  -t TIMEOUT, --timeout=TIMEOUT
                        Try hard to inject for the time specified [default:
                        none]
  -r RENDER_SCRIPTS, --render-script=RENDER_SCRIPTS
                        Add a script to be injected into each window (render
                        thread)

Showcase

Inject hotkeys F12:toggle devconsole and F5:reload into closed source apps with devconsole disabled.

--enable-devtools-hotkeys .. enable developer hotkeys --timeout=xx .. patch all known remote webContent/windows in a timeframe of xx seconds. set this to an arbitrary high value to make sure we're patching all future windows.

whatsapp

$ python -m electron_inject -d -t 60 - \\PATH\TO\Local\WhatsApp\app-0.2.2244\WhatsApp.exe

whatsapp gif

If this gives you an error try launching it with the alternative browser method:

$ python -m electron_inject --browser - \PATH\TO\Local\WhatsApp\app-0.2.2244\WhatsApp.exe

slack

$ python -m electron_inject -d -t 60 - \\PATH\TO\Local\slack\app-2.5.2\slack.exe

slack

Render Scripts

Passing the -r file parameter allows to pass a list of scripts to be injected into the render thread. It does not follow imports, just evaluate the text

python -m electron_inject -r ./test.js -r ~/test2.js -r /usr/bin/test3.js - /opt/electron-api-demos/Electron\ API\ Demos

Acknowledgments

electron-inject's People

Contributors

desophos avatar nathanpb avatar tintinweb 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  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  avatar  avatar  avatar  avatar  avatar

electron-inject's Issues

Exception: Could not execute cmd

When I try to inject into Discord, I get this error Exception: Could not execute cmd: 'C:\Users\Dan\AppData\Local\Discord\app-1.0.9002\Discord.exe --remote-debugging-port=49391'

UnboundLocalError: local variable 'ce' referenced before assignment

I tried it for Whatsapp Web using Python 2.7.15 and Python 3.7.2. Both didn't work probably.

python3

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/site-packages/electron_inject-0.4-py3.7.egg/electron_inject/__main__.py", line 97, in <module>
  File "/usr/local/lib/python3.7/site-packages/electron_inject-0.4-py3.7.egg/electron_inject/__main__.py", line 76, in main
  File "/usr/local/lib/python3.7/site-packages/electron_inject-0.4-py3.7.egg/electron_inject/__init__.py", line 57, in windows
  File "/usr/local/lib/python3.7/site-packages/electron_inject-0.4-py3.7.egg/electron_inject/__init__.py", line 74, in requests_get
UnboundLocalError: local variable 'ce' referenced before assignment

python2

Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "build/bdist.macosx-10.14-x86_64/egg/electron_inject/__main__.py", line 97, in <module>
  File "build/bdist.macosx-10.14-x86_64/egg/electron_inject/__main__.py", line 76, in main
  File "build/bdist.macosx-10.14-x86_64/egg/electron_inject/__init__.py", line 57, in windows
  File "build/bdist.macosx-10.14-x86_64/egg/electron_inject/__init__.py", line 74, in requests_get
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=53415): Max retries exceeded with url: /json/list?t=1550338492 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x106a37910>: Failed to establish a new connection: [Errno 61] Connection refused',))

Is there any way of injecting scripts?

The examples show only how to inject the script to allow opening the devtools, but is there any way to inject other JS source code in the main process?

Startup failed

Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\lib\site-packages\electron_inject\__main__.py", line 97, in <module>
    main()
  File "C:\Python27\lib\site-packages\electron_inject\__main__.py", line 76, in main
    for w in (_ for _ in erb.windows() if _['id'] not in windows_visited):
  File "C:\Python27\lib\site-packages\electron_inject\__init__.py", line 57, in windows
    for w in self.requests_get("http://%(host)s:%(port)s/json/list?t=%(ts)d" % params).json():
  File "C:\Python27\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded```

require not defined

It works well, but when I press F12, the console says "require is not defined".
How can I resolve it ?

Fails to enable devtools or inject js into slack

Test Procedure

The following is the contents of test.js

setInterval(()=>{console.log("test")}, 100)

If it is successfully injected, we should see test logged to the console 10 times every second

To ensure the process runs correctly, I run the following to make sure slack has no background instances

taskkill -im slack.exe -f

Running the following command leads to slack successfully launching, with a perfect trace of what is being logged into the devtools console.

python -m electron_inject -d -t 600 -r ./test.js - C:\ProgramData\F53\slack\app-4.27.154\slack.exe

Pressing F12 in this new slack window does not open devtools, despite us having the -d argument

Running the /slackdevtools in any slack chat shows them manually.
Looking at the console we just opened, test is not being logged 10 times every second

Possibly relevant Info

Python 3.10.1
electron-inject 0.7
all installed python packages
Windows 10 Enterprise N build 19044.1826
Slack 4.27.154

Syntax issue running it on windows

Hello! I've just installed your project and I'm having issue running it. Any idea why that happens ?

Traceback (most recent call last):
  File "C:\Users\Edi\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\Users\Edi\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 128, in _get_module_details
    spec = importlib.util.find_spec(mod_name)
  File "C:\Users\Edi\AppData\Local\Programs\Python\Python37-32\lib\importlib\util.py", line 103, in find_spec
    return _find_spec(fullname, parent_path)
  File "<frozen importlib._bootstrap>", line 906, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1254, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1235, in _legacy_get_spec
  File "<frozen importlib._bootstrap>", line 441, in spec_from_loader
  File "<frozen importlib._bootstrap_external>", line 594, in spec_from_file_location
  File "C:\Users\Edi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\electron_inject-0.3-py3.7.egg\electron_inject\__init__.py", line 70
    except requests.exceptions.ConnectionError, ce:
                                              ^
SyntaxError: invalid syntax

Keep running after closing cmd prompt

I'm not sure this has much to do with your package, but I thought I'd ask for some guidance here anyway. I have a script that I want to inject into an electron app that modifies the UI. I've successfully done it through the command line and also through importing it as a module. My issue is when I close the console, it also closes the electron app.

This only happens when I run it on Windows and I'm guessing it has to do with terminating child processes, but I'm kind of out of my depth here. Is there a way that I can prevent this from happening?

injecting the script into newly created renderer threads

So I ran into situation where the app ( TradingView desktop version ) opens up a new renderer threads during the usage for example when opening a chart for a certain stock.

so my script is injected successfully for the renderer threads that exists when first opens the app but not injected into the new ones during using the app.

Is there a way to inject the scripts into the newly created renderer threads ?

pip repo is out of date

I attempted to install via pip, and it looks like it provided me with the python-2 compatible version. The pip repository package hasn't been updated since 2017, looks like python 3 migration was done in 2018

macOS problem injecting into slack

I run the command
python -m electron_inject -d -t 60 - /Applications/Slack.app/Contents/MacOS/Slack

The result is:

/Applications/Slack.app/Contents/MacOS/Slack --remote-debugging-port=57356
2018-03-29 11:14:07,454 - DEBUG - Starting new HTTP connection (1): localhost
2018-03-29 11:14:08,461 - DEBUG - Starting new HTTP connection (1): localhost
/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppleGVA/AppleGVA-11.3.7/Sources/Slices/Driver/AVD_loader.cpp: failed to get a service for display 5
2018-03-29 11:14:08,574 - DEBUG - http://localhost:57356 "GET /json/list?t=1522318447 HTTP/1.1" 200 5
2018-03-29 11:14:08,575 - DEBUG - timeout not hit.
Creating Slack Application
2018-03-29 11:14:09,577 - DEBUG - Starting new HTTP connection (1): localhost
2018-03-29 11:14:09,586 - DEBUG - http://localhost:57356 "GET /json/list?t=1522318449 HTTP/1.1" 200 4874
2018-03-29 11:14:09,586 - INFO - injecting hotkeys script into 3cf6736d-a891-43c5-9cbc-1a3355dce8ef
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"main", fname, loader, pkg_name)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/site-packages/electron_inject/main.py", line 72, in
main()
File "/usr/local/lib/python2.7/site-packages/electron_inject/main.py", line 61, in main
logger.debug(erb.eval(w, SCRIPT_HOTKEYS_F12_DEVTOOLS_F5_REFRESH))
File "/usr/local/lib/python2.7/site-packages/electron_inject/init.py", line 93, in eval
ret = json.loads(w['ws'].sendrcv(json.dumps(data)))
File "/usr/local/lib/python2.7/site-packages/electron_inject/init.py", line 41, in sendrcv
self.send(msg)
File "/usr/local/lib/python2.7/site-packages/electron_inject/init.py", line 35, in send
return self._connect().send(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/electron_inject/init.py", line 31, in _connect
self.ws = websocket.create_connection(self.url)
AttributeError: 'module' object has no attribute 'create_connection'

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.