完成自定义桥接功能

master
yutent 2023-09-06 15:30:09 +08:00
parent 1ba77b4cce
commit fe56af2292
3 changed files with 16 additions and 1 deletions

View File

@ -7,4 +7,5 @@ from ._version import *
from ._webengine import WebEngine
from ._settings import create_setting
from ._hotreload import create_hmr_server
from ._hotreload import create_hmr_server
from ._custom_bridge import create_bridge

View File

@ -0,0 +1,7 @@
#!/usr/bin/env python3
def create_bridge():
def wrapper(app, extra = None):
app.custom_bridge = extra
return wrapper

View File

@ -73,6 +73,7 @@ class WebEngine(WebKit2.WebView):
root = None
window = None
custom_bridge = None
def __init__(self, window):
@ -200,6 +201,12 @@ class WebEngine(WebKit2.WebView):
case 'md5':
output = hashlib.md5(str(params.get('value'))).hexdigest()
case _:
if self.custom_bridge is None:
pass
else:
_error, output = self.custom_bridge(event, params)
# 有回调则返回结果
if callback:
self.call_js(callback, output, _error)