diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 51905f74d..13fef46a8 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1659,6 +1659,8 @@ class CommandDispatcher: """ try: elem.set_value(text) + # Kick off js handlers to trick them into thinking there was input. + elem.dispatch_event("input", bubbles=True) except webelem.OrphanedError: message.error('Edited element vanished') ed.backup() diff --git a/qutebrowser/browser/webelem.py b/qutebrowser/browser/webelem.py index 1d719738b..5ff59653b 100644 --- a/qutebrowser/browser/webelem.py +++ b/qutebrowser/browser/webelem.py @@ -139,6 +139,18 @@ class AbstractWebElement(collections.abc.MutableMapping): """Set the element value.""" raise NotImplementedError + def dispatch_event(self, event, bubbles=False, + cancelable=False, composed=False): + """Dispatch an event to the element. + + Args: + bubbles: Whether this event should bubble. + cancelable: Whether this event can be cancelled. + composed: Whether the event will trigger listeners outside of a + shadow root + """ + raise NotImplementedError + def insert_text(self, text): """Insert the given text into the element.""" raise NotImplementedError diff --git a/qutebrowser/browser/webengine/webengineelem.py b/qutebrowser/browser/webengine/webengineelem.py index aefa51f5a..4ef20da18 100644 --- a/qutebrowser/browser/webengine/webengineelem.py +++ b/qutebrowser/browser/webengine/webengineelem.py @@ -135,6 +135,10 @@ class WebEngineElement(webelem.AbstractWebElement): def set_value(self, value): self._js_call('set_value', value) + def dispatch_event(self, event, bubbles=False, + cancelable=False, composed=False): + self._js_call('dispatch_event', event, bubbles, cancelable, composed) + def caret_position(self): """Get the text caret position for the current element. diff --git a/qutebrowser/browser/webkit/webkitelem.py b/qutebrowser/browser/webkit/webkitelem.py index 7ec979d09..b9f2c55c2 100644 --- a/qutebrowser/browser/webkit/webkitelem.py +++ b/qutebrowser/browser/webkit/webkitelem.py @@ -128,6 +128,18 @@ class WebKitElement(webelem.AbstractWebElement): value = javascript.string_escape(value) self._elem.evaluateJavaScript("this.value='{}'".format(value)) + def dispatch_event(self, event, bubbles=False, + cancelable=False, composed=False): + self._check_vanished() + log.webelem.debug("Firing event on {!r} via javascript.".format(self)) + self._elem.evaluateJavaScript( + "this.dispatchEvent(new Event({}, " + "{{'bubbles': {}, 'cancelable': {}, 'composed': {}}}))" + .format(javascript.convert_js_arg(event), + javascript.convert_js_arg(bubbles), + javascript.convert_js_arg(cancelable), + javascript.convert_js_arg(composed))) + def caret_position(self): """Get the text caret position for the current element.""" self._check_vanished() diff --git a/qutebrowser/javascript/.eslintrc.yaml b/qutebrowser/javascript/.eslintrc.yaml index 9e99b0aa5..cb1bb1fcb 100644 --- a/qutebrowser/javascript/.eslintrc.yaml +++ b/qutebrowser/javascript/.eslintrc.yaml @@ -59,3 +59,4 @@ rules: multiline-ternary: ["error", "always-multiline"] max-lines-per-function: "off" require-unicode-regexp: "off" + max-params: "off" diff --git a/qutebrowser/javascript/caret.js b/qutebrowser/javascript/caret.js index 8f3ec8d72..28ab3fab0 100644 --- a/qutebrowser/javascript/caret.js +++ b/qutebrowser/javascript/caret.js @@ -1,5 +1,5 @@ /* eslint-disable max-len, max-statements, complexity, -max-params, default-case, valid-jsdoc */ +default-case, valid-jsdoc */ // Copyright 2014 The Chromium Authors. All rights reserved. // diff --git a/qutebrowser/javascript/webelem.js b/qutebrowser/javascript/webelem.js index ad18899f6..4b9a8e8cd 100644 --- a/qutebrowser/javascript/webelem.js +++ b/qutebrowser/javascript/webelem.js @@ -362,6 +362,15 @@ window._qutebrowser.webelem = (function() { document.execCommand("insertText", false, text); }; + funcs.dispatch_event = (id, event, bubbles = false, + cancelable = false, composed = false) => { + const elem = elements[id]; + elem.dispatchEvent( + new Event(event, {"bubbles": bubbles, + "cancelable": cancelable, + "composed": composed})); + }; + funcs.set_attribute = (id, name, value) => { elements[id].setAttribute(name, value); }; diff --git a/qutebrowser/utils/javascript.py b/qutebrowser/utils/javascript.py index 21b373dd1..620cc2c73 100644 --- a/qutebrowser/utils/javascript.py +++ b/qutebrowser/utils/javascript.py @@ -49,7 +49,7 @@ def string_escape(text): return text -def _convert_js_arg(arg): +def convert_js_arg(arg): """Convert the given argument so it's the equivalent in JS.""" if arg is None: return 'undefined' @@ -68,7 +68,7 @@ def _convert_js_arg(arg): def assemble(module, function, *args): """Assemble a javascript file and a function call.""" - js_args = ', '.join(_convert_js_arg(arg) for arg in args) + js_args = ', '.join(convert_js_arg(arg) for arg in args) if module == 'window': parts = ['window', function] else: diff --git a/tests/unit/utils/test_javascript.py b/tests/unit/utils/test_javascript.py index 0a196cfa1..143d569e4 100644 --- a/tests/unit/utils/test_javascript.py +++ b/tests/unit/utils/test_javascript.py @@ -89,9 +89,9 @@ class TestStringEscape: def test_convert_js_arg(arg, expected): if expected is TypeError: with pytest.raises(TypeError): - javascript._convert_js_arg(arg) + javascript.convert_js_arg(arg) else: - assert javascript._convert_js_arg(arg) == expected + assert javascript.convert_js_arg(arg) == expected @pytest.mark.parametrize('base, expected_base', [