Hello All,
I am trying to write an application with Mozilla Embedding. I do not want any
UI involved in this application.
This is a pure backend application.
Here is what I want to acheive:
1. Load a URL.
2. Save the Document generated after any embedded javascript(s) are
executed.
I did the following:
nsCOMPtr<nsILocalFile> nsFile;
NS_NewLocalFile(NS_LITERAL_STRING("/usr/X11R6/lib/firefox"), TRUE,
getter_AddRefs(nsFile));
nsresult rv = NS_InitEmbedding(nsFile, nsnull);
if (rv != NS_OK) {
cerr << "Embedding failed.." << rv << endl;
exit(1);
}
nsCOMPtr<nsIComponentRegistrar> registrar;
rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
if (NS_FAILED(rv)) {
cerr << "Failed to get component registrar.." << endl;
exit (1);
}
nsCOMPtr<nsILocalFile> componentDir;
NS_NewLocalFile(NS_LITERAL_STRING("/usr/X11R6/lib/firefox/components"),
false, getter_AddRefs(componentDir));
rv = registrar->AutoRegister(componentDir);
if (NS_FAILED(rv)) {
cerr << "Auto registration failed" << endl;
}
nsCOMPtr<nsIServiceManager> servMan;
nsresult r = NS_GetServiceManager(getter_AddRefs(servMan));
if (NS_FAILED(r)) {
cerr << "Failed to get service manager.." << endl;
}
nsCOMPtr<nsIComponentManager> compMan;
if (NS_FAILED(NS_GetComponentManager(getter_AddRefs(compMan))) ) {
cerr << "Failed to component manager.." << endl;
}
nsCOMPtr<nsIWebNavigation> docShell = do_GetService("@mozilla.org/webshell;1",
&r);
if (NS_FAILED(r)) {
fprintf(stderr, "%x\n", r);
cerr << "failed to get web browser component.." << NS_FAILED(r) << endl;
exit (1);
}
nsCOMPtr<nsIIOService> ioService = nsnull;
r = servMan->GetServiceByContractID("@mozilla.org/network/io-service;1", NS_GET_IID(nsIIOService),
getter_AddRefs(ioService));
if (NS_FAILED(r)) {
cerr << "Failed to get IO Service" << endl;
exit (1);
}
if (ioService) {
nsCOMPtr<nsIURI> uri = nsnull;
ioService->NewURI(NS_LITERAL_CSTRING("http://www.yahoo.com"), nsnull,
nsnull, getter_AddRefs(uri));
nsEmbedString scheme;
uri->GetScheme(scheme);
cerr << "Scheme is " << scheme.get() << endl;
PRBool flag = PR_TRUE;
r = docShell->LoadURI(NS_ConvertASCIItoUTF16("http://www.yahoo.com").get(), nsIWebNavigation::LOAD_FLAGS_NONE,
nsnull, nsnull, nsnull);
fprintf(stderr, "Succes : %x\n", r);
sleep(5); // wait for the page to load. Did not find any addListener
method(s) in nsIWebNavigation
nsCOMPtr<nsIDOMDocument> dom;
docShell->GetDocument(getter_AddRefs(dom));
if (!dom) {
cerr << "Failed to get DOM.." << endl;
return 0;
}
saveDocument(dom.get(), "", "/home/baliga/tmp/localfile.html"); // I could
save DOM into a file
}
Here saveDocument Always save following content into localfile.html
<html><head><base href="about:blank"></head><body></body></html>
I am doing something wrong here.. Don't know what yet. Any pointer would be
greatly appreciated.
Thanks,
-- Yogish
begin:vcard
fn:Yogish Baliga
n:Baliga;Yogish
org:Yahoo! Inc
email;internet:[EMAIL PROTECTED]
title:Senior Technical Yahoo!
tel;work:(408)-349-7450
tel;fax:(408)-349-5454
tel;pager:[EMAIL PROTECTED]
x-mozilla-html:FALSE
url:http://billpay.yahoo.com
version:2.1
end:vcard