Add view_source command.

This commit is contained in:
Florian Bruhin 2014-09-15 17:59:54 +02:00
parent 14cc77ff42
commit 715aec991e
2 changed files with 23 additions and 0 deletions

View File

@ -28,6 +28,9 @@ from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QClipboard
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebInspector
import pygments
import pygments.lexers
import pygments.formatters
from qutebrowser.commands import userscripts, cmdexc, cmdutils
from qutebrowser.config import config
@ -747,6 +750,22 @@ class CommandDispatcher:
page = self._current_widget().page()
self._tabs.download_get.emit(self._tabs.current_url(), page)
@cmdutils.register(instance='mainwindow.tabs.cmd')
def view_source(self):
"""Show the source of the current page."""
widget = self._current_widget()
if widget.viewing_source:
raise cmdexc.CommandError("Already viewing source!")
frame = widget.page().currentFrame()
url = self._tabs.current_url()
html = frame.toHtml()
lexer = pygments.lexers.HtmlLexer()
formatter = pygments.formatters.HtmlFormatter(full=True)
highlighted = pygments.highlight(html, lexer, formatter)
tab = self._tabs.tabopen(explicit=True)
tab.setHtml(highlighted, url)
tab.viewing_source = True
@cmdutils.register(instance='mainwindow.tabs.cmd',
modes=[usertypes.KeyMode.insert],
hide=True)

View File

@ -52,6 +52,8 @@ class WebView(QWebView):
inspector: The QWebInspector used for this webview.
load_status: loading status of this page (index into LoadStatus)
open_target: Where to open the next tab ("normal", "tab", "tab_bg")
viewing_source: Whether the webview is currently displaying source
code.
_page: The QWebPage behind the view
_cur_url: The current URL (accessed via cur_url property).
_has_ssl_errors: Whether SSL errors occured during loading.
@ -107,6 +109,7 @@ class WebView(QWebView):
lambda msg: setattr(self, 'statusbar_message', msg))
self.page().networkAccessManager().sslErrors.connect(
lambda *args: setattr(self, '_has_ssl_errors', True))
self.viewing_source = False
# FIXME find some way to hide scrollbars without setScrollBarPolicy
def __repr__(self):
@ -341,6 +344,7 @@ class WebView(QWebView):
def on_load_started(self):
"""Leave insert/hint mode and set vars when a new page is loading."""
self.progress = 0
self.viewing_source = False
self._has_ssl_errors = False
self._set_load_status(LoadStatus.loading)