Implement follow_selected in frames

This commit is contained in:
Jay Kamat 2017-12-07 14:46:18 -05:00
parent 0fc99108bf
commit 825939633a
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5

View File

@ -227,7 +227,7 @@ window._qutebrowser.webelem = (function() {
// Check if elem is an iframe, and if so, run func on it. // Check if elem is an iframe, and if so, run func on it.
// If no iframes match, return null // If no iframes match, return null
function run_in_iframes(elem, func) { function replace_elem_frame(elem, func) {
for (let i = 0; i < window.frames.length; ++i) { for (let i = 0; i < window.frames.length; ++i) {
if (iframe_same_domain(window.frames[i])) { if (iframe_same_domain(window.frames[i])) {
const frame = window.frames[i]; const frame = window.frames[i];
@ -239,6 +239,20 @@ window._qutebrowser.webelem = (function() {
return null; return null;
} }
// Runs a function in a frame until the result is not null, then return
function run_frames(func) {
for (let i = 0; i < window.frames.length; ++i) {
if (iframe_same_domain(window.frames[i])) {
const frame = window.frames[i];
const result = func(frame);
if (result) {
return result;
}
}
}
return null;
}
funcs.find_focused = () => { funcs.find_focused = () => {
const elem = document.activeElement; const elem = document.activeElement;
@ -249,7 +263,7 @@ window._qutebrowser.webelem = (function() {
} }
// Check if we got an iframe, and if so, recurse inside of it // Check if we got an iframe, and if so, recurse inside of it
const frame_elem = run_in_iframes(elem, const frame_elem = replace_elem_frame(elem,
(frame) => serialize_elem(frame.document.activeElement, frame)); (frame) => serialize_elem(frame.document.activeElement, frame));
if (frame_elem === null) { if (frame_elem === null) {
@ -263,7 +277,7 @@ window._qutebrowser.webelem = (function() {
// Check if we got an iframe, and if so, recurse inside of it // Check if we got an iframe, and if so, recurse inside of it
const frame_elem = run_in_iframes(elem, const frame_elem = replace_elem_frame(elem,
(frame) => { (frame) => {
// Subtract offsets due to being in an iframe // Subtract offsets due to being in an iframe
const frame_offset_rect = const frame_offset_rect =
@ -282,10 +296,22 @@ window._qutebrowser.webelem = (function() {
// Function for returning a selection to python (so we can click it) // Function for returning a selection to python (so we can click it)
funcs.find_selected_link = () => { funcs.find_selected_link = () => {
const elem = window.getSelection().anchorNode; const elem = window.getSelection().anchorNode;
if (!elem) { if (elem) {
return null; return serialize_elem(elem.parentNode);
} }
return serialize_elem(elem.parentNode);
const serialized_frame_elem = run_frames((frame) => {
const node = frame.window.getSelection().anchorNode;
if (node) {
return serialize_elem(node.parentNode, frame);
}
return null;
});
if (serialized_frame_elem) {
return serialized_frame_elem;
}
return null;
}; };
funcs.set_value = (id, value) => { funcs.set_value = (id, value) => {