Skip to content

API reference

Functions

browsermem.current_url(browsers=None)

URL of the active tab, or None if no browser is open or it can't be resolved.

Shortcut for get_active_tab().url.

browsermem.get_active_tab(browsers=None)

Active tab of the focused (or most recently focused) browser window.

Returns None when no matching browser window is open.

browsermem.get_tab(window)

Active tab of a specific window (a :class:BrowserWindow or an HWND).

Raises :class:NotFoundError if the handle is not a supported browser window.

browsermem.get_all_tabs(browsers=None)

The active tab of every open browser window.

browsermem.list_windows(browsers=None)

All browser windows, most recently active first. Does not read memory.

browsermem.dump_urls(pid=0)

Every URL string in a process's memory, most frequent first.

pid=0 picks the most recently active browser.

browsermem.version()

Version of the native library.

Live monitoring

browsermem.TabMonitor

Watches browser windows and reports tab changes together with their URL.

Consume events in whichever style fits your program (they can be combined)::

# callback
TabMonitor(lambda tab: print(tab.url)).run_forever()

# decorators, per event kind
monitor = TabMonitor()

@monitor.on(EventKind.TAB_CHANGED)
def changed(tab): ...

# blocking iterator
with TabMonitor() as monitor:
    for tab in monitor.events():
        ...

# asyncio
async with TabMonitor() as monitor:
    async for tab in monitor.aevents():
        ...

Callbacks run on a native worker thread: keep them short, or hand work off to your own thread / event loop.

__init__(callback=None, *, browsers=None, all_windows=False, debounce_ms=600, rescan_ms=0, cache_ttl_ms=30000, emit_unresolved=True, queue_size=1000)

Parameters:

Name Type Description Default
callback Optional[Handler]

Called for every event.

None
browsers BrowserFilter

Only watch these browsers (default: all supported).

None
all_windows bool

Also report changes in background browser windows.

False
debounce_ms int

How long a window caption must be stable before it is reported.

600
rescan_ms int

Periodically rescan the focused tab to catch navigations that keep the same title (0 = off; a rescan usually takes a few ms).

0
cache_ttl_ms int

Reuse a result while a window's caption is unchanged.

30000
emit_unresolved bool

Deliver events even when no URL could be resolved.

True
queue_size int

Capacity of the buffer behind :meth:events.

1000

on(*kinds)

Decorator registering a handler for the given event kinds (all if none).

on_url(pattern, *kinds)

Decorator: call the handler only when the tab URL matches a regex (re.search)::

@monitor.on_url(r"youtube.com/watch") def video(tab): ...

on_domain(*domains)

Decorator: call the handler only for tabs on these domains (and subdomains)::

@monitor.on_domain("github.com", "gitlab.com") def code_hosting(tab): ...

wait_for(match=None, timeout=None)

Block until a tab event matches, then return it (None on timeout).

match is a URL regex, a predicate, or None for the next event with a URL. Starts the monitor if needed::

tab = monitor.wait_for(r"/checkout/complete", timeout=300)

remove_handler(fn)

Unregister a handler (including ones added with on_url / on_domain).

start()

Start monitoring (idempotent). The current tab is reported as a SNAPSHOT event.

stop()

Stop monitoring (idempotent). Safe to call from inside a handler.

events(timeout=None)

Yield events until :meth:stop is called, or until timeout seconds pass without an event. Ctrl+C works while waiting.

aevents() async

Async iterator over events, delivered on the running event loop.

run_forever()

Start, dispatch events to handlers until Ctrl+C, then stop.

Activity tracking

browsermem.TabHistory

Records which URL was active, when, and for how long::

monitor = TabMonitor()
history = TabHistory(monitor)
monitor.start()
...
for domain, spent in history.time_by_domain().items():
    print(domain, spent)

A new visit starts whenever the active URL changes (tab switch, navigation, switching to another browser window). Time spent outside the browser is counted towards the last visit until the browser reports a new event, so durations are an upper bound.

visits property

All recorded visits, oldest first (a copy).

current property

The visit in progress, if any.

attach(monitor)

Start recording events from monitor.

record(tab)

Feed one event (called automatically when attached to a monitor).

time_by_domain()

Total time per domain, longest first.

to_list()

JSON-friendly list of visits.

browsermem.Visit dataclass

A stretch of time during which one URL was the active tab of a window.

duration property

Time on the page so far (or in total, once the visit has ended).

Data types

browsermem.Tab dataclass

The active tab of a browser window, as recovered from memory.

resolved property

True if a URL was found.

scheme property

"https", "chrome", "file", ... (None if unresolved).

host property

Lower-cased host name, e.g. "www.github.com".

domain property

Host name without a leading www., e.g. "github.com".

is_web property

True for http/https pages (False for chrome://newtab, files, ...).

matches_domain(*domains)

True if the tab is on one of domains or a subdomain of one.

to_dict()

JSON-friendly representation.

browsermem.BrowserWindow dataclass

A top-level browser window (found without reading memory).

browsermem.UrlCount

Bases: NamedTuple

A URL found in memory and how many copies of it exist.

browsermem.Browser

Bases: IntEnum

Supported browsers (values match BM_BROWSER_*).

parse(value) classmethod

Browser.parse("edge") -> Browser.EDGE.

browsermem.EventKind

Bases: IntEnum

Why a :class:Tab was produced (values match BM_EVENT_*).

Forensics

browsermem.MemoryImage

A captured or reconstructed process memory image.

__init__()

Create an empty image; add memory with :meth:add_region.

capture(pid=0) classmethod

Copy the private read/write memory of a running process.

pid=0 picks the most recently active browser.

from_regions(regions) classmethod

Build an image from (base_address, data) pairs.

from_minidump(path, *, private_only=True) classmethod

Load a Windows minidump (.dmp), e.g. from Task Manager → Create dump file, ProcDump, or :func:write_minidump. Needs a full-memory dump.

With private_only (default) only private, writable memory is loaded when the dump records region types -- the same memory a live capture reads.

add_region(base, data)

Add one memory region (copied). Regions must not overlap.

urls()

Every URL string in the image, most frequent first.

resolve(title, browser=None)

URL of the tab whose title is title.

If browser is given, title may also be a full window caption ("Inbox - Gmail - Google Chrome"). Check tab.resolved.

browsermem.forensics.write_minidump(pid, path)

Write a full-memory minidump of a running process (like Task Manager's Create dump file), for later analysis with :meth:MemoryImage.from_minidump.

browsermem.forensics.read_minidump_regions(path, *, private_only=True)

Yield (base_address, data) for the memory ranges stored in a minidump.

Exceptions

browsermem.BrowserMemError

Bases: Exception

Base class for all browsermem errors. status is the native status code.

browsermem.NotFoundError

Bases: BrowserMemError

No matching browser window or process.

browsermem.AccessDeniedError

Bases: BrowserMemError

The target process could not be opened or read (e.g. it runs elevated).

browsermem.StateError

Bases: BrowserMemError

The call is not allowed in the current state.

C API

The native library (browsermem.dll) has a plain C interface, so you can also use it from C, C++, C#, Rust or Go. The full contract is in native/include/browsermem.h.