Permalink
Browse files

First prototype of QtWebKit WebElements

  • Loading branch information...
The-Compiler committed Aug 4, 2016
1 parent 0169f3a commit 2232e7474a39353006c33b9c7b80ae638865e599
@@ -474,6 +474,11 @@ def _navigate_up(self, url, tab, background, window):
url.setPath(new_path)
self._open(url, tab, background, window)
+ @cmdutils.register(instance='command-dispatcher', scope='window')
+ def foobar(self):
+ """foo"""
+ self._current_widget().find_all_elements('a')
+
@cmdutils.register(instance='command-dispatcher', scope='window',
backend=usertypes.Backend.QtWebKit)
@cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment',
@@ -413,8 +413,15 @@ def clear_ssl_errors(self):
def find_all_elements(self, selector, *, only_visible=False):
log.stub()
+ js_code = javascript.assemble('webelem', 'find_all_elements', selector)
+ self.run_js_async(js_code, self._elem_cb)
return []
+ def _elem_cb(self, elems):
+ for elem in elems:
+ print(elem['id'])
+ self.run_js_async('_qutebrowser_get_element({}).style.backgroundColor = "#FF00FF";'.format(elem['id']))
+
def _connect_signals(self):
view = self._widget
page = view.page()
@@ -0,0 +1,61 @@
+/**
+ * Copyright 2016 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/>.
+ */
+
+
+document._qutebrowser_elements = [];
+
+
+function _qutebrowser_serialize_elem(elem, id) {
+ var out = {};
+
+ var attributes = {};
+ for (var i = 0; i < elem.attributes.length; ++i) {
+ attr = elem.attributes[i];
+ attributes[attr.name] = attr.value;
+ }
+ out["attributes"] = attributes;
+
+ out["text"] = elem.text;
+ out["tag_name"] = elem.tagName;
+ out["outer_xml"] = elem.outerHTML;
+ out["id"] = id;
+
+ return out;
+}
+
+
+function _qutebrowser_find_all_elements(selector) {
+ var elems = document.querySelectorAll(selector);
+ var out = [];
+ var id = document._qutebrowser_elements.length;
+
+ for (var i = 0; i < elems.length; ++i) {
+ var elem = elems[i];
+ out.push(_qutebrowser_serialize_elem(elem, id));
+ document._qutebrowser_elements[id] = elem;
+ id++;
+ }
+
+ return out;
+}
+
+
+function _qutebrowser_get_element(id) {
+ return document._qutebrowser_elements[id];
+}

0 comments on commit 2232e74

Please sign in to comment.