fix(page-controller): honor viewportExpansion in DOM extraction

This commit is contained in:
fancy
2026-03-10 12:28:54 +08:00
parent 222bbef670
commit 16da7d936d
3 changed files with 11 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ import {
scrollVertically, scrollVertically,
selectOptionElement, selectOptionElement,
} from './actions' } from './actions'
import { VIEWPORT_EXPANSION } from './constants' import { resolveViewportExpansion } from './constants'
import * as dom from './dom' import * as dom from './dom'
import type { FlatDomTree, InteractiveElementDomNode } from './dom/dom_tree/type' import type { FlatDomTree, InteractiveElementDomNode } from './dom/dom_tree/type'
import { getPageInfo } from './dom/getPageInfo' import { getPageInfo } from './dom/getPageInfo'
@@ -24,7 +24,6 @@ import { patchReact } from './patches/react'
* Configuration for PageController * Configuration for PageController
*/ */
export interface PageControllerConfig extends dom.DomConfig { export interface PageControllerConfig extends dom.DomConfig {
viewportExpansion?: number
/** Enable visual mask overlay during operations (default: false) */ /** Enable visual mask overlay during operations (default: false) */
enableMask?: boolean enableMask?: boolean
} }
@@ -131,7 +130,7 @@ export class PageController extends EventTarget {
const url = window.location.href const url = window.location.href
const title = document.title const title = document.title
const pi = getPageInfo() const pi = getPageInfo()
const viewportExpansion = this.config.viewportExpansion ?? VIEWPORT_EXPANSION const viewportExpansion = resolveViewportExpansion(this.config.viewportExpansion)
await this.updateTree() await this.updateTree()

View File

@@ -14,3 +14,7 @@
*/ */
// export const VIEWPORT_EXPANSION = 100 // export const VIEWPORT_EXPANSION = 100
export const VIEWPORT_EXPANSION = -1 export const VIEWPORT_EXPANSION = -1
export function resolveViewportExpansion(viewportExpansion?: number): number {
return viewportExpansion ?? VIEWPORT_EXPANSION
}

View File

@@ -1,4 +1,4 @@
import { VIEWPORT_EXPANSION } from '../constants' import { resolveViewportExpansion } from '../constants'
import domTree from './dom_tree/index.js' import domTree from './dom_tree/index.js'
import { import {
ElementDomNode, ElementDomNode,
@@ -8,6 +8,7 @@ import {
} from './dom_tree/type' } from './dom_tree/type'
export interface DomConfig { export interface DomConfig {
viewportExpansion?: number
interactiveBlacklist?: (Element | (() => Element))[] interactiveBlacklist?: (Element | (() => Element))[]
interactiveWhitelist?: (Element | (() => Element))[] interactiveWhitelist?: (Element | (() => Element))[]
includeAttributes?: string[] includeAttributes?: string[]
@@ -21,6 +22,8 @@ export interface DomConfig {
const newElementsCache = new WeakMap<HTMLElement, string>() const newElementsCache = new WeakMap<HTMLElement, string>()
export function getFlatTree(config: DomConfig): FlatDomTree { export function getFlatTree(config: DomConfig): FlatDomTree {
const viewportExpansion = resolveViewportExpansion(config.viewportExpansion)
const interactiveBlacklist = [] as Element[] const interactiveBlacklist = [] as Element[]
for (const item of config.interactiveBlacklist || []) { for (const item of config.interactiveBlacklist || []) {
if (typeof item === 'function') { if (typeof item === 'function') {
@@ -43,7 +46,7 @@ export function getFlatTree(config: DomConfig): FlatDomTree {
doHighlightElements: true, doHighlightElements: true,
debugMode: true, debugMode: true,
focusHighlightIndex: -1, focusHighlightIndex: -1,
viewportExpansion: VIEWPORT_EXPANSION, viewportExpansion,
interactiveBlacklist, interactiveBlacklist,
interactiveWhitelist, interactiveWhitelist,
highlightOpacity: config.highlightOpacity ?? 0.0, highlightOpacity: config.highlightOpacity ?? 0.0,