Add run_js_eval and get :jseval to run

This commit is contained in:
Florian Bruhin 2016-06-14 18:47:26 +02:00
parent cd95f94ac8
commit edb65ecf50
4 changed files with 41 additions and 19 deletions

View File

@ -1107,7 +1107,7 @@ class CommandDispatcher:
QWebSettings.JavascriptEnabled):
if tab:
page.open_target = usertypes.ClickTarget.tab
page.currentFrame().evaluateJavaScript(
widget.run_js_async(
'window.getSelection().anchorNode.parentNode.click()')
else:
try:
@ -1698,26 +1698,29 @@ class CommandDispatcher:
js_code: The string to evaluate.
quiet: Don't show resulting JS object.
"""
frame = self._current_widget().page().mainFrame()
out = frame.evaluateJavaScript(js_code)
if quiet:
return
if out is None:
# Getting the actual error (if any) seems to be difficult. The
# error does end up in BrowserPage.javaScriptConsoleMessage(), but
# distinguishing between :jseval errors and errors from the webpage
# is not trivial...
message.info(self._win_id, 'No output or error')
jseval_cb = None
else:
# The output can be a string, number, dict, array, etc. But *don't*
# output too much data, as this will make qutebrowser hang
out = str(out)
if len(out) > 5000:
message.info(self._win_id, out[:5000] + ' [...trimmed...]')
else:
message.info(self._win_id, out)
def jseval_cb(out):
if out is None:
# Getting the actual error (if any) seems to be difficult.
# The error does end up in
# BrowserPage.javaScriptConsoleMessage(), but distinguishing
# between :jseval errors and errors from the webpage is not
# trivial...
message.info(self._win_id, 'No output or error')
else:
# The output can be a string, number, dict, array, etc. But
# *don't* output too much data, as this will make
# qutebrowser hang
out = str(out)
if len(out) > 5000:
message.info(self._win_id, out[:5000] + ' [...trimmed...]')
else:
message.info(self._win_id, out)
self._current_widget().run_js_async(js_code, callback=jseval_cb)
@cmdutils.register(instance='command-dispatcher', scope='window')
def fake_key(self, keystring, global_=False):

View File

@ -303,6 +303,14 @@ class AbstractTab(QWidget):
"""
raise NotImplementedError
def run_js_async(self, code, callback=None):
"""Run javascript async.
The given callback will be called with the result when running JS is
complete.
"""
raise NotImplementedError
def shutdown(self):
raise NotImplementedError

View File

@ -114,6 +114,12 @@ class WebEngineViewTab(tab.AbstractTab):
else:
self._widget.page().toHtml(callback)
def run_js_async(self, code, callback=None):
if callback is None:
self._widget.page().runJavaScript(code)
else:
self._widget.page().runJavaScript(code, callback)
def shutdown(self):
# TODO
pass

View File

@ -378,6 +378,11 @@ class WebViewTab(tab.AbstractTab):
else:
callback(frame.toHtml())
def run_js_async(self, code, callback=None):
result = self._widget.page().mainFrame().evaluateJavaScript(code)
if callback is not None:
callback(result)
def icon(self):
return self._widget.icon()