Discussion:
How to get currently selected tab index and its content
veeru
2010-03-29 10:18:12 UTC
Permalink
Hi,

I am developing an XPCOM component in c++. I am able to get number of
tabs currently opened using XBL. Please see the code below:

////////////////////////////////////////////C O D
E//////////////////////////////////////////
nsresult rv;
nsCOMPtr<nsIWindowMediator> windowMediator
= do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
PRUint32 len;
nsString temp;

nsCOMPtr<nsIDOMDocument> curDoc;
nsCOMPtr<nsIDOMElement> domEl;
nsCOMPtr<nsIDOMNodeList> tabbrowser;
nsCOMPtr<nsIDOMDocument> doc;
nsCOMPtr<nsIDOMWindowInternal> dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
if (dwi)
{
dwi->GetDocument(getter_AddRefs(doc));
if (doc)
{
nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
if (xbl)
return NS_ERROR_FAILURE;
doc->GetElementById(NS_LITERAL_STRING("content"),
getter_AddRefs(domEl));
if (domEl)
{
nsCOMPtr<nsIDOMElement> pAnoEl;
xbl->GetAnonymousElementByAttribute(domEl,
NS_LITERAL_STRING("anonid"), NS_LITERAL_STRING("tabcontainer"),
getter_AddRefs(pAnoEl));
if (pAnoEl)
{
nsCOMPtr<nsIDOMNodeList> nodeList;
pAnoEl->GetChildNodes(getter_AddRefs(nodeList));
if (nodeList)
{
nsCOMPtr<nsIDOMNode> domNode;
nodeList->GetLength(&len);
for (PRUint32 i = 0; i < len; i++)
{
nodeList->Item(i, getter_AddRefs(domNode));
curDoc =
do_QueryInterface(domNode); // fails here.
}
}
}
}
}
}

///////////////////////////////////END OF
CODE////////////////////////////////////////

Here I am getting number of tab, even I am able to set/get attributes.
But I am able to decide which is the currently selected tab. I tried
to use property and method from tabbrowser.xml, but failed.

Also I want to get the content of currently selected tab. But I tried
to QI nsIDOMNode for nsIDOMDocument, it failed. But in dependency
diagram on mozilla site, it is given that nsIDOMNode is derived form
nsIDOMDocument.
How can I get the current tab content, DOM pointer and its index?
Please help me, I am stuck to it.

