Hi,
eventListener "afterOpen" is triggered twice - after doc opening and after applying a window to it.
In 1st case you are seeing an error since no window is applied yet.
You could check an event.target.constructor.name and run your function body if it is !="Document"
(just with 2nd case only)
Jarek