window.print() support for WebEngine

This commit is contained in:
Joakim Särehag 2018-04-24 14:05:53 +02:00
parent cfa5ee2835
commit 199eac2db8
3 changed files with 42 additions and 3 deletions

View File

@ -700,6 +700,12 @@ class WebEngineTab(browsertab.AbstractTab):
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()

View File

@ -48,7 +48,7 @@ class WebEngineView(QWebEngineView):
else:
profile = webenginesettings.default_profile
page = WebEnginePage(theme_color=theme_color, profile=profile,
parent=self)
parent=self, win_id=win_id)
self.setPage(page)
def shutdown(self):
@ -130,7 +130,7 @@ class WebEnginePage(QWebEnginePage):
shutting_down = pyqtSignal()
navigation_request = pyqtSignal(usertypes.NavigationRequest)
def __init__(self, *, theme_color, profile, parent=None):
def __init__(self, *, theme_color, profile, win_id, parent=None):
super().__init__(profile, parent)
self._is_shutting_down = False
self.featurePermissionRequested.connect(
@ -138,6 +138,7 @@ class WebEnginePage(QWebEnginePage):
self._theme_color = theme_color
self._set_bg_color()
config.instance.changed.connect(self._set_bg_color)
self._win_id = win_id
self.urlChanged.connect(self._inject_userjs)
@config.change_filter('colors.webpage.bg')
@ -310,9 +311,17 @@ class WebEnginePage(QWebEnginePage):
QWebEnginePage.NavigationTypeOther:
usertypes.NavigationRequest.Type.other,
}
accept_request = True
if(url.scheme() == "qute" and url.path() == "print"):
command_dispatcher = objreg.get('command-dispatcher',
scope='window',
window=self._win_id)
command_dispatcher.printpage()
accept_request = False
navigation = usertypes.NavigationRequest(url=url,
navigation_type=type_map[typ],
is_main_frame=is_main_frame)
is_main_frame=is_main_frame,
accepted=accept_request)
self.navigation_request.emit(navigation)
return navigation.accepted

View File

@ -0,0 +1,24 @@
/**
* Copyright 2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
*
* This file is part of qutebrowser.
*
* qutebrowser is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qutebrowser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
window.print = function() {
window.location = "qute:print";
};