Merge pull request #2215 from mozbugbox/speedup-hinting

Speedup hinting
This commit is contained in:
Oliver Blanthorn 2020-03-08 14:07:33 +00:00 committed by GitHub
commit 9f137c3c3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View file

@ -32,7 +32,6 @@ import {
islice,
izip,
map,
unique,
} from "@src/lib/itertools"
import { contentState } from "@src/content/state_content"
import * as config from "@src/lib/config"
@ -779,8 +778,8 @@ function pushSpace() {
export function hintables(selectors = DOM.HINTTAGS_selectors, withjs = false) {
let elems = DOM.getElemsBySelector(selectors, [])
if (withjs) {
elems = elems.concat(DOM.hintworthy_js_elems)
elems = unique(elems)
const elemSet = new Set([...elems, ...DOM.hintworthy_js_elems])
elems = [...elemSet]
}
return elems.filter(DOM.isVisible)
}

View file

@ -219,14 +219,19 @@ export function isVisible(element: Element) {
element = element.parentElement
}
const clientRect = element.getBoundingClientRect()
const computedStyle = getComputedStyle(element)
// remove elements that are barely within the viewport, tiny, or invisible
switch (true) {
case !clientRect:
case clientRect.bottom < 4:
case clientRect.top >= innerHeight - 4:
case clientRect.right < 4:
case clientRect.left >= innerWidth - 4:
return false
}
// remove elements that are barely within the viewport, tiny, or invisible
// Only call getComputedStyle when necessary
const computedStyle = getComputedStyle(element)
switch (true) {
case widthMatters(computedStyle) && clientRect.width < 3:
case heightMatters(computedStyle) && clientRect.height < 3:
case computedStyle.visibility !== "visible":
@ -388,7 +393,7 @@ export function compareElementArea(a: HTMLElement, b: HTMLElement): number {
return aArea - bArea
}
export const hintworthy_js_elems = []
export const hintworthy_js_elems: Set<Element> = new Set()
/** Adds or removes an element from the hintworthy_js_elems array of the
* current tab.
@ -444,14 +449,13 @@ export function registerEvListenerAction(
case "mouseup":
case "mouseover":
if (add) {
hintworthy_js_elems.push(elem)
hintworthy_js_elems.add(elem)
} else {
// Possible bug: If a page adds an event listener for "click" and
// "mousedown" and removes "mousedown" twice, we lose track of the
// elem even though it still has a "click" listener.
// Fixing this might not be worth the added complexity.
const index = hintworthy_js_elems.indexOf(elem)
if (index >= 0) hintworthy_js_elems.splice(index, 1)
hintworthy_js_elems.delete(elem)
}
}
}