This project allows you to control browser tabs from python (also nodejs terminal, coming soon)
Tested on Chrome 96, Firefox 108 on Linux and Windows x86 with Tampermonkey and Python 3.8
Also tested on KDE Falkon :)
There are a few known bugs, which I am working on
-
Clone the repo
-
Run setup.py to install module
-
Install websockets module and run coordinator.py server
-
Install Tampermonkey and load the script js_control.js
import js_control as js
tabdata = {"url": "URL OF TAB", "title": "TITLE OF TAB"} # Data used to identify tab
tab = js.WSSTab("127.0.0.1", 16388, tabdata) # Connect to browser tab
document = js.Object("document", tab) # Locate document object on tab
myelement = document.createElement("p")
myelement.innerHTML = "I ran javascript"
document.body.appendChild(myelement)
As of the latest update, you no longer need to place strings in double quotes. If you want to refer to a JS variable, use a new Variable object
All messages between python and the browser are co-ordinated by the coordinator.
It operates a websocket server on port 16384 and a socket server on port 16388
Messages are sent in JSON with scripts sent as
{"type": "data", "tab": {"url": "<Tab URL>", "title": "<Tab title>"}, "data": {"script": "<Script to execute, uses eval sorry>"}}
The browser will then return the following JSON data
{"type": "data", "tab": {"url": "<Tab URL>", "title": "<Tab title>"}, "data": {"return": "<Data returned>"}}
If an error occurs, this message will be sent
{"type": "data", "tab": {"url": "<Tab URL>", "title": "<Tab title>"}, "data": {"error": "<Exception, fresh out of a try ... catch>"}}
When a tab connects it will send this
{"type": "meta", "data": {"url": "<Tab URL>", "title": "<Tab title>"}}
Finally commands are sent using this format. Their return data will be shown in the commands section
{"type": "meta", "tab": OPTIONAL, "data": {"command": "<command>"}}
OR
{"type": "meta", "tab": OPTIONAL, "data": {"command": ["<command>", "<arg1>", "<arg2>", ...]}}
"resend_info": Runs on all tabs. Forces them to resend their tabdata
"ping": Just returns "pong". Runs on specified tab
String containing reference to javascript variable
Tab object. Inherits from BaseTab
Original definition of javascript variable. usually a function call
If you manage to create an instance of this class, something went seriously wrong.
Locates a variable on tab. If once is True, a new variable will be created
This is used when inheriting from Variable. jstype is the type given from typeof x in javascript. Dont know why it edits the registry of Variable
Gets, sets or deletes attribute on javascript variable. Does not require two pairs of quotes, except for setting attribute value
Gets, sets or deletes item on javascript variable. Requires two pairs of quotes
Returns <{self.__class__.__module__}.{self.__class__.__name__} {self._name or self._def} at {hex(id(self))}>
Returns self._name
or self._def
if applicable
Returns str(self).encode()
Returns (self._tab.send_script(f"String(!!{self._name})") == "true")
Returned from javascript.variable.Variable.__new__
when JS variable referred to corresponds to type. No special functions
The really interesting part! Parses args and kwargs into string literal and returns Variable with function call as _def
Creates a new variable on javascript end using once=True
Just returns integer representation of itself
Just returns string representation of itself
Im not writing that. if you want to see, just look at js.errors.all_errors
Generates a random string. used by Variable to generate new variable name
Converts various python types to valid literals in javascript
Gets the real type of a JS object. uses Object.prototype.toString.call
All tab objects must inherit from this
Used for testing. Instead of sending JS to browser, it will run input()
The default tab. Connects to co-ordinator
Just sets up variables, will only connect to ping tab unless ping is False
Returns f"{self.__class__.__name__}({self.connection[0]}, {self.connection[1]}, {self.tabdata})"
Sends raw JSON data. Use send_script with fdata instead
Sends javascript. will return the JS data or raise an error corresponding to JS errors
Allows javascript to call python functions. Not done yet, so do not use
Also not done yet
Forces all tabs to resend data, returns list of WSSTab. URL search and title search not implemented, do not use the arguments
Just calls the function described above
Definently not done yet, only works on linux due to broken pty module