Search code examples
firefox-addonxpcom

How to get images from cache using a XPCOM Component in Firefox


I need to get the cache file path for ever image loaded in a document, I am wondering what are the Interfaces I need to use in order to do that

https://developer.mozilla.org/en/XPCOM_Interface_Reference


Solution

  • This is what I used to evict cache entry:

      function removeItem(url){
        let cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
                                .getService(Components.interfaces.nsICacheService);
        var Ci = Components.interfaces;
        var session = cacheService.createSession("image", Ci.nsICache.STORE_ANYWHERE, false);
        if(!session){
            return;
        }
    
        var entry;
        try{
            entry = session.openCacheEntry(url, Ci.nsICache.ACCESS_READ, false);
            if(!entry){
                return;
            }
        }catch(ex){
            return;
        }
    
        entry.doom();
        entry.close();
      }
    }
    

    Once you have entry you should be able to open a stream to it - possibly getting the content or even replacing it - I haven't tried it though.