Re: Getting tab URL in C++
[email protected] Thu, 3 Jan 2013 21:57:56 -0800 (PST)
| Newsgroups | gmane.comp.mozilla.devel.dom |
|---|---|
| Message-ID | <[email protected]> |
Hi Vaibhav, I am also implementing the c++ code to get all opened firefox url. Could you please send me your code. regards Pushpendra On Monday, April 12, 2010 3:33:22 PM UTC+5:30, Vaibhav wrote: > On Apr 10, 7:15=A0am, Jens Sorensen <[email protected]> wrote: > > You are almost there.. > > > > 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". =A0You > > 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. > > > > =A0 =A0 =A0 =A0 nsresult rv; > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIWindowMediator> windowMediator =3D > > do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMWindowInternal> dwi; > > =A0 =A0 =A0 =A0 windowMediator->GetMostRecentWindow(L"navigator:browser= ", getter_AddRefs(dwi)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMDocument> doc; > > =A0 =A0 =A0 =A0 dwi->GetDocument(getter_AddRefs(doc)); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc)); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMElement> domEl; > > =A0 =A0 =A0 =A0 doc->GetElementById(NS_LITERAL_STRING("content"), gette= r_AddRefs(domEl)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMElement> pAnoEl; > > > > =A0 =A0 =A0 =A0 // getting xul:tabpanels > > =A0 =A0 =A0 =A0 xbl->GetAnonymousElementByAttribute(domEl, > > NS_LITERAL_STRING("anonid"), NS_LITERAL_STRING("panelcontainer"), > > getter_AddRefs(pAnoEl) =A0); > > =A0 =A0 =A0 =A0 nsString retval; > > =A0 =A0 =A0 =A0 PRBool bRet =3D 0; > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNodeList> nodeList; > > =A0 =A0 =A0 =A0 pAnoEl->GetChildNodes(getter_AddRefs(nodeList)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domNode; > > > > =A0 =A0 =A0 =A0 PRUint32 len =3D 0; > > =A0 =A0 =A0 =A0 rv =3D nodeList->GetLength(&len); > > > > =A0 =A0 =A0 =A0 for( PRUint32 i=3D0; i<len; i++ ) > > =A0 =A0 =A0 =A0 { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // getting the xul::notificationbox > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domNode; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nodeList->Item(i, getter_AddRefs(domNod= e)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // get the last child of the 'xul::noti= ficationbox' which is the 'xul:browser' > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domXULBrowser; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 domNode->GetLastChild(getter_AddRefs(do= mXULBrowser)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMXULElement> xulElement = =3D do_QueryInterface(domXULBrowser); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIBoxObject> boxObject; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 xulElement->GetBoxObject(getter_AddRefs= (boxObject)); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIBrowserBoxObject> browserbo= xObject =3D do_QueryInterface(boxObject); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDocShell> docShell; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 browserboxObject->GetDocShell(getter_Ad= dRefs(docShell)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIWebNavigation> webNav =3D d= o_QueryInterface(docShell); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIURI> uri; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 webNav->GetCurrentURI(getter_AddRefs(ur= i)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCString path; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 uri->GetAsciiSpec(path); > > =A0 =A0 =A0 =A0 } > > > > 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 =3D do_QueryInterface(dwi); > > eventTarget->AddEventListener(.....) > > > > Remember to check for null pointers etc.. > > Cheers, > > Jens Sorensen > > > > =A0 =A0 =A0 =A0 nsresult rv; > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIWindowMediator> windowMediator =3D > > do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMWindowInternal> dwi; > > =A0 =A0 =A0 =A0 windowMediator->GetMostRecentWindow(L"navigator:browser= ", getter_AddRefs(dwi)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMDocument> doc; > > =A0 =A0 =A0 =A0 dwi->GetDocument(getter_AddRefs(doc)); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMDocumentXBL> xbl(do_QueryInterface(doc)); > > > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMElement> domEl; > > =A0 =A0 =A0 =A0 doc->GetElementById(NS_LITERAL_STRING("content"), gette= r_AddRefs(domEl)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMElement> pAnoEl; > > > > =A0 =A0 =A0 =A0 // getting xul:tabpanels > > =A0 =A0 =A0 =A0 xbl->GetAnonymousElementByAttribute(domEl, > > NS_LITERAL_STRING("anonid"), NS_LITERAL_STRING("panelcontainer"), > > getter_AddRefs(pAnoEl) =A0); > > =A0 =A0 =A0 =A0 nsString retval; > > =A0 =A0 =A0 =A0 PRBool bRet =3D 0; > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNodeList> nodeList; > > =A0 =A0 =A0 =A0 pAnoEl->GetChildNodes(getter_AddRefs(nodeList)); > > =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domNode; > > > > =A0 =A0 =A0 =A0 PRUint32 len =3D 0; > > =A0 =A0 =A0 =A0 rv =3D nodeList->GetLength(&len); > > > > =A0 =A0 =A0 =A0 for( PRUint32 i=3D0; i<len; i++ ) > > =A0 =A0 =A0 =A0 { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // getting the xul::notificationbox > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domNode; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nodeList->Item(i, getter_AddRefs(domNod= e)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // get the last child of the 'xul::noti= ficationbox' which is the 'xul:browser' > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMNode> domXULBrowser; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 domNode->GetLastChild(getter_AddRefs(do= mXULBrowser)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDOMXULElement> xulElement = =3D do_QueryInterface(domXULBrowser); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIBoxObject> boxObject; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 xulElement->GetBoxObject(getter_AddRefs= (boxObject)); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIBrowserBoxObject> browserbo= xObject =3D do_QueryInterface(boxObject); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIDocShell> docShell; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 browserboxObject->GetDocShell(getter_Ad= dRefs(docShell)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIWebNavigation> webNav =3D d= o_QueryInterface(docShell); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCOMPtr<nsIURI> uri; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 webNav->GetCurrentURI(getter_AddRefs(ur= i)); > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nsCString path; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 uri->GetAsciiSpec(path); > > =A0 =A0 =A0 =A0 } > > > > > > > > On Thu, Apr 8, 2010 at 6:04 AM, Vaibhav <[email protected]> wrote: > > > Hi, > > > > > I am writing Firefox extension using C++. > > > > > I am enumerating the tabs to get their respective URLs. > > > > > Following is pseudocode: > > > > > /////////////////////////////////////////////////////////////////////= ////// ////////////////////////////////////////////////////////////////////= /////// /// > > > > > nsCOMPtr<nsIWindowMediator> windowMediator =3D > > > 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( > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0domEl, > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0NS_LITERAL_STRIN= G("anonid"), > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0NS_LITERAL_STRIN= G("tabcontainer"), > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0getter_AddRefs(p= AnoEl) > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0); > > > nsString retval; > > > PRBool bRet =3D 0; > > > nsCOMPtr<nsIDOMNodeList> nodeList; > > > pAnoEl->GetChildNodes(getter_AddRefs(nodeList)); > > > nsCOMPtr<nsIDOMNode> domNode; > > > rv =3D nodeList->GetLength(&len); > > > for (PRUint32 i =3D 0; i < len; i++) > > > { > > > =A0 =A0 =A0 =A0nodeList->Item(i, getter_AddRefs(domNode)); > > > =A0 =A0 =A0 =A0nsCOMPtr<nsIDOMXULElement> xulElement =3D do_QueryInte= rface(domNode); > > > =A0 =A0 =A0 =A0nsCOMPtr<nsIBoxObject> boxObject; > > > =A0 =A0 =A0 =A0xulElement->GetBoxObject(getter_AddRefs(boxObject)); > > > =A0 =A0 =A0 =A0nsCOMPtr<nsIBrowserBoxObject> browserboxObject =3D > > > 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. > > > _______________________________________________ > > > dev-tech-dom mailing list > > > [email protected] > > >https://lists.mozilla.org/listinfo/dev-tech-dom > = > Hi Jens, > = > Thanks a lot. > It is a solution. > = > Vaibhav.