GLib has a very flexible signals system. Use it. The attach_window function has an EphyWindow argument. As an example, the switch_page signal will be fired on a window's tab whenever the tab changes. Put the following in attach_window to hook the switch_page signal to your own callback:
Example 4.1. Connecting the switch_page signal
def _switch_page_cb(notebook, page, page_num, data):
print data
def attach_window(window):
notebook = window.get_notebook()
sig = notebook.connect('switch_page', _switch_page_cb, 'Foo')
notebook._example_extension_sig = sig
def detach_window(window):
notebook = window.get_notebook()
sig = notebook._example_extension_sig
notebook.disconnect(sig)
del notebook._example_extension_sig
tab_added can connect signals of its own. Of particular interest are the EphyEmbed signals (prefixed by ge_). These signals are listed in epiphany/embed/ephy-embed.c. For example, to call a function when the browser changes to a new location:
Example 4.2. Listening to location changes
def _location_cb(embed, address):
print 'New location: %s' % address
def attach_tab(window, tab):
embed = tab.get_embed()
sig = embed.connect('ge-location', _location_cb)
embed._python_sample_location_sig = sig
def detach_tab(window, tab):
embed = tab.get_embed()
sig = embed._python_sample_location_sig
del embed._python_sample_location_sig
embed.disconnect(sig)
Free all memory and disconnect all signals when they're not needed. In the case of this ge_location signal connection, disconnect the signal when the tab is removed.
Python's garbage collector frees memory properly, but it does not disconnect signals; you must disconnect signals.