diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index c7bc9d90e..a2c4cab49 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1294,22 +1294,20 @@ class CommandDispatcher: dest: Where to write the file to. plain: Write plain text instead of HTML. """ - web_view = self._current_widget() - mainframe = web_view.page().mainFrame() - if plain: - data = mainframe.toPlainText() - else: - data = mainframe.toHtml() - + tab = self._current_widget() dest = os.path.expanduser(dest) - try: - with open(dest, 'w', encoding='utf-8') as f: - f.write(data) - except OSError as e: - raise cmdexc.CommandError('Could not write page: {}'.format(e)) - else: - message.info(self._win_id, "Dumped page to {}.".format(dest)) + def callback(data): + try: + with open(dest, 'w', encoding='utf-8') as f: + f.write(data) + except OSError as e: + message.error(self._win_id, 'Could not write page: {}'.format(e)) + else: + message.info(self._win_id, "Dumped page to {}.".format(dest)) + + tab.dump_async(callback, plain=plain) + @cmdutils.register(instance='command-dispatcher', name='help', scope='window') diff --git a/qutebrowser/browser/tab.py b/qutebrowser/browser/tab.py index 4b2983d2c..dbcc1dbf1 100644 --- a/qutebrowser/browser/tab.py +++ b/qutebrowser/browser/tab.py @@ -150,6 +150,14 @@ class AbstractTab(QWidget): def openurl(self, url): raise NotImplementedError + def dump_async(self, callback=None, *, plain=False): + """Dump the current page to a file ascync. + + The given callback will be called with the result when dumping is + complete. + """ + raise NotImplementedError + def __repr__(self): url = utils.elide(self.cur_url.toDisplayString(QUrl.EncodeUnicode), 100) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 3c01e61c9..f7d08b9fb 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -83,6 +83,12 @@ class WebEngineViewTab(tab.AbstractTab): def scroll_pos(self): return (0, 0) + def dump_async(self, callback=None, *, plain=False): + if plain: + self._widget.page().toPlainText(callback) + else: + self._widget.page().toHtml(callback) + def _connect_signals(self): view = self._widget page = view.page() diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 8eec2a89d..dcdc85032 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -89,6 +89,13 @@ class WebViewTab(tab.AbstractTab): def scroll_pos(self): return self._widget.scroll_pos + def dump_async(self, callback=None, *, plain=False): + frame = self._widget.page().mainFrame() + if plain: + callback(frame.toPlainText()) + else: + callback(frame.toHtml()) + def _connect_signals(self): view = self._widget page = view.page()