📜 ⬆️ ⬇️

Get and display the favicon of the site in AdobeAIR

Safari is able to display images in the .ico format, but AdobeAIR does not know how, although both use WebKIT . It is very annoying when I want to display his favicon near the link to the site, but I cannot do it.

Solution found, though not perfect. I decided to use the service to get favicon from the site in png , get an answer from there and cache it locally.


')
We will use air.URLLoader to upload images.

The result was the following function:

  function getFavicon (url) {
     // here we pull out only the part we need from the url string - domain name 
     var domain = id.match (/ (\ w +): \ / \ / ([^ \ /:] +) (: \ d *)? ([^ #] *) / i); 
     if (domain! = null) { 
         var domname = domain [2]; 
         
         // check if we already have such an icon in the cache 
         var file = air.File.applicationStorageDirectory.resolvePath ("favicons /" + domname + '.png'); 
         if (file.exists) 
             // and, if there is, return a link to it 
             return file.url; 
         
         // but if not, send a request for sectorprime 
         var fav = "http://www.sectorprime.com/cgi-bin/fav2png.pl?fav=" + domname; 
         air.trace ('load favicon' + domname); 
         
         var request = new air.URLRequest (fav); 
         
         var loader = new air.URLLoader </a> (); 
         // important!  we will receive data in binary form 
         loader.dataFormat = air.URLLoaderDataFormat.BINARY; 
         
         loader.addEventListener (air.Event.COMPLETE, function (e) { 
             // this function will work out immediately after we receive the answer to the request 
             var loader = e.target; 
             
             // create a stream to write to the file 
             var fileStream = new air.FileStream (); 
             fileStream.open (file, air.FileMode.WRITE);  
             // and write everything we got into it 
             fileStream.writeBytes (loader.data); 
             fileStream.close (); 
 
         }); 
         
         // actually start the boot process 
         loader.load (request); 
     } 
 
     // but this icon will be issued by us by default 
     return "/images/icon_item.png";
 } 


For the first time, we will not display the icon, but we will begin to download it. When the function is called the next time, the icon will be taken from the local cache (if you have already downloaded it). Why not immediately? - so as not to slow down the execution of the script by waiting.

Clearing the icon cache can be done like this:

  var favdir = air.File.applicationStorageDirectory.resolvePath ("favicons"); 
             
 // find out if such a folder exists at all 
 if ((favdir.exists) && (favdir.isDirectory)) 
     // if exists, delete the folder along with the contents 
     favdir.deleteDirectory (true); 


Disadvantages of this solution:


If someone has suggestions for improving the function, write, I will be glad to any comments.

Crosspost from a personal blog.

Source: https://habr.com/ru/post/27435/


All Articles