Hi Neil
I'll try your way too. Again Thanks a lot :)
Also found a way myself which seems to work:
I found the solution. Just need to get the Parent windows of the one I
wa using. Except if you want the "unload" event which seems to only
work for the window I was first using. Here goes the complete updated
code for those who might have had the same troubles as I.
Thanks again to Neil for his great help.
void CSpecialThing::getBrowserQuestionForForum()
{
nsString wsHrefXul = NS_LITERAL_STRING( "chrome://xulcrawler/
content/main.xul" );
nsString wsIDBrowser = NS_LITERAL_STRING( "mybrowser1" );
// --- A lot of variables ---
nsresult rv;
nsCOMPtr<nsIDOMWindowInternal> pWinInt(0);
nsCOMPtr<nsISimpleEnumerator> pWinEnumerator;
nsCOMPtr<nsIDOMLocation> pDOMLocation;
nsCOMPtr<nsIDOMWindowCollection> pDOMWindowCollection;
nsCOMPtr<nsIDOMWindow> pDomWin;
nsCOMPtr<nsIDOMWindow> pDomWinBrowser;
nsCOMPtr<nsIDOMDocument> pDomDoc;
// -----------------------------
// --- Get DomWindowInternal ---
// -----------------------------
nsCOMPtr<nsIWindowMediator>
pWindowMediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
nsString sWindowType( NS_LITERAL_STRING("") );
rv = pWindowMediator->GetEnumerator( sWindowType.get(),
getter_AddRefs(pWinEnumerator));
PRBool bHasMoreElements;
rv = pWinEnumerator->HasMoreElements( &bHasMoreElements );
while ( bHasMoreElements ) {
nsCOMPtr<nsISupports> pSupportsWin;
rv = pWinEnumerator->GetNext( getter_AddRefs(pSupportsWin) );
nsCOMPtr<nsIDOMWindowInternal>
pWinInternal(do_QueryInterface(pSupportsWin));
rv = pWinInternal-
>GetLocation( getter_AddRefs(pDOMLocation) );
nsString wsHrefTest;
rv = pDOMLocation->GetHref( wsHrefTest );
// See if we have the correct (XUL) URI
if ( wsHrefXul == wsHrefTest ) pWinInt = pWinInternal;
rv = pWinEnumerator->HasMoreElements( &bHasMoreElements );
}
// --- Get the "mybrowser1" element ---
pWinInt->GetFrames( getter_AddRefs(pDOMWindowCollection) );
pDOMWindowCollection->NamedItem( wsIDBrowser,
getter_AddRefs(pDomWin) );
pDomWin->GetParent( getter_AddRefs(pDomWinBrowser) );
// --- Add events ---
nsCOMPtr<nsIDOMEventTarget>
pDomEventTargetBrowserWin(do_QueryInterface(pDomWin));
nsCOMPtr<nsIDOMEventTarget>
pDomEventTargetBrowser(do_QueryInterface(pDomWinBrowser));
pDomEventTargetBrowser-
>AddEventListener(NS_LITERAL_STRING("DOMContentLoaded"), this,
PR_FALSE);
pDomEventTargetBrowser-
>AddEventListener(NS_LITERAL_STRING("resize"), this, PR_FALSE);
pDomEventTargetBrowserWin-
>AddEventListener(NS_LITERAL_STRING("unload"), this, PR_FALSE);
// ---------------------------------------------------------------
// --- Bonus example on how to get the current URI of the page ---
// ---------------------------------------------------------------
pDomWin->GetDocument( getter_AddRefs(pDomDoc) );
nsCOMPtr<nsIDOMDocumentView>
pDomDocumentView(do_QueryInterface(pDomDoc));
nsCOMPtr<nsIDOMAbstractView> domAbstractView =
getDefaultView(pDomDocumentView);
nsCOMPtr<nsIWebNavigation>
webNavigation(do_GetInterface(domAbstractView));
printf("webNavigation uri: %s\n",
getCurrentURI(webNavigation).c_str() );
}