fixed image reloading bug for images with relative URLs
[k8imago.git] / code / modules / imgreload.js
blob5de37d602e07c8b1eb1f4a6591728344fa9da496
1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 // reload image
11 let EXPORTED_SYMBOLS = [
12 "reloadImage",
13 "reloadUrlList"
16 const {utils:Cu, classes:Cc, interfaces:Ci, results:Cr} = Components;
19 //////////////////////////////////////////////////////////////////////////////
20 Cu.import("chrome://k8-imago-code/content/modules/utils.js");
21 Cu.import(MODULE_PATH+"debuglog.js");
22 Cu.import(MODULE_PATH+"prefs.js");
24 const iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
27 //////////////////////////////////////////////////////////////////////////////
28 function expungeImageCache (el, url) {
29 if (!el || typeof(el.tagName) !== "string" || el.tagName !== "IMG") {
30 if (PREFS.debugLog) conlog("WTF! ["+url+"] is not an image!");
31 return;
33 try {
34 //if (PREFS.debugLog) conlog(" converting URL=[", url, "]...");
35 //let ourl = url;
36 //let uri = iosvc.newURI(url, null, null);
37 //let url = uri.spec;
38 //if (PREFS.debugLog) conlog(" URL=[", ourl, "]; URI=[", url, "]");
39 let tools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
40 let cache = tools.getImgCacheForDocument(el.ownerDocument);
41 if (cache) {
42 if (url === undefined) {
43 url = el.getAttribute("src");
44 if (PREFS.debugLog) conlog(" got URL from image: URL=[", url, "]...");
46 if (typeof(url) === "string") {
47 if (PREFS.debugLog) conlog(" converting URL=[", url, "] to URI");
48 let baseURI = null;
49 try {
50 if (PREFS.debugLog) conlog(" docURI=[", el.ownerDocument.documentURI, "]");
51 baseURI = iosvc.newURI(el.ownerDocument.documentURI, null, null);
52 if (PREFS.debugLog) conlog(" BASE=[", baseURI.spec, "]");
53 } catch (e) {}
54 url = iosvc.newURI(url, null, baseURI);
56 if (PREFS.debugLog) conlog("got cache object for image with url ", url.spec);
57 try {
58 cache.removeEntry(url);
59 } catch (e) {
60 if (PREFS.debugLog) conlog(" error clearing cache for image with url ", url.spec);
61 if (PREFS.debugLog) logException("cache error", e);
63 } else {
64 if (PREFS.debugLog) conlog("CAN'T get cache object for document");
66 } catch (e) {
67 if (PREFS.debugLog) conlog(" some error occuren when clearing cache for image with url ", el.src);
68 if (PREFS.debugLog) logException("unknown cache error", e);
73 //////////////////////////////////////////////////////////////////////////////
74 let reloadUrlList = {};
77 function reloadImage (el) {
78 if (el instanceof Ci.nsIImageLoadingContent) {
79 if (PREFS.debugLog) conlog("forcing reload!");
80 let src = el.getAttribute("src");
81 reloadUrlList[src] = true;
82 expungeImageCache(el);
83 oneShotTimer(function () { delete reloadUrlList[src]; }, 500);
84 // and for all images in document
85 for (let img of el.ownerDocument.querySelectorAll("img[src]")) {
86 if (img.getAttribute("src") === src) {
87 if (img instanceof Ci.nsIImageLoadingContent) {
88 try {
89 img.forceReload();
90 } catch (e) {
91 if (PREFS.debugLog) logException("reload error", e);
96 } else {
97 if (PREFS.debugLog) conlog("alas...");