mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-07-11 19:51:58 +00:00
Added library function to execute code in a different tab
This commit is contained in:
@@ -31,31 +31,22 @@ class Tab:
|
|||||||
return (await self.websocket.receive_json()) if receive else None
|
return (await self.websocket.receive_json()) if receive else None
|
||||||
raise RuntimeError("Websocket not opened")
|
raise RuntimeError("Websocket not opened")
|
||||||
|
|
||||||
async def evaluate_js(self, js):
|
async def evaluate_js(self, js, run_async):
|
||||||
await self.open_websocket()
|
await self.open_websocket()
|
||||||
res = await self._send_devtools_cmd({
|
res = await self._send_devtools_cmd({
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"method": "Runtime.evaluate",
|
"method": "Runtime.evaluate",
|
||||||
"params": {
|
"params": {
|
||||||
"expression": js,
|
"expression": js,
|
||||||
"userGesture": True
|
"userGesture": True,
|
||||||
|
"awaitPromise": run_async
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
await self.client.close()
|
await self.client.close()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
async def get_steam_resource(self, url):
|
async def get_steam_resource(self, url):
|
||||||
await self.open_websocket()
|
res = await self.evaluate_js(f'(async function test() {{ return await (await fetch("{url}")).text() }})()', True)
|
||||||
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()
|
|
||||||
return res["result"]["result"]["value"]
|
return res["result"]["result"]["value"]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -86,16 +77,17 @@ async def get_tab(tab_name):
|
|||||||
raise ValueError("Tab {} not found".format(tab_name))
|
raise ValueError("Tab {} not found".format(tab_name))
|
||||||
return tab
|
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)
|
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):
|
async def tab_has_element(tab_name, element_name):
|
||||||
try:
|
try:
|
||||||
tab = await get_tab(tab_name)
|
tab = await get_tab(tab_name)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
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"]:
|
if not "result" in res or not "result" in res["result"] or not "value" in res["result"]["result"]:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -40,3 +40,11 @@ async function call_plugin_method(method_name, arg_object={}) {
|
|||||||
'args': arg_object
|
'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
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
|
from injector import inject_to_tab
|
||||||
|
|
||||||
class Utilities:
|
class Utilities:
|
||||||
def __init__(self, context) -> None:
|
def __init__(self, context) -> None:
|
||||||
@@ -6,7 +7,8 @@ class Utilities:
|
|||||||
self.util_methods = {
|
self.util_methods = {
|
||||||
"ping": self.ping,
|
"ping": self.ping,
|
||||||
"http_request": self.http_request,
|
"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):
|
async def confirm_plugin_install(self, request_id):
|
||||||
@@ -23,3 +25,16 @@ class Utilities:
|
|||||||
|
|
||||||
async def ping(self, **kwargs):
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user