diff --git a/app/app_devtools.py b/app/app_devtools.py index 4bbe83d4..44ddbdc9 100644 --- a/app/app_devtools.py +++ b/app/app_devtools.py @@ -1,100 +1,67 @@ import devtools -import os -from pathlib import Path +## Create Variables r1 = { - "id": 0, "method": "Browser.setDownloadBehavior", "params": { "behavior": "allowAndName", - "downloadPath": "'+str(cwd)+'", + "downloadPath": "str(cwd)", "eventsEnabled": True, }, } -r2 = {"id": 1, "method": "Target.getTargets"} +r2 = {"method": "Target.getTargets"} r3 = { - "id": 2, "method": "Target.createTarget", - "params": {"url": "file://'+str(cwd / test.html)+'"}, + "params": {"url": "file://str(cwd / test.html)"}, } r4 = { - "id": 3, "method": "Target.attachToTarget", - "params": {"flatten": True, "targetId": "+tId+"}, + "params": {"flatten": True, "targetId": "tId"}, } -r5 = {"sessionId": "+sId+", "id": 0, "method": "Page.enable"} -r6 = {"sessionId": "+sId+", "id": 2, "method": "Page.reload"} +r5 = {"method": "Page.enable"} +r6 = {"method": "Page.reload"} r7 = { - "sessionId": "+sId+", - "id": 3, "method": "Browser.setDownloadBehavior", "params": { "behavior": "allow", - "downloadPath": "'+str(cwd)+'", + "downloadPath": "str(cwd)", "eventsEnabled": True, }, } r8 = { - "sessionId": "+sId+", - "id": 13, "method": "Page.setDownloadBehavior", "params": { "behavior": "allow", - "downloadPath": "'+str(cwd)+'", + "downloadPath": "str(cwd)", "eventsEnabled": True, }, } r9 = { - "sessionId": "+sId+", - "id": 23, "method": "Page.setInterceptFileChooserDialog", "params": {"enabled": True}, } -r10 = {"sessionId": "+sId+", "id": 4, "method": "Runtime.enable"} +r10 = {"method": "Runtime.enable"} r11 = { - "sessionId": "+sId+", - "id": 5, "method": "Runtime.compileScript", "params": { - "expression": "console.log(document.getElementsByTagName(\\'body\\')[0].innerHTML); 10; let goose = document.getElementById(\\'agoose2\\'); goose.download=\\'goose.jpg\\';goose.click();', 'sourceURL':'+str( cwd / 'test.html' )+', 'persistScript':true" + "expression": "console.log(document.getElementsByTagName(\\'body\\')[0].innerHTML); 10; let goose = document.getElementById(\\'agoose2\\'); goose.download=\\'goose.jpg\\';goose.click();', 'sourceURL':'str( cwd / 'test.html' )', 'persistScript':true" }, } r12 = { - "sessionId": "+sId+", - "id": 6, "method": "Runtime.runScript", - "params": {"scriptId": "+scriptId+"}, + "params": {"scriptId": "scriptId"}, } +list_r = [r1, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12] -list_r = [r1, r3, r4] -list_r_session_id = [r7, r8, r9, r11, r12] +## Create Connection +browser = devtools.Connection() +browser.list_tabs() -session_1 = devtools.Session() +## Print Commands for r in list_r: - print(session_1.send_command(command=r["method"], params=r["params"])) - -print(session_1.send_command(command=r2["method"], params=None)) - -print( - devtools.Session(str(r5["sessionId"])).send_command( - command=r5["method"], params=None - ) -) -print( - devtools.Session(str(r6["sessionId"])).send_command( - command=r6["method"], params=None - ) -) -print( - devtools.Session(str(r10["sessionId"])).send_command( - command=r10["method"], params=None - ) -) - -for r in list_r_session_id: print( - devtools.Session(str(r["sessionId"])).send_command( - command=r["method"], params=r["params"] + browser.browser_session.send_command( + command=r["method"], params=r["params"] if "params" in r else None ) ) diff --git a/devtools/connection.py b/devtools/connection.py index 59b638e9..4113d8d3 100644 --- a/devtools/connection.py +++ b/devtools/connection.py @@ -25,3 +25,6 @@ def list_tabs(self): def close_tab(self, session_obj): del self.tab_sessions[id(session_obj)] print(f"The following session was deleted: {session_obj.session_id}") + + def send_command(self, command, params=None, cb=None): + return self.browser_session.send_command(self, command, params, cb) diff --git a/devtools/session.py b/devtools/session.py index c2751ea0..3038c732 100644 --- a/devtools/session.py +++ b/devtools/session.py @@ -13,7 +13,7 @@ def __init__(self, parent, session_id=""): self.message_id = 0 self.parent_connection = parent - def send_command(self, command, params, cb=None): + def send_command(self, command, params=None, cb=None): if cb and not callable(cb): raise TypeError("The arg that you use, is not able at cb") if not isinstance(command, str): @@ -25,9 +25,11 @@ def send_command(self, command, params, cb=None): json_command = { "id": self.message_id, "method": command, - "params": params, } + if params: + json_command["params"] = params + if self.session_id != "": json_command["session_id"] = self.session_id diff --git a/pyproject.toml b/pyproject.toml index b2d1d898..5123b0bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,3 +15,6 @@ version = "0.0.1" dev = [ "pytest", ] + +[tool.ruff.lint] +ignore = ["E701"]