summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortza <marios8543@gmail.com>2022-04-04 20:46:35 +0300
committertza <marios8543@gmail.com>2022-04-04 20:46:35 +0300
commit8d0fe5c45ac67ea4eef9a7c6a5e4eaaa57ad01d3 (patch)
treeb7f966d50db684ef51736d78da76c966a92e4563
parent1d4100fabb7f70eaf1c653f5fd0647c20aa71a63 (diff)
downloaddecky-loader-8d0fe5c45ac67ea4eef9a7c6a5e4eaaa57ad01d3.tar.gz
decky-loader-8d0fe5c45ac67ea4eef9a7c6a5e4eaaa57ad01d3.zip
hot reload now refreshes iframe
also fixed fetch_nocors
-rw-r--r--plugin_loader/loader.py22
-rw-r--r--plugin_loader/static/library.js3
-rw-r--r--plugin_loader/static/plugin_page.js5
3 files changed, 22 insertions, 8 deletions
diff --git a/plugin_loader/loader.py b/plugin_loader/loader.py
index 3d234cae..d74eed51 100644
--- a/plugin_loader/loader.py
+++ b/plugin_loader/loader.py
@@ -18,13 +18,13 @@ class FileChangeHandler(FileSystemEventHandler):
src_path = event.src_path
if "__pycache__" in src_path:
return
- self.loader.import_plugin(src_path)
+ self.loader.import_plugin(src_path, refresh=True)
def on_modified(self, event):
src_path = event.src_path
if "__pycache__" in src_path:
return
- self.loader.import_plugin(src_path)
+ self.loader.import_plugin(src_path, refresh=True)
class Loader:
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
@@ -47,7 +47,7 @@ class Loader:
web.get("/steam_resource/{path:.+}", self.get_steam_resource)
])
- def import_plugin(self, file):
+ def import_plugin(self, file, refresh=False):
try:
spec = spec_from_file_location("_", file)
module = module_from_spec(spec)
@@ -69,14 +69,14 @@ class Loader:
self.logger.info("Loaded {}".format(module.Plugin.name))
except Exception as e:
self.logger.error("Could not load {}. {}".format(file, e))
+ finally:
+ if refresh:
+ self.loop.create_task(self.refresh_iframe())
def import_plugins(self):
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
for file in files:
self.import_plugin(path.join(self.plugin_path, file))
-
- async def watch_for_file_change(self):
- pass
async def reload_plugins(self, request=None):
self.logger.info("Re-importing plugins.")
@@ -89,7 +89,10 @@ class Loader:
async def get_steam_resource(self, request):
tab = (await get_tabs())[0]
- return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
+ try:
+ return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
+ except Exception as e:
+ return web.Response(text=str(e), status=400)
async def load_plugin(self, request):
plugin = self.plugins[request.match_info["name"]]
@@ -103,3 +106,8 @@ class Loader:
@template('plugin_view.html')
async def plugin_iframe_route(self, request):
return {"plugins": self.plugins.values()}
+
+ async def refresh_iframe(self):
+ tab = next((i for i in await get_tabs() if i.title == "QuickAccess"), None)
+ await tab.open_websocket()
+ return await tab.evaluate_js("reloadIframe()") \ No newline at end of file
diff --git a/plugin_loader/static/library.js b/plugin_loader/static/library.js
index 1e35eee6..05e08ff2 100644
--- a/plugin_loader/static/library.js
+++ b/plugin_loader/static/library.js
@@ -2,7 +2,6 @@ class PluginEventTarget extends EventTarget { }
method_call_ev_target = new PluginEventTarget();
window.addEventListener("message", function(evt) {
- console.log(evt);
let ev = new Event(evt.data.call_id);
ev.data = evt.data.result;
method_call_ev_target.dispatchEvent(ev);
@@ -27,6 +26,8 @@ async function fetch_nocors(url, request={}) {
let args = { method: "POST", headers: {}, body: "" };
request = {...args, ...request};
request.url = url;
+ request.data = request.body;
+ delete request.body; //maintain api-compatibility with fetch
return await call_server_method("http_request", request);
}
diff --git a/plugin_loader/static/plugin_page.js b/plugin_loader/static/plugin_page.js
index c58188db..b42cd711 100644
--- a/plugin_loader/static/plugin_page.js
+++ b/plugin_loader/static/plugin_page.js
@@ -38,6 +38,11 @@
}, 100);
})();
+function reloadIframe() {
+ console.log("reloading iframe");
+ document.getElementById("plugin_iframe").contentWindow.location.href = "http://127.0.0.1:1337/plugins/iframe";
+}
+
function resolveMethodCall(call_id, result) {
let iframe = document.getElementById("plugin_iframe").contentWindow;
iframe.postMessage({'call_id': call_id, 'result': result}, "http://127.0.0.1:1337");