My XulFile is very simple where the interesting part is this line
declaring the browser:
<browser id="mybrowser1" src="http://www.google.dk" flex="1" />
The C++ code is like this:
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;
// -----------------------------
// --- 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) );
// --- Add events ---
nsCOMPtr<nsIDOMEventTarget>
pDomEventTarget(do_QueryInterface(pDomWin));
pDomEventTarget-
>AddEventListener(NS_LITERAL_STRING("DOMContentLoaded"), this,
PR_FALSE);
pDomEventTarget->AddEventListener(NS_LITERAL_STRING("resize"),
this, PR_FALSE);
pDomEventTarget->AddEventListener(NS_LITERAL_STRING("unload"),
this, PR_FALSE);
}
>I would like to get the "DOMContentLoaded" event from C++. It works from javascript, but not from C++. However it seems I more or less have the correct event target since "resize" and "unload" event does work.
>
Chances are you did attach the event handler to the window, but
unfortunately loading a new document clears all extant event handlers.
In JavaScript you've probably been attaching a capturing event handler
to the browser element instead.
--
Warning: May contain traces of nuts.
Ok. Any idea how I get the browser element ?
Is it one of those I already have interfaces to? Anyway I found the
code in the source for Songbird, but they do not listen to the
"DOMContentLoaded" so you're probably right that I am attaching event
listneners to the wrong element.
Thanks a lot
>Ok. Any idea how I get the browser element ?
>
>
(Using abbreviated variable names to save space)
nsCOMPtr<nsIDOMDocument> doc;
win->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMElement> elt;
doc->GetElementById(NS_LITERAL_STRING("browser1"), getter_AddRefs(elt));
nsCOMPtr<nsIDOMEventTarget> tgt(do_QueryInterface(elt);
tgt->AddEventListener(NS_LITERAL_STRING("DOMContentLoaded"), this, PR_TRUE);
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;
nsCOMPtr<nsIWindowMediator>
pWindowMediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
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() );
}
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;
nsCOMPtr<nsIWindowMediator>
pWindowMediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
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);
// ---------------------------------------------------------------