1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# Copyright 2015-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Customized QWebInspector for QtWebEngine."""
from typing import Optional
from qutebrowser.qt import machinery
from qutebrowser.qt.webenginewidgets import QWebEngineView
from qutebrowser.qt.webenginecore import QWebEnginePage
from qutebrowser.qt.widgets import QWidget
from qutebrowser.browser import inspector
from qutebrowser.browser.webengine import webenginesettings, webview
from qutebrowser.misc import miscwidgets
from qutebrowser.utils import version, usertypes, qtutils
from qutebrowser.keyinput import modeman
class WebEngineInspectorView(QWebEngineView):
"""The QWebEngineView used for the inspector.
We don't use a qutebrowser WebEngineView because that has various
customization which doesn't apply to the inspector.
"""
def createWindow(self,
wintype: QWebEnginePage.WebWindowType) -> QWebEngineView:
"""Called by Qt when a page wants to create a new tab or window.
In case the user wants to open a resource in a new tab, we use the
createWindow handling of the main page to achieve that.
See WebEngineView.createWindow for details.
"""
inspected_page = self.page().inspectedPage()
if machinery.IS_QT5:
view = inspected_page.view()
assert isinstance(view, QWebEngineView), view
return view.createWindow(wintype)
else: # Qt 6
newpage = inspected_page.createWindow(wintype)
return webview.WebEngineView.forPage(newpage)
class WebEngineInspector(inspector.AbstractWebInspector):
"""A web inspector for QtWebEngine with Qt API support."""
_widget: WebEngineInspectorView
def __init__(self, splitter: miscwidgets.InspectorSplitter,
win_id: int,
parent: QWidget = None) -> None:
super().__init__(splitter, win_id, parent)
self._check_devtools_resources()
self._settings: Optional[webenginesettings.WebEngineSettings] = None
def _on_window_close_requested(self) -> None:
"""Called when the 'x' was clicked in the devtools."""
modeman.leave(
self._win_id,
usertypes.KeyMode.insert,
'devtools close requested',
maybe=True,
)
self.hide()
def _check_devtools_resources(self) -> None:
"""Make sure that the devtools resources are available on Fedora.
Fedora packages devtools resources into its own package. If it's not
installed, we show a nice error instead of a blank inspector.
"""
dist = version.distribution()
if dist is None or dist.parsed != version.Distribution.fedora:
return
data_path = qtutils.library_path(qtutils.LibraryPath.data)
pak = data_path / 'resources' / 'qtwebengine_devtools_resources.pak'
if not pak.exists():
raise inspector.Error("QtWebEngine devtools resources not found, "
"please install the qt5-qtwebengine-devtools "
"Fedora package.")
def inspect(self, page: QWebEnginePage) -> None:
if not self._widget:
view = WebEngineInspectorView()
inspector_page = QWebEnginePage(
page.profile(),
self
)
inspector_page.windowCloseRequested.connect(self._on_window_close_requested)
view.setPage(inspector_page)
self._settings = webenginesettings.WebEngineSettings(view.settings())
self._set_widget(view)
inspector_page = self._widget.page()
assert inspector_page.profile() == page.profile()
inspector_page.setInspectedPage(page)
assert self._settings is not None
self._settings.update_for_url(inspector_page.requestedUrl())
def _needs_recreate(self) -> bool:
"""Recreate the inspector when detaching to a window.
WORKAROUND for what's likely an unknown Qt bug.
"""
return True
|