hot reload now refreshes iframe

also fixed fetch_nocors
This commit is contained in:
tza
2022-04-04 20:46:35 +03:00
parent 1d4100fabb
commit 8d0fe5c45a
3 changed files with 22 additions and 8 deletions

View File

@@ -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()")

View File

@@ -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);
}

View File

@@ -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");