summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerWolv <werwolv98@gmail.com>2022-04-12 21:59:09 +0200
committerWerWolv <werwolv98@gmail.com>2022-04-12 21:59:09 +0200
commitfe9faefd0bb01212299259bcdcc431d24493d963 (patch)
treea04b54f5660e6b55b4ac1272f79ae1a0c67e92c9
parent012274b1a09f6959d41e9aa8bd69f5c30cc8f871 (diff)
downloaddecky-loader-fe9faefd0bb01212299259bcdcc431d24493d963.tar.gz
decky-loader-fe9faefd0bb01212299259bcdcc431d24493d963.zip
Added functions to inject and remove css from tabs
-rw-r--r--plugin_loader/loader.py2
-rw-r--r--plugin_loader/static/library.js14
-rw-r--r--plugin_loader/utilities.py71
3 files changed, 84 insertions, 3 deletions
diff --git a/plugin_loader/loader.py b/plugin_loader/loader.py
index 1461d1d0..cf5ca381 100644
--- a/plugin_loader/loader.py
+++ b/plugin_loader/loader.py
@@ -190,4 +190,4 @@ class Loader:
async def refresh_iframe(self):
tab = await get_tab("QuickAccess")
await tab.open_websocket()
- return await tab.evaluate_js("reloadIframe()") \ No newline at end of file
+ return await tab.evaluate_js("reloadIframe()", False) \ No newline at end of file
diff --git a/plugin_loader/static/library.js b/plugin_loader/static/library.js
index 2ec6ce89..abd4453f 100644
--- a/plugin_loader/static/library.js
+++ b/plugin_loader/static/library.js
@@ -47,4 +47,18 @@ async function execute_in_tab(tab, run_async, code) {
'run_async': run_async,
'code': code
});
+}
+
+async function inject_css_into_tab(tab, style) {
+ return await call_server_method("inject_css_into_tab", {
+ 'tab': tab,
+ 'style': style
+ });
+}
+
+async function remove_css_from_tab(tab, css_id) {
+ return await call_server_method("remove_css_from_tab", {
+ 'tab': tab,
+ 'css_id': css_id
+ });
} \ No newline at end of file
diff --git a/plugin_loader/utilities.py b/plugin_loader/utilities.py
index a1785c15..0d6c549b 100644
--- a/plugin_loader/utilities.py
+++ b/plugin_loader/utilities.py
@@ -1,5 +1,6 @@
from aiohttp import ClientSession
from injector import inject_to_tab
+import uuid
class Utilities:
def __init__(self, context) -> None:
@@ -8,7 +9,9 @@ class Utilities:
"ping": self.ping,
"http_request": self.http_request,
"confirm_plugin_install": self.confirm_plugin_install,
- "execute_in_tab": self.execute_in_tab
+ "execute_in_tab": self.execute_in_tab,
+ "inject_css_into_tab": self.inject_css_into_tab,
+ "remove_css_from_tab": self.remove_css_from_tab
}
async def confirm_plugin_install(self, request_id):
@@ -28,9 +31,47 @@ class Utilities:
async def execute_in_tab(self, tab, run_async, code):
try:
+ result = await inject_to_tab(tab, code, run_async)
+
+ if "exceptionDetails" in result["result"]:
+ return {
+ "success": False,
+ "result": result["result"]
+ }
+
+ return {
+ "success": True,
+ "result" : result["result"]["result"]["value"]
+ }
+ except Exception as e:
+ return {
+ "success": False,
+ "result": e
+ }
+
+ async def inject_css_into_tab(self, tab, style):
+ try:
+ css_id = str(uuid.uuid4())
+
+ result = await inject_to_tab(tab,
+ f"""
+ (function() {{
+ const style = document.createElement('style');
+ style.id = "{css_id}";
+ document.head.append(style);
+ style.sheet.insertRule(`{style}`);
+ }})()
+ """, False)
+
+ if "exceptionDetails" in result["result"]:
+ return {
+ "success": False,
+ "result": result["result"]
+ }
+
return {
"success": True,
- "result" : await inject_to_tab(tab, code, run_async)
+ "result" : css_id
}
except Exception as e:
return {
@@ -38,3 +79,29 @@ class Utilities:
"result": e
}
+ async def remove_css_from_tab(self, tab, css_id):
+ try:
+ result = await inject_to_tab(tab,
+ f"""
+ (function() {{
+ let style = document.getElementById("{css_id}");
+
+ if (style.nodeName.toLowerCase() == 'style')
+ style.parentNode.removeChild(style);
+ }})()
+ """, False)
+
+ if "exceptionDetails" in result["result"]:
+ return {
+ "success": False,
+ "result": result
+ }
+
+ return {
+ "success": True
+ }
+ except Exception as e:
+ return {
+ "success": False,
+ "result": e
+ }