#!/usr/bin/python3

import pyatspi

def urisFromHyperlink(obj):
    try:
        hyperlink = obj.queryHyperlink()
    except NotImplementedError:
        print("Hyperlink interface not implemented")
        return

    URIs = [hyperlink.getURI(x) for x in range(hyperlink.get_n_anchors())]
    print("URIs via hyperlink: %s" % URIs)

def urisFromHypertext(obj):
    try:
        hypertext = obj.queryHypertext()
    except NotImplementedError:
        print("Hypertext interface not implemented")
        return

    links = [hypertext.getLink(x) for x in range(hypertext.getNLinks())]
    URIs = [link.getURI(x) for link in links for x in range(link.get_n_anchors())]
    print("URIs via hypertext: %s" % URIs)

def listener(e):
    if not e.detail1:
        return

    print("\n%s" % e.source.getRoleName())
    urisFromHyperlink(e.source)
    urisFromHypertext(e.source)

pyatspi.Registry.registerEventListener(listener, "object:state-changed:focused")
pyatspi.Registry.start()