Thank you in advance.
Josh Matthews
2010-03-29 15:13:53 UTC
Permalink
Post by veeru
Hi,
I am developing an XPCOM component in c++. I am able to get number of
////////////////////////////////////////////C O D
E//////////////////////////////////////////
nsresult rv;
nsCOMPtr<nsIWindowMediator> windowMediator
= do_GetService(NS_WINDOWMEDIATOR_CONTRACTID,&rv);
NS_ENSURE_SUCCESS(rv,rv);
PRUint32 len;
nsString temp;
nsCOMPtr<nsIDOMDocument> curDoc;
nsCOMPtr<nsIDOMElement> domEl;
nsCOMPtr<nsIDOMNodeList> tabbrowser;
nsCOMPtr<nsIDOMDocument> doc;
nsCOMPtr<nsIDOMWindowInternal> dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
if (dwi)
{
dwi->GetDocument(getter_AddRefs(doc));
if (doc)
{
nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
if (xbl)
return NS_ERROR_FAILURE;
This condition looks wrong to me, especially since you later use xbl->

Cheers,
Josh
Jens Sorensen
2010-03-29 21:23:56 UTC
Permalink
You are almost there.. You can get the Tab's document from the tabnode
(your domnode i assume) like this

:
nodeList->Item(i, getter_AddRefs(domNode));


nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(domNode);

nsCOMPtr<nsIBoxObject> boxObject;
xulElement->GetBoxObject( getter_AddRefs(boxObject);

nsCOMPtr<nsIBrowserBoxObject> browserObj = do_QueryInterface(boxObject);

nsCOMPtr<nsIDocShell> docShell;
browserObj->GetDocShell( getter_AddRefs(docShell) );

nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(docShell)

nsCOMPtr<nsIDOMDocument> doc;
domWindow->GetDocument( getter_AddRefs((spDoc) );

Remember to check return values and NULL pointers :)... Keep in mind
that some of the interfaces are unfrozen as well.

Let me know how it goes..

Cheers,
Jens
Post by Josh Matthews
Post by veeru
Hi,
I am developing an XPCOM component in c++. I am able to get number of
////////////////////////////////////////////C O D
E//////////////////////////////////////////
nsresult rv;
nsCOMPtr<nsIWindowMediator>  windowMediator
= do_GetService(NS_WINDOWMEDIATOR_CONTRACTID,&rv);
NS_ENSURE_SUCCESS(rv,rv);
PRUint32 len;
nsString temp;
nsCOMPtr<nsIDOMDocument>  curDoc;
nsCOMPtr<nsIDOMElement>  domEl;
nsCOMPtr<nsIDOMNodeList>  tabbrowser;
nsCOMPtr<nsIDOMDocument>  doc;
nsCOMPtr<nsIDOMWindowInternal>  dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
if (dwi)
{
       dwi->GetDocument(getter_AddRefs(doc));
       if (doc)
       {
               nsCOMPtr<nsIDOMDocumentXBL>  xbl(do_QueryInterface(doc));
               if (xbl)
                       return NS_ERROR_FAILURE;
This condition looks wrong to me, especially since you later use xbl->
Cheers,
Josh
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
veeru
2010-03-30 05:01:10 UTC
Permalink
Post by Jens Sorensen
You are almost there.. You can get the Tab's document from the tabnode
(your domnode i assume) like this
nodeList->Item(i, getter_AddRefs(domNode));
nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(domNode);
nsCOMPtr<nsIBoxObject> boxObject;
xulElement->GetBoxObject( getter_AddRefs(boxObject);
nsCOMPtr<nsIBrowserBoxObject> browserObj = do_QueryInterface(boxObject);
nsCOMPtr<nsIDocShell> docShell;
browserObj->GetDocShell( getter_AddRefs(docShell) );
nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(docShell)
nsCOMPtr<nsIDOMDocument> doc;
domWindow->GetDocument( getter_AddRefs((spDoc) );
Remember to check return values and NULL pointers :)... Keep in mind
that some of the interfaces are unfrozen as well.
Let me know how it goes..
Cheers,
Jens
Post by Josh Matthews
Post by veeru
Hi,
I am developing an XPCOM component in c++. I am able to get number of
////////////////////////////////////////////C O D
E//////////////////////////////////////////
nsresult rv;
nsCOMPtr<nsIWindowMediator>  windowMediator
= do_GetService(NS_WINDOWMEDIATOR_CONTRACTID,&rv);
NS_ENSURE_SUCCESS(rv,rv);
PRUint32 len;
nsString temp;
nsCOMPtr<nsIDOMDocument>  curDoc;
nsCOMPtr<nsIDOMElement>  domEl;
nsCOMPtr<nsIDOMNodeList>  tabbrowser;
nsCOMPtr<nsIDOMDocument>  doc;
nsCOMPtr<nsIDOMWindowInternal>  dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
if (dwi)
{
       dwi->GetDocument(getter_AddRefs(doc));
       if (doc)
       {
               nsCOMPtr<nsIDOMDocumentXBL>  xbl(do_QueryInterface(doc));
               if (xbl)
                       return NS_ERROR_FAILURE;
This condition looks wrong to me, especially since you later use xbl->
Cheers,
Josh
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Hi Jens,

NULL pointer is returned at the following line

sCOMPtr<nsIBrowserBoxObject> browserObj =
do_QueryInterface(boxObject);

Thus I am not able to move forward.

Also I've implemented nsIWebProgressListener to listen on the tab ie.
on STATE_START and STATE_STOP in OnStateChange() method.
In this function, I want to determine for which tab STATE_START or
STATE_STOP has occured. But I am not able to do so. Here is the code
where I am listening on event

/////////////////// CODE START //////////////////////////////

NS_IMETHODIMP CTestComponent::OnStateChange(nsIWebProgress
*aWebProgress,
nsIRequest *aRequest,PRUint32
aStateFlags,nsresult aStatus)
{
nsresult rv;

if ((aStateFlags & STATE_START | STATE_STOP) && (aStateFlags &
STATE_IS_WINDOW))
{
nsCOMPtr<nsIDOMWindow> domWindow;
rv = aWebProgress->GetDOMWindow(getter_AddRefs(domWindow));
if (FAILED(rv))
{
return NS_ERROR_FAILURE;
}

if(domWindow)
{
// Here how to determine for which tab the
event has occurred. Mean either getting tab reference or its index.
}
}

return NS_OK;
}

///////////////////////////////// CODE
END ////////////////////////////////////

Please help me out. I am stuck to it badly.

Continue reading on narkive:
Loading...