Added library function to execute code in a different tab

This commit is contained in:
WerWolv
2022-04-12 21:15:31 +02:00
parent 070d11154f
commit 012274b1a0
3 changed files with 33 additions and 18 deletions
+8 -16
View File
@@ -31,31 +31,22 @@ class Tab:
return (await self.websocket.receive_json()) if receive else None
raise RuntimeError("Websocket not opened")
async def evaluate_js(self, js):
async def evaluate_js(self, js, run_async):
await self.open_websocket()
res = await self._send_devtools_cmd({
"id": 1,
"method": "Runtime.evaluate",
"params": {
"expression": js,
"userGesture": True
"userGesture": True,
"awaitPromise": run_async
}
})
await self.client.close()
return res
async def get_steam_resource(self, url):
await self.open_websocket()
res = await self._send_devtools_cmd({
"id": 1,
"method": "Runtime.evaluate",
"params": {
"expression": f'(async function test() {{ return await (await fetch("{url}")).text() }})()',
"userGesture": True,
"awaitPromise": True
}
})
await self.client.close()
res = await self.evaluate_js(f'(async function test() {{ return await (await fetch("{url}")).text() }})()', True)
return res["result"]["result"]["value"]
def __repr__(self):
@@ -86,16 +77,17 @@ async def get_tab(tab_name):
raise ValueError("Tab {} not found".format(tab_name))
return tab
async def inject_to_tab(tab_name, js):
async def inject_to_tab(tab_name, js, run_async=False):
tab = await get_tab(tab_name)
logger.debug(f"Injected JavaScript Result: {await tab.evaluate_js(js)}")
return await tab.evaluate_js(js, run_async)
async def tab_has_element(tab_name, element_name):
try:
tab = await get_tab(tab_name)
except ValueError:
return False
res = await tab.evaluate_js(f"document.getElementById('{element_name}') != null")
res = await tab.evaluate_js(f"document.getElementById('{element_name}') != null", False)
if not "result" in res or not "result" in res["result"] or not "value" in res["result"]["result"]:
return False
+8
View File
@@ -39,4 +39,12 @@ async function call_plugin_method(method_name, arg_object={}) {
'method_name': method_name,
'args': arg_object
});
}
async function execute_in_tab(tab, run_async, code) {
return await call_server_method("execute_in_tab", {
'tab': tab,
'run_async': run_async,
'code': code
});
}
+17 -2
View File
@@ -1,4 +1,5 @@
from aiohttp import ClientSession
from injector import inject_to_tab
class Utilities:
def __init__(self, context) -> None:
@@ -6,7 +7,8 @@ class Utilities:
self.util_methods = {
"ping": self.ping,
"http_request": self.http_request,
"confirm_plugin_install": self.confirm_plugin_install
"confirm_plugin_install": self.confirm_plugin_install,
"execute_in_tab": self.execute_in_tab
}
async def confirm_plugin_install(self, request_id):
@@ -22,4 +24,17 @@ class Utilities:
}
async def ping(self, **kwargs):
return "pong"
return "pong"
async def execute_in_tab(self, tab, run_async, code):
try:
return {
"success": True,
"result" : await inject_to_tab(tab, code, run_async)
}
except Exception as e:
return {
"success": False,
"result": e
}