From ba0bcd6cc9338bfa2f734decee6d10498489e725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sat, 22 Jun 2024 01:25:13 +0800 Subject: [PATCH] inspect --- .../LunaTranslator/gui/usefulwidget.py | 61 +++++++++++++++++-- .../LunaTranslator/rendertext/webview.py | 60 ------------------ 2 files changed, 56 insertions(+), 65 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 24165a0f..b677b309 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -1,7 +1,7 @@ from qtsymbols import * import os, platform, functools, threading, time from traceback import print_exc -import windows, qtawesome, winsharedutils +import windows, qtawesome, winsharedutils, gobject from webviewpy import ( webview_native_handle_kind_t, Webview, @@ -10,7 +10,7 @@ from webviewpy import ( from winsharedutils import HTMLBrowser from myutils.config import _TR, globalconfig from myutils.wrapper import Singleton, Singleton_close -from myutils.utils import nowisdark +from myutils.utils import nowisdark, checkportavailable class FocusCombo(QComboBox): @@ -1070,10 +1070,17 @@ class QWebWrap(abstractwebview): from PyQt5.QtWebEngineWidgets import QWebEngineView else: from PyQt6.QtWebEngineWidgets import QWebEngineView + if 'QTWEBENGINE_REMOTE_DEBUGGING' not in os.environ: + DEBUG_PORT = 5588 + for i in range(100): + if checkportavailable(DEBUG_PORT): + break + DEBUG_PORT += 1 + os.environ["QTWEBENGINE_REMOTE_DEBUGGING"] = str(DEBUG_PORT) + self.DEBUG_URL = "http://127.0.0.1:%s" % os.environ["QTWEBENGINE_REMOTE_DEBUGGING"] self.internal = QWebEngineView(self) - # self.internal.page().urlChanged.connect( - # lambda qurl: self.on_load.emit(qurl.url()) - # ) + self.internal.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.internal.customContextMenuRequested.connect(self._qwmenu) self.internal.loadFinished.connect(self._loadFinish) self.internal_zoom = 1 t = QTimer(self) @@ -1082,6 +1089,50 @@ class QWebWrap(abstractwebview): t.timeout.emit() t.start() + def _qwmenu(self, pos): + + if isqt5: + from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView + + web_menu = self.internal.page().createStandardContextMenu() + else: + from PyQt6.QtWebEngineWidgets import QWebEngineView + from PyQt6.QtWebEngineCore import QWebEnginePage + + web_menu = self.internal.createStandardContextMenu() + loadinspector = QAction("Inspect") + if ( + self.internal.page().action(QWebEnginePage.WebAction.InspectElement) + not in web_menu.actions() + ): + web_menu.addAction(loadinspector) + action = web_menu.exec(self.internal.mapToGlobal(pos)) + + if action == loadinspector: + + class QMW(saveposwindow): + def closeEvent(_self, e): + self.internal.page().setDevToolsPage(None) + super(QMW, _self).closeEvent(e) + + def __init__(_self) -> None: + super().__init__( + gobject.baseobject.settin_ui, + poslist=globalconfig["qwebinspectgeo"], + ) + _self.setWindowTitle("Inspect") + _self.internal = QWebEngineView(_self) + _self.setCentralWidget(_self.internal) + _self.internal.load(QUrl(self.DEBUG_URL)) + self.internal.page().setDevToolsPage(_self.internal.page()) + self.internal.page().triggerAction( + QWebEnginePage.WebAction.InspectElement + ) + + _self.show() + + QMW() + def _loadFinish(self): self.on_load.emit(self.internal.url().url()) diff --git a/LunaTranslator/LunaTranslator/rendertext/webview.py b/LunaTranslator/LunaTranslator/rendertext/webview.py index 0473a355..73191222 100644 --- a/LunaTranslator/LunaTranslator/rendertext/webview.py +++ b/LunaTranslator/LunaTranslator/rendertext/webview.py @@ -18,20 +18,8 @@ class TextBrowser(QWidget, dataget): def __init__(self, parent) -> None: super().__init__(parent) if globalconfig["rendertext_using"] == "QWebEngine": - DEBUG_PORT = 5588 - for i in range(100): - if checkportavailable(DEBUG_PORT): - break - DEBUG_PORT += 1 - self.DEBUG_URL = "http://127.0.0.1:%s" % DEBUG_PORT - os.environ["QTWEBENGINE_REMOTE_DEBUGGING"] = str(DEBUG_PORT) self.webivewwidget = QWebWrap(self) self.webivewwidget.on_load.connect(self.__loadextra) - self.webivewwidget.internal.setContextMenuPolicy( - Qt.ContextMenuPolicy.CustomContextMenu - ) - self.webivewwidget.internal.customContextMenuRequested.connect(self._qwmenu) - else: # webview2当会执行alert之类的弹窗js时,若qt窗口不可视,会卡住 self.webivewwidget = WebivewWidget(self) @@ -46,54 +34,6 @@ class TextBrowser(QWidget, dataget): self.isfirst = True self._qweb_query_word() - def _qwmenu(self, pos): - - if isqt5: - from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView - - web_menu = self.webivewwidget.internal.page().createStandardContextMenu() - else: - from PyQt6.QtWebEngineWidgets import QWebEngineView - from PyQt6.QtWebEngineCore import QWebEnginePage - - web_menu = self.webivewwidget.internal.createStandardContextMenu() - loadinspector = QAction("Inspect") - if ( - self.webivewwidget.internal.page().action( - QWebEnginePage.WebAction.InspectElement - ) - not in web_menu.actions() - ): - web_menu.addAction(loadinspector) - action = web_menu.exec(self.webivewwidget.mapToGlobal(pos)) - - if action == loadinspector: - - class QMW(saveposwindow): - def closeEvent(_self, e): - self.webivewwidget.internal.page().setDevToolsPage(None) - super(QMW, _self).closeEvent(e) - - def __init__(_self) -> None: - super().__init__( - gobject.baseobject.settin_ui, - poslist=globalconfig["qwebinspectgeo"], - ) - _self.setWindowTitle("Inspect") - _self.internal = QWebEngineView(_self) - _self.setCentralWidget(_self.internal) - _self.internal.load(QUrl(self.DEBUG_URL)) - self.webivewwidget.internal.page().setDevToolsPage( - _self.internal.page() - ) - self.webivewwidget.internal.page().triggerAction( - QWebEnginePage.WebAction.InspectElement - ) - - _self.show() - - QMW() - def showEvent(self, e): if not self.isfirst: return