pagescript

package
v0.94.0-rc.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pagescript holds the scripts that read a page for the browser pane's notes, run as they are by every engine that draws a page: Chrome for the agent's page, and the web view the window holds itself.

Index

Constants

View Source
const FrameSeparator = " >>> "

FrameSeparator joins a frame's selector to the selector of an element inside it. No CSS selector holds it, so splitting on it is unambiguous.

View Source
const Frames = `const visible = ` + visibleScript + `;
	const content = f => {
		const r = f.getBoundingClientRect(), s = f.ownerDocument.defaultView.getComputedStyle(f);
		const pl = parseFloat(s.paddingLeft), pt = parseFloat(s.paddingTop);
		return {x: r.left + f.clientLeft + pl, y: r.top + f.clientTop + pt,
			w: f.clientWidth - pl - parseFloat(s.paddingRight), h: f.clientHeight - pt - parseFloat(s.paddingBottom)};
	};
	const offset = frames => frames.reduce((o, f) => {
		const c = content(f);
		return {x: o.x + c.x, y: o.y + c.y};
	}, {x: 0, y: 0});
	const inside = f => {
		const doc = /^i?frame$/i.test(f.tagName) ? f.contentDocument : null;
		return doc && doc.documentElement ? doc : null;
	};
	const framed = (e, frames) => {
		let b = visible(e);
		for (let i = frames.length - 1; i >= 0 && b.w > 0; i--) {
			const c = content(frames[i]), v = visible(frames[i]);
			const x0 = Math.max(b.x + c.x, c.x, v.x), y0 = Math.max(b.y + c.y, c.y, v.y);
			const x1 = Math.min(b.x + b.w + c.x, c.x + c.w, v.x + v.w), y1 = Math.min(b.y + b.h + c.y, c.y + c.h, v.y + v.h);
			b = x1 <= x0 || y1 <= y0 ? {x: 0, y: 0, w: 0, h: 0} :
				{x: Math.round(x0), y: Math.round(y0), w: Math.round(x1 - x0), h: Math.round(y1 - y0)};
		}
		return b;
	};
	const find = s => {
		const frames = [];
		let doc = document;
		for (const p of s.split(` + "'" + FrameSeparator + "'" + `)) {
			if (!doc) return null;
			const e = doc.querySelector(p);
			if (!e) return null;
			frames.push(e);
			doc = inside(e);
		}
		return {e: frames.pop(), frames};
	};
	const deepest = (x, y) => {
		const frames = [];
		let doc = document, e;
		for (;;) {
			e = doc.elementFromPoint(x, y);
			while (e && e.shadowRoot) {
				const inner = e.shadowRoot.elementFromPoint(x, y);
				if (!inner || inner === e) break;
				e = inner;
			}
			const inner = e && inside(e);
			if (!inner) return {e, frames};
			const c = content(e);
			x -= c.x; y -= c.y;
			frames.push(e);
			doc = inner;
		}
	};
	const documents = () => {
		const all = [];
		const walk = (doc, frames) => {
			all.push({doc, frames});
			for (const f of doc.querySelectorAll('iframe, frame')) {
				const inner = inside(f);
				if (inner) walk(inner, [...frames, f]);
			}
		};
		walk(document, []);
		return all;
	};
	const focused = () => {
		let e = document.activeElement;
		while (e && inside(e) && inside(e).activeElement) e = inside(e).activeElement;
		return e;
	};`

Frames declares what a script needs to see into same-origin frames, whose documents a page's own script reaches; a cross-origin one stays the frame element. A frame is an iframe or frame element, and frames are a path of them, outermost first.

  • visible(e): visibleScript, in e's own document.
  • content(f): the viewport box of f's content, where its document starts.
  • offset(frames): where the innermost frame's viewport starts in the top one.
  • framed(e, frames): visible(e) in the top viewport, cut to each frame's content and to what shows of the frame itself.
  • find(s): {e, frames} a selector joined by FrameSeparator names, null where any step is gone. An invalid selector throws, as querySelector.
  • deepest(x, y): {e, frames} under a point of the top viewport, through open shadow roots and same-origin frames.
  • documents(): every same-origin document of the page, {doc, frames}, the top one first.
  • focused(): the element that holds the focus, inside frames too.

Variables

This section is empty.

Functions

func ElementAt

func ElementAt(x, y float64) string

ElementAt is the call of elementScript at x,y, CSS pixels of the viewport. It answers the element as api.BrowserElement's JSON, or null.

func Locate

func Locate(selectors []string, reveal string) (string, error)

Locate is the call of locateScript for selectors, first revealing reveal's element. It answers a list of api.BrowserRect's JSON.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL