From 64dc70b594a5a3ffb3f6eb1d638214eb8e7065ca Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 24 Jan 2024 17:37:31 +0800 Subject: [PATCH] =?UTF-8?q?js=E8=B0=83=E7=94=A8python=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=BC=82=E6=AD=A5;=20js=E8=B0=83=E7=94=A8python=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF=E6=94=B9=E4=B8=BA?= =?UTF-8?q?Error=E5=AF=B9=E8=B1=A1;=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=A1=A5=E6=8E=A5=E6=96=B9=E6=B3=95=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dist-packages/webengine/gtk3/_utils.py | 36 +++++++++++++++++-- .../dist-packages/webengine/gtk3/_version.py | 2 +- .../webengine/gtk3/_webengine.py | 16 +++++---- .../dist-packages/webengine/gtk3/inject.js | 2 +- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py b/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py index 3a4745a..06a1897 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py @@ -3,9 +3,9 @@ # @author yutent # @date 2023/07/28 14:39:33 -import gi +import gi, threading gi.require_version('Gtk', '3.0') -from gi.repository import GdkPixbuf +from gi.repository import GdkPixbuf, GObject def noop(): pass @@ -66,4 +66,34 @@ def dict_to_pixbuf(data): else: image = None - return image \ No newline at end of file + return image + + +# 定义一个异步修饰器, 用于在子线程中运行一些会阻塞主线程的任务 +def run_async(func): + def wrapper(*args, **kwargs): + thread = threading.Thread(target=func, args=args, kwargs=kwargs) + thread.daemon = True + thread.start() + return thread + return wrapper + +# 类型js的settimeout的修饰器 +def set_timeout(timeout = 0.5): + def decorator(callback): + def wrapper(*args): + t = threading.Timer(timeout, callback, args=args) + t.start() + return t + return wrapper + + return decorator + + +# 定义一个修饰器, 用于将当前方法转到主线程中运行 (子线程中调用方法时) +def idle(func): + def wrapper(*args): + GObject.idle_add(func, *args) + return wrapper + + diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_version.py b/usr/lib/python3/dist-packages/webengine/gtk3/_version.py index 23b88b2..b504f7b 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_version.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_version.py @@ -1,4 +1,4 @@ #!/usr/bin/env python3 -build = (0, 6, 1) +build = (0, 7, 0) version = '.'.join(map(str, build)) \ No newline at end of file diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py index fd354f5..4ca7b60 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py @@ -38,7 +38,7 @@ from ._settings import create_setting from ._protocal import create_protocal from ._notify import create_notify from ._inject import Inject -from ._utils import noop, get_monitor_info, pixbuf_to_dict, dict_to_pixbuf +from ._utils import noop, get_monitor_info, pixbuf_to_dict, dict_to_pixbuf, run_async, idle @@ -155,14 +155,14 @@ class WebEngine(WebKit2.WebView): raise ValueError('url must starts with "/" or "app://"') - + @idle def call_js(self, method, data = None, err = None): if err is not None: err = str(err) scripts = 'native.$emit("' + method + '", ' + json.dumps(err) + ', ' + json.dumps(data) + ')' self.evaluate_javascript(scripts, -1) - + @run_async def called_by_js(self, webview, message): data = json.loads(message.get_js_value().to_json(0)) @@ -268,10 +268,12 @@ class WebEngine(WebKit2.WebView): 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 self.custom_bridge is not None: + try: + _error, output = self.custom_bridge(event, params) or (None, None) + except Exception as err: + print(err) + _error = err # 有回调则返回结果 if callback: diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js index b5a4827..3c51dc2 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js +++ b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js @@ -73,7 +73,7 @@ callback = rand() native.$once(callback, (err, res) => { if (err) { - _.reject(err) + _.reject(new Error(err)) } else { _.resolve(res) }