"chrome:" URIs have no host; check for that, and substitute "hohost" instead
[k8imago.git] / code / modules / stoplist.js
blobf641aba88a5b24a3384ba1c35c1b9a74b808918a
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 // black&white lists
11 var EXPORTED_SYMBOLS = [
12 "blockImage",
13 "unblockImage",
14 "isBlockedImage",
15 "isUnblockedImage",
16 "resetImageStatus",
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);
33 return res;
37 //////////////////////////////////////////////////////////////////////////////
38 // format:
39 // key string url ; without '?' and '#'; docuri (also stripped) appended as "|"+docuri
40 // value: object
41 // int expireTime (-666: haven't seen yet)
42 // int setTime (so we can remove stale unblocks)
43 // bool blocked
44 let imgBlockList = {};
45 let clearTId = null;
48 // returns cleaned list
49 function cleanList (snow, list) {
50 //let snow = Math.floor(Date.now()/1000);
51 let badkeys = null;
52 for (let [n, v] of Iterator(list)) {
53 if (v.expireTime == -666) {
54 if (snow-v.setTime < 10*60) continue;
55 } else {
56 if (v.expireTime > snow) continue;
58 if (!badkeys) badkeys = [];
59 badkeys.push(n);
60 if (PREFS.debugLog) conlog("expired: ", n);
62 if (badkeys) for (let n of badkeys) delete list[n];
63 return list;
67 //////////////////////////////////////////////////////////////////////////////
68 function findMinSec (curmin, snow, list) {
69 let mt = false;
70 for (let [n, v] of Iterator(list)) {
71 if (v.expireTime == -666) continue;
72 if (mt === false || v.expireTime < mt) mt = v.expireTime;
74 if (mt !== false) {
75 if (mt < snow) mt = snow;
76 mt -= snow;
77 if (curmin === false || mt < curmin) curmin = mt;
79 return curmin;
83 function minExpireTimeSec (snow) {
84 let res = findMinSec(false, snow, imgBlockList);
85 return res;
89 //////////////////////////////////////////////////////////////////////////////
90 function doCleanup () {
91 clearTId = null;
92 setCleanupTimer();
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 () {
109 if (clearTId) {
110 clearTId.cancel();
111 clearTId = null;
113 setCleanupTimer();
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();
151 return null;
153 return it;
155 return null;
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;