Skip to content

Commit c9a3ffe

Browse files
authored
Handle fields hidden with an opacity rule (#85)
1 parent 818a552 commit c9a3ffe

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/inject.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@
224224
* @return array List of search results
225225
*/
226226
function queryAllVisible(parent, field, form) {
227-
var result = [];
228-
for (var i = 0; i < field.selectors.length; i++) {
229-
var elems = parent.querySelectorAll(field.selectors[i]);
230-
for (var j = 0; j < elems.length; j++) {
231-
var elem = elems[j];
227+
const result = [];
228+
for (let i = 0; i < field.selectors.length; i++) {
229+
let elems = parent.querySelectorAll(field.selectors[i]);
230+
for (let j = 0; j < elems.length; j++) {
231+
let elem = elems[j];
232232
// Select only elements from specified form
233233
if (form && form != elem.form) {
234234
continue;
@@ -247,19 +247,35 @@
247247
continue;
248248
}
249249
// Elem takes space on the screen, but it or its parent is hidden with a visibility style.
250-
var style = window.getComputedStyle(elem);
250+
let style = window.getComputedStyle(elem);
251251
if (style.visibility == "hidden") {
252252
continue;
253253
}
254254
// Elem is outside of the boundaries of the visible viewport.
255-
var rect = elem.getBoundingClientRect();
255+
let rect = elem.getBoundingClientRect();
256256
if (
257257
rect.x + rect.width < 0 ||
258258
rect.y + rect.height < 0 ||
259259
(rect.x > window.innerWidth || rect.y > window.innerHeight)
260260
) {
261261
continue;
262262
}
263+
// Elem is hidden by its or or its parent's opacity rules
264+
const OPACITY_LIMIT = 0.1;
265+
let opacity = 1;
266+
for (
267+
let testElem = elem;
268+
opacity >= OPACITY_LIMIT && testElem && testElem.nodeType === Node.ELEMENT_NODE;
269+
testElem = testElem.parentNode
270+
) {
271+
let style = window.getComputedStyle(testElem);
272+
if (style.opacity) {
273+
opacity *= parseFloat(style.opacity);
274+
}
275+
}
276+
if (opacity < OPACITY_LIMIT) {
277+
continue;
278+
}
263279
// This element is visible, will use it.
264280
result.push(elem);
265281
}

0 commit comments

Comments
 (0)