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.
11 var EXPORTED_SYMBOLS
= [
17 "imageLoaderActivated"
20 let {utils
:Cu
, classes
:Cc
, interfaces
:Ci
, results
:Cr
} = Components
;
23 //////////////////////////////////////////////////////////////////////////////
24 Cu
.import("chrome://k8-imago-code/content/modules/utils.js");
25 Cu
.import(MODULE_PATH
+"debuglog.js");
26 Cu
.import(MODULE_PATH
+"prefs.js");
29 //////////////////////////////////////////////////////////////////////////////
30 function uri2key (uri
, docuri
) {
31 let res
= url2HashKey(uri
);
32 if (docuri
) res
+= "|"+url2HashKey(docuri
);
37 //////////////////////////////////////////////////////////////////////////////
39 // key string url ; without '?' and '#'; docuri (also stripped) appended as "|"+docuri
41 // int expireTime (-666: haven't seen yet)
42 // int setTime (so we can remove stale unblocks)
44 let imgBlockList
= {};
48 // returns cleaned list
49 function cleanList (snow
, list
) {
50 //let snow = Math.floor(Date.now()/1000);
52 for (let [n
, v
] of Iterator(list
)) {
53 if (v
.expireTime
== -666) {
54 if (snow
-v
.setTime
< 10*60) continue;
56 if (v
.expireTime
> snow
) continue;
58 if (!badkeys
) badkeys
= [];
60 if (PREFS
.debugLog
) conlog("expired: ", n
);
62 if (badkeys
) for (let n
of badkeys
) delete list
[n
];
67 //////////////////////////////////////////////////////////////////////////////
68 function findMinSec (curmin
, snow
, list
) {
70 for (let [n
, v
] of Iterator(list
)) {
71 if (v
.expireTime
== -666) continue;
72 if (mt
=== false || v
.expireTime
< mt
) mt
= v
.expireTime
;
75 if (mt
< snow
) mt
= snow
;
77 if (curmin
=== false || mt
< curmin
) curmin
= mt
;
83 function minExpireTimeSec (snow
) {
84 let res
= findMinSec(false, snow
, imgBlockList
);
89 //////////////////////////////////////////////////////////////////////////////
90 function doCleanup () {
93 if (PREFS
.debugLog
) conlog("cleanup timer fired!");
97 function setCleanupTimer () {
98 if (clearTId
=== null) {
99 let snow
= Math
.floor(Date
.now()/1000);
100 imgBlockList
= cleanList(snow
, imgBlockList
);
101 let secs
= minExpireTimeSec(snow
);
102 if (secs
=== false) return;
103 clearTId
= oneShotTimer(doCleanup
, secs
*1000+500);
108 function stoplistResetTimer () {
117 registerPrefHook("forgetSeconds", stoplistResetTimer
);
120 //////////////////////////////////////////////////////////////////////////////
121 function blockImage (uri
, docuri
) {
122 let url
= uri2key(uri
, docuri
);
123 //imgBlockList[url] = snow+PREFS.forgetSeconds;
124 let snow
= Math
.floor(Date
.now()/1000);
125 imgBlockList
[url
] = {setTime
:snow
, expireTime
:-666, blocked
:true};
126 stoplistResetTimer();
130 function unblockImage (uri
, docuri
, forceValidTime
) {
131 let url
= uri2key(uri
, docuri
);
132 //if (PREFS.debugLog) conlog("unblocking: ["+url+"]");
133 //unblockedImages[url] = snow+PREFS.forgetSeconds;
134 let snow
= Math
.floor(Date
.now()/1000);
135 let etime
= (forceValidTime
? snow
+PREFS
.forgetSeconds
: -666);
136 imgBlockList
[url
] = {setTime
:snow
, expireTime
:etime
, blocked
:false};
137 stoplistResetTimer();
141 function findImageBlockInfo (uri
, docuri
) {
142 let url
= uri2key(uri
, docuri
);
143 if (url
in imgBlockList
) {
144 let it
= imgBlockList
[url
];
145 let snow
= Math
.floor(Date
.now()/1000);
146 if ((it
.expireTime
== -666 && snow
-it
.setTime
>= 10*60) ||
147 (it
.expireTime
!= -666 && snow
-it
.expireTime
> PREFS
.forgetSeconds
))
149 delete imgBlockList
[url
];
150 stoplistResetTimer();
159 function isBlockedImage (uri
, docuri
) {
160 let it
= findImageBlockInfo(uri
, docuri
);
161 return (it
&& it
.blocked
);
165 function isUnblockedImage (uri
, docuri
) {
166 let it
= findImageBlockInfo(uri
, docuri
);
167 return (it
&& !it
.blocked
);
171 function resetImageStatus (uri
, docuri
) {
172 let url
= uri2key(uri
, docuri
);
173 if (url
in imgBlockList
) {
174 if (PREFS
.debugLog
) conlog("resetting image status for ["+url
+"]");
175 delete imgBlockList
[url
];
176 stoplistResetTimer();
181 function imageLoaderActivated (uri
, docuri
) {
182 let it
= findImageBlockInfo(uri
, docuri
);
183 if (it
&& it
.expireTime
== -666) {
184 if (PREFS
.debugLog
) conlog("setting valid block timeout for ["+uri2key(uri
, docuri
)+"]");
185 it
.expireTime
= Math
.floor(Date
.now()/1000)+PREFS
.forgetSeconds
;