I am writing Firefox extension using C++.
I am enumerating the tabs to get their respective URLs.
Following is pseudocode:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
nsCOMPtr<nsIWindowMediator> windowMediator =
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
windowMediator->GetMostRecentWindow(L"navigator:browser",
getter_AddRefs(dwi));
dwi->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
doc->GetElementById(NS_LITERAL_STRING("content"),
getter_AddRefs(domEl));
nsCOMPtr<nsIDOMElement> pAnoEl;
xbl->GetAnonymousElementByAttribute(
domEl,
NS_LITERAL_STRING("anonid"),
NS_LITERAL_STRING("tabcontainer"),
getter_AddRefs(pAnoEl)
);
nsString retval;
PRBool bRet = 0;
nsCOMPtr<nsIDOMNodeList> nodeList;
pAnoEl->GetChildNodes(getter_AddRefs(nodeList));
nsCOMPtr<nsIDOMNode> domNode;
rv = nodeList->GetLength(&len);
for (PRUint32 i = 0; i < len; i++)
{
nodeList->Item(i, getter_AddRefs(domNode));
nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(domNode);
nsCOMPtr<nsIBoxObject> boxObject;
xulElement->GetBoxObject(getter_AddRefs(boxObject));
nsCOMPtr<nsIBrowserBoxObject> browserboxObject =
do_QueryInterface(boxObject);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
In the above code QueryInterface for getting nsIBrowserBoxObject fails
with error code: NS_ERROR_NO_INTERFACE.
Please suggest me the ways to get tab URL.
Help Me !!!
Thanks,
Vaibhav.
If you bring up DOM inspector then you will notice that the
'browsers/tabs' are actually located under the 'xul:panels' node which
has the anonid value of "panelcontainer" and not "tabcontainer". You
passed "tabcontainer" in to "GetAnonymousElementByAttribute" which is
not correct. You will also notice that this element contains multiple
'xul:notificationbox' elements (one for each tab) and that the
'xul:browser' node is the last child of the 'xul:notificationbox'.
The 'xul:browser" node is what you are looking for.
Here is the updated code.
nsresult rv;
nsCOMPtr<nsIWindowMediator> windowMediator =
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
nsCOMPtr<nsIDOMWindowInternal> dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser", getter_AddRefs(dwi));
nsCOMPtr<nsIDOMDocument> doc;
dwi->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
nsCOMPtr<nsIDOMElement> domEl;
doc->GetElementById(NS_LITERAL_STRING("content"), getter_AddRefs(domEl));
nsCOMPtr<nsIDOMElement> pAnoEl;
// getting xul:tabpanels
xbl->GetAnonymousElementByAttribute(domEl,
NS_LITERAL_STRING("anonid"), NS_LITERAL_STRING("panelcontainer"),
getter_AddRefs(pAnoEl) );
nsString retval;
PRBool bRet = 0;
nsCOMPtr<nsIDOMNodeList> nodeList;
pAnoEl->GetChildNodes(getter_AddRefs(nodeList));
nsCOMPtr<nsIDOMNode> domNode;
PRUint32 len = 0;
rv = nodeList->GetLength(&len);
for( PRUint32 i=0; i<len; i++ )
{
// getting the xul::notificationbox
nsCOMPtr<nsIDOMNode> domNode;
nodeList->Item(i, getter_AddRefs(domNode));
// get the last child of the 'xul::notificationbox' which is the 'xul:browser'
nsCOMPtr<nsIDOMNode> domXULBrowser;
domNode->GetLastChild(getter_AddRefs(domXULBrowser));
nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(domXULBrowser);
nsCOMPtr<nsIBoxObject> boxObject;
xulElement->GetBoxObject(getter_AddRefs(boxObject));
nsCOMPtr<nsIBrowserBoxObject> browserboxObject = do_QueryInterface(boxObject);
nsCOMPtr<nsIDocShell> docShell;
browserboxObject->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(docShell);
nsCOMPtr<nsIURI> uri;
webNav->GetCurrentURI(getter_AddRefs(uri));
nsCString path;
uri->GetAsciiSpec(path);
}
If you do this early in the stage then the uri might be NULL or the
URL will most likely be about:blank.
I suggest that you add "TabOpen", "TabSelect" "TabXXXX" event
listeners if you need to track tab creation, destruction etc.
nsCOMPtr<nsIDOMEventTarget> eventTarget = do_QueryInterface(dwi);
eventTarget->AddEventListener(.....)
Remember to check for null pointers etc..
Cheers,
Jens Sorensen
nsresult rv;
nsCOMPtr<nsIWindowMediator> windowMediator =
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
nsCOMPtr<nsIDOMWindowInternal> dwi;
windowMediator->GetMostRecentWindow(L"navigator:browser", getter_AddRefs(dwi));
nsCOMPtr<nsIDOMDocument> doc;
dwi->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc));
nsCOMPtr<nsIDOMElement> domEl;
doc->GetElementById(NS_LITERAL_STRING("content"), getter_AddRefs(domEl));
nsCOMPtr<nsIDOMElement> pAnoEl;
// getting xul:tabpanels
xbl->GetAnonymousElementByAttribute(domEl,
NS_LITERAL_STRING("anonid"), NS_LITERAL_STRING("panelcontainer"),
getter_AddRefs(pAnoEl) );
nsString retval;
PRBool bRet = 0;
nsCOMPtr<nsIDOMNodeList> nodeList;
pAnoEl->GetChildNodes(getter_AddRefs(nodeList));
nsCOMPtr<nsIDOMNode> domNode;
PRUint32 len = 0;
rv = nodeList->GetLength(&len);
for( PRUint32 i=0; i<len; i++ )
{
// getting the xul::notificationbox
nsCOMPtr<nsIDOMNode> domNode;
nodeList->Item(i, getter_AddRefs(domNode));
// get the last child of the 'xul::notificationbox' which is the 'xul:browser'
nsCOMPtr<nsIDOMNode> domXULBrowser;
domNode->GetLastChild(getter_AddRefs(domXULBrowser));
nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(domXULBrowser);
nsCOMPtr<nsIBoxObject> boxObject;
xulElement->GetBoxObject(getter_AddRefs(boxObject));
nsCOMPtr<nsIBrowserBoxObject> browserboxObject = do_QueryInterface(boxObject);
nsCOMPtr<nsIDocShell> docShell;
browserboxObject->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(docShell);
nsCOMPtr<nsIURI> uri;
webNav->GetCurrentURI(getter_AddRefs(uri));
nsCString path;
uri->GetAsciiSpec(path);
}
> _______________________________________________
> dev-tech-dom mailing list
> dev-te...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-dom
>
Hi Jens,
Thanks a lot.
It is a solution.
Vaibhav.