diff --git a/cpp/winsharedutils/webview2_extra.cpp b/cpp/winsharedutils/webview2_extra.cpp index 9e7177d5..683e6f72 100644 --- a/cpp/winsharedutils/webview2_extra.cpp +++ b/cpp/winsharedutils/webview2_extra.cpp @@ -302,15 +302,22 @@ DECLARE_API void get_root_html(ICoreWebView2Controller *m_host, void (*cb)(LPCWS #ifndef WINXP wil::com_ptr m_controller(m_host); wil::com_ptr m_webView; + wil::unique_handle asyncMethodCompleteEvent(CreateEvent(nullptr, false, false, nullptr)); CHECK_FAILURE_NORET(m_controller->get_CoreWebView2(&m_webView)); - CHECK_FAILURE_NORET(m_webView->ExecuteScript(L"document.documentElement.outerHTML", Callback( - [=](HRESULT errorCode, - LPCWSTR resultObjectAsJson) - { - cb(resultObjectAsJson); - return S_OK; - }) - .Get())); + CHECK_FAILURE_NORET( + m_webView->ExecuteScript( + L"document.documentElement.outerHTML", + Callback( + [=, asyncMethodCompleteEventHandle = asyncMethodCompleteEvent.get()](HRESULT errorCode, LPCWSTR resultObjectAsJson) + { + SetEvent(asyncMethodCompleteEventHandle); + cb(resultObjectAsJson); + return S_OK; + }) + .Get())); + DWORD handleIndex = 0; + CoWaitForMultipleHandles(COWAIT_DISPATCH_WINDOW_MESSAGES | COWAIT_DISPATCH_CALLS | COWAIT_INPUTAVAILABLE, + INFINITE, 1, asyncMethodCompleteEvent.addressof(), &handleIndex); #endif } diff --git a/py/LunaTranslator/gui/usefulwidget.py b/py/LunaTranslator/gui/usefulwidget.py index 95627091..d2e22492 100644 --- a/py/LunaTranslator/gui/usefulwidget.py +++ b/py/LunaTranslator/gui/usefulwidget.py @@ -1073,7 +1073,7 @@ class abstractwebview(QWidget): html_limit = 2 * 1024 * 1024 # 必须的接口 - def getHtml(self, callback): + def getHtml(self): return def setHtml(self, html): @@ -1157,13 +1157,13 @@ class WebivewWidget(abstractwebview): # https://github.com/MicrosoftEdge/WebView2Feedback/issues/1355#issuecomment-1384161283 dropfilecallback = pyqtSignal(str) - def getHtml(self, callback): - def __(html): - callback(json.loads(html)) - - cb = winsharedutils.html_get_select_text_cb(__) + def getHtml(self): + _ = [] + cb = winsharedutils.html_get_select_text_cb(_.append) winsharedutils.get_root_html(self.get_controller(), cb) - self.callbacks.append(cb) + if not _: + return "" + return json.loads(_[0]) def __del__(self): if not self.webview: @@ -1376,10 +1376,13 @@ class QWebWrap(abstractwebview): class mshtmlWidget(abstractwebview): - def getHtml(self, callback): - cb = winsharedutils.html_get_select_text_cb(callback) + def getHtml(self): + _ = [] + cb = winsharedutils.html_get_select_text_cb(_.append) winsharedutils.html_get_html(self.browser, cb) - self.callbacks.append(cb) + if not _: + return "" + return _[0] def eval(self, js): winsharedutils.html_eval(self.browser, js) @@ -1550,16 +1553,13 @@ class auto_select_webview(QWidget): self.internalsavedzoom = zoom self.on_ZoomFactorChanged.emit(zoom) - def _gethtmlcallback(self, html): - self.layout().removeWidget(self.internal) - self._createinternal() - self.internal.setHtml(html) - def _on_load(self, url): self.saveurl = url self.on_load.emit(url) def _createinternal(self): + if self.internal: + self.layout().removeWidget(self.internal) self.internal = self._createwebview() self.internal.set_zoom(self.internalsavedzoom) self.internal.on_load.connect(self._on_load) @@ -1571,15 +1571,15 @@ class auto_select_webview(QWidget): self.internal.bind(*_) def _maybecreate_internal(self): - if self.internal: - if self.saveurl and self.saveurl != "about:blank": - self.layout().removeWidget(self.internal) - self._createinternal() - self.internal.navigate(self.saveurl) - else: - self.internal.getHtml(self._gethtmlcallback) - return - self._createinternal() + if not self.internal: + return self._createinternal() + if self.saveurl and self.saveurl != "about:blank": + self._createinternal() + self.internal.navigate(self.saveurl) + else: + html = self.internal.getHtml() + self._createinternal() + self.internal.setHtml(html) def _createwebview(self): contex = globalconfig["usewebview"]