From 0e756d2f68027d8c9cf6847b91c3263bbeb337b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=A4rehag?= Date: Wed, 9 May 2018 14:36:03 +0200 Subject: [PATCH] changes to pull request 3864, window.print for webengine --- qutebrowser/browser/webengine/webenginetab.py | 26 ++++++++++--------- qutebrowser/javascript/print.js | 8 +++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index dfa5ed6bc..8b34b20f6 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -696,16 +696,14 @@ class WebEngineTab(browsertab.AbstractTab): utils.read_file('javascript/webelem.js'), utils.read_file('javascript/caret.js'), ) + self._inject_early_js('js', + utils.read_file('javascript/print.js'), + subframes=True, + world=QWebEngineScript.MainWorld) # FIXME:qtwebengine what about subframes=True? self._inject_early_js('js', js_code, subframes=True) self._init_stylesheet() - js_code_print = utils.read_file('javascript/print.js') - self._inject_early_js('js', - js_code_print, - subframes=True, - world=QWebEngineScript.MainWorld) - greasemonkey = objreg.get('greasemonkey') greasemonkey.scripts_reloaded.connect(self._inject_userscripts) self._inject_userscripts() @@ -761,9 +759,7 @@ class WebEngineTab(browsertab.AbstractTab): scripts.insert(new_script) def _install_event_filter(self): - fp = self._widget.focusProxy() - if fp is not None: - fp.installEventFilter(self._mouse_event_filter) + self._widget.focusProxy().installEventFilter(self._mouse_event_filter) self._child_event_filter = mouse.ChildEventFilter( eventfilter=self._mouse_event_filter, widget=self._widget, parent=self) @@ -1052,6 +1048,14 @@ class WebEngineTab(browsertab.AbstractTab): @pyqtSlot(usertypes.NavigationRequest) def _on_navigation_request(self, navigation): super()._on_navigation_request(navigation) + + if navigation.url == QUrl('qute://print'): + command_dispatcher = objreg.get('command-dispatcher', + scope='window', + window=self.win_id) + command_dispatcher.printpage() + navigation.accepted = False + if not navigation.accepted or not navigation.is_main_frame: return @@ -1110,6 +1114,4 @@ class WebEngineTab(browsertab.AbstractTab): self.predicted_navigation.connect(self._on_predicted_navigation) def event_target(self): - fp = self._widget.focusProxy() - assert fp is not None - return fp + return self._widget.focusProxy() diff --git a/qutebrowser/javascript/print.js b/qutebrowser/javascript/print.js index 20e1dadce..922d1c59e 100644 --- a/qutebrowser/javascript/print.js +++ b/qutebrowser/javascript/print.js @@ -17,8 +17,14 @@ * along with qutebrowser. If not, see . */ +/* + * this is a hack based on the QupZilla solution, https://github.com/QupZilla/qupzilla/commit/d3f0d766fb052dc504de2426d42f235d96b5eb60 + * + * We go to a qute://print which triggers the print, then we cancel the request. + */ + "use strict"; window.print = function() { - window.location = "qute:print"; + window.location = "qute://print"; };