Click to See Complete Forum and Search --> : extracting a website's icon


sjc
August 25th, 2006, 03:48 PM
I'd like to extract a website's icon programmatically either by using IE automation or by some other means. For example, for Yahoo!, the website's icon is www.yahoo.com/favicon.ico. Websites can put a favicon.ico in their root directory and IE will pick it up. I want to save this icon programmatically. Using JavaScript, I could do:

var ie = new ActiveXObject("InternetExplorer.Application");
ie.Visible = true;
ie.Navigate("http://www.yahoo.com/favicon.ico");

So is there a way I can programmatically save this to some location? How about if the website is using the "link" tag to specify a location for their site icon. How can I extract and save that using automation?

Alternatively, is there a way I can do this using C++ with IWebBrowser2 or something?

PeejAvery
August 25th, 2006, 05:32 PM
Well, there are many ways to go about doing this. ActiveX will only run in IE so if you plan to use other browsers, that is not a good option.

Is this going to be for your use only, or are you putting this on a website for all to download the favicon automatically?

sjc
August 25th, 2006, 07:16 PM
This is for a personal piece of desktop software. It's much like a shortcut bar for web links. If yahoo.com was added, I'd like the Yahoo icon to show instead of the default browser icon. And at this time, supporting on IE is OK, so I can use automation if it's the easiest thing to do. My app is written C++/MFC and I'm adding a context menu item to IE. When my item is clicked, it executes a script I provide. In here, I'd like to extract out the site's icon.

PeejAvery
August 25th, 2006, 07:40 PM
Well, I am not a C++ programmer but I do Visual Basic. If it was Visual Basic I would use internet controls and link to the favicon of the URL, if it exists, I would download it using API.

Can you do anything similar in C++? You can't really do client-side scripting to download unless you use VBScript and that would be IE only also.

sjc
August 26th, 2006, 01:02 PM
How would you do it using VBScript?

PeejAvery
August 26th, 2006, 05:35 PM
Remember that VBScript is IE only.

Here is what I would do...
<script language="VBScript">
strSource = "http://www.google.com/favicon.ico"
strDest = "c:\path\favicon.ico"
Const adTypeBinary = 1
Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
set httpConn = WScript.CreateObject("Microsoft.XMLHTTP")
httpConn.open "GET", strSource, False
httpConn.send
set httpStream = createobject("adodb.stream")
httpStream.type = adTypeBinary
httpStream.open
httpStream.write httpConn.responseBody
httpStream.savetofile strDest, adSaveCreateOverWrite
set httpStream = nothing
set httpConn = nothing
</script>
If you don't like that, you can take a look at How to tranparently download a file over the web and run it on the client machine (http://www.eggheadcafe.com/articles/20010329.asp). Just use the favicon of the currect URL.