Simplify webelem.js

serialize_elem() now takes care of assigning IDs.
This commit is contained in:
Florian Bruhin 2016-08-18 10:23:03 +02:00
parent 0d35502e87
commit 274fe58d6d

View File

@ -23,7 +23,14 @@ window._qutebrowser.webelem = (function() {
var funcs = {};
var elements = [];
function serialize_elem(elem, id) {
function serialize_elem(elem) {
if (!elem) {
return null;
}
var id = elements.length;
elements[id] = elem;
var out = {
"id": id,
"text": elem.text,
@ -60,13 +67,9 @@ window._qutebrowser.webelem = (function() {
funcs.find_all = function(selector) {
var elems = document.querySelectorAll(selector);
var out = [];
var id = elements.length;
for (var i = 0; i < elems.length; ++i) {
var elem = elems[i];
out.push(serialize_elem(elem, id));
elements[id] = elem;
id++;
out.push(serialize_elem(elems[i]));
}
return out;
@ -81,9 +84,7 @@ window._qutebrowser.webelem = (function() {
return null;
}
var id = elements.length;
elements[id] = elem;
return serialize_elem(elem, id);
return serialize_elem(elem);
};
funcs.set_text = function(id, text) {
@ -97,13 +98,7 @@ window._qutebrowser.webelem = (function() {
// element is returned (the iframe itself).
var elem = document.elementFromPoint(x, y);
if (!elem) {
return null;
}
var id = elements.length;
elements[id] = elem;
return serialize_elem(elem, id);
return serialize_elem(elem);
};
return funcs;