diff options
| author | WerWolv <werwolv98@gmail.com> | 2022-04-12 21:15:31 +0200 |
|---|---|---|
| committer | WerWolv <werwolv98@gmail.com> | 2022-04-12 21:15:36 +0200 |
| commit | 012274b1a09f6959d41e9aa8bd69f5c30cc8f871 (patch) | |
| tree | dd6a2815d4a4a7b948bce5493c04aebb2cfdfb92 | |
| parent | 070d11154f0ec761fba938a6f27b4a9111988412 (diff) | |
| download | decky-loader-012274b1a09f6959d41e9aa8bd69f5c30cc8f871.tar.gz decky-loader-012274b1a09f6959d41e9aa8bd69f5c30cc8f871.zip | |
Added library function to execute code in a different tab
| -rw-r--r-- | plugin_loader/injector.py | 24 | ||||
| -rw-r--r-- | plugin_loader/static/library.js | 8 | ||||
| -rw-r--r-- | plugin_loader/utilities.py | 19 |
3 files changed, 33 insertions, 18 deletions
diff --git a/plugin_loader/injector.py b/plugin_loader/injector.py index 85da1e6a..edb09527 100644 --- a/plugin_loader/injector.py +++ b/plugin_loader/injector.py @@ -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 diff --git a/plugin_loader/static/library.js b/plugin_loader/static/library.js index 05e08ff2..2ec6ce89 100644 --- a/plugin_loader/static/library.js +++ b/plugin_loader/static/library.js @@ -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 + }); }
\ No newline at end of file diff --git a/plugin_loader/utilities.py b/plugin_loader/utilities.py index a4abf5bc..a1785c15 100644 --- a/plugin_loader/utilities.py +++ b/plugin_loader/utilities.py @@ -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"
\ No newline at end of file + 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 + } + |
