This commit is contained in:
恍兮惚兮 2025-01-12 18:27:49 +08:00
parent f9a6d5110a
commit 17a8e0c13d
2 changed files with 39 additions and 32 deletions

View File

@ -302,15 +302,22 @@ DECLARE_API void get_root_html(ICoreWebView2Controller *m_host, void (*cb)(LPCWS
#ifndef WINXP #ifndef WINXP
wil::com_ptr<ICoreWebView2Controller> m_controller(m_host); wil::com_ptr<ICoreWebView2Controller> m_controller(m_host);
wil::com_ptr<ICoreWebView2> m_webView; wil::com_ptr<ICoreWebView2> m_webView;
wil::unique_handle asyncMethodCompleteEvent(CreateEvent(nullptr, false, false, nullptr));
CHECK_FAILURE_NORET(m_controller->get_CoreWebView2(&m_webView)); CHECK_FAILURE_NORET(m_controller->get_CoreWebView2(&m_webView));
CHECK_FAILURE_NORET(m_webView->ExecuteScript(L"document.documentElement.outerHTML", Callback<ICoreWebView2ExecuteScriptCompletedHandler>( CHECK_FAILURE_NORET(
[=](HRESULT errorCode, m_webView->ExecuteScript(
LPCWSTR resultObjectAsJson) L"document.documentElement.outerHTML",
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[=, asyncMethodCompleteEventHandle = asyncMethodCompleteEvent.get()](HRESULT errorCode, LPCWSTR resultObjectAsJson)
{ {
SetEvent(asyncMethodCompleteEventHandle);
cb(resultObjectAsJson); cb(resultObjectAsJson);
return S_OK; return S_OK;
}) })
.Get())); .Get()));
DWORD handleIndex = 0;
CoWaitForMultipleHandles(COWAIT_DISPATCH_WINDOW_MESSAGES | COWAIT_DISPATCH_CALLS | COWAIT_INPUTAVAILABLE,
INFINITE, 1, asyncMethodCompleteEvent.addressof(), &handleIndex);
#endif #endif
} }

View File

@ -1073,7 +1073,7 @@ class abstractwebview(QWidget):
html_limit = 2 * 1024 * 1024 html_limit = 2 * 1024 * 1024
# 必须的接口 # 必须的接口
def getHtml(self, callback): def getHtml(self):
return return
def setHtml(self, html): def setHtml(self, html):
@ -1157,13 +1157,13 @@ class WebivewWidget(abstractwebview):
# https://github.com/MicrosoftEdge/WebView2Feedback/issues/1355#issuecomment-1384161283 # https://github.com/MicrosoftEdge/WebView2Feedback/issues/1355#issuecomment-1384161283
dropfilecallback = pyqtSignal(str) dropfilecallback = pyqtSignal(str)
def getHtml(self, callback): def getHtml(self):
def __(html): _ = []
callback(json.loads(html)) cb = winsharedutils.html_get_select_text_cb(_.append)
cb = winsharedutils.html_get_select_text_cb(__)
winsharedutils.get_root_html(self.get_controller(), cb) winsharedutils.get_root_html(self.get_controller(), cb)
self.callbacks.append(cb) if not _:
return ""
return json.loads(_[0])
def __del__(self): def __del__(self):
if not self.webview: if not self.webview:
@ -1376,10 +1376,13 @@ class QWebWrap(abstractwebview):
class mshtmlWidget(abstractwebview): class mshtmlWidget(abstractwebview):
def getHtml(self, callback): def getHtml(self):
cb = winsharedutils.html_get_select_text_cb(callback) _ = []
cb = winsharedutils.html_get_select_text_cb(_.append)
winsharedutils.html_get_html(self.browser, cb) winsharedutils.html_get_html(self.browser, cb)
self.callbacks.append(cb) if not _:
return ""
return _[0]
def eval(self, js): def eval(self, js):
winsharedutils.html_eval(self.browser, js) winsharedutils.html_eval(self.browser, js)
@ -1550,16 +1553,13 @@ class auto_select_webview(QWidget):
self.internalsavedzoom = zoom self.internalsavedzoom = zoom
self.on_ZoomFactorChanged.emit(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): def _on_load(self, url):
self.saveurl = url self.saveurl = url
self.on_load.emit(url) self.on_load.emit(url)
def _createinternal(self): def _createinternal(self):
if self.internal:
self.layout().removeWidget(self.internal)
self.internal = self._createwebview() self.internal = self._createwebview()
self.internal.set_zoom(self.internalsavedzoom) self.internal.set_zoom(self.internalsavedzoom)
self.internal.on_load.connect(self._on_load) self.internal.on_load.connect(self._on_load)
@ -1571,15 +1571,15 @@ class auto_select_webview(QWidget):
self.internal.bind(*_) self.internal.bind(*_)
def _maybecreate_internal(self): def _maybecreate_internal(self):
if self.internal: if not self.internal:
return self._createinternal()
if self.saveurl and self.saveurl != "about:blank": if self.saveurl and self.saveurl != "about:blank":
self.layout().removeWidget(self.internal)
self._createinternal() self._createinternal()
self.internal.navigate(self.saveurl) self.internal.navigate(self.saveurl)
else: else:
self.internal.getHtml(self._gethtmlcallback) html = self.internal.getHtml()
return
self._createinternal() self._createinternal()
self.internal.setHtml(html)
def _createwebview(self): def _createwebview(self):
contex = globalconfig["usewebview"] contex = globalconfig["usewebview"]