19
19
Home: https://github.com/gorhill/uBlock
20
20
*/
21
21
22
- /* global µBlock */
22
+ /* global vAPI, µBlock */
23
+
24
+ /******************************************************************************/
25
+ /******************************************************************************/
26
+
27
+ ( function ( ) {
28
+
23
29
'use strict' ;
24
30
25
31
/******************************************************************************/
26
32
33
+ var µb = µBlock ;
34
+
35
+ /******************************************************************************/
36
+ /******************************************************************************/
37
+
27
38
// When the DOM content of root frame is loaded, this means the tab
28
39
// content has changed.
29
40
vAPI . tabs . onNavigation = function ( details ) {
30
41
if ( details . frameId !== 0 ) {
31
42
return ;
32
43
}
33
- µBlock . bindTabToPageStats ( details . tabId , details . url ) ;
44
+ µb . bindTabToPageStats ( details . tabId , details . url ) ;
34
45
} ;
35
46
36
47
// It may happen the URL in the tab changes, while the page's document
@@ -43,19 +54,19 @@ vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
43
54
if ( ! changeInfo . url ) {
44
55
return ;
45
56
}
46
- µBlock . bindTabToPageStats ( tabId , changeInfo . url , 'tabUpdated' ) ;
57
+ µb . bindTabToPageStats ( tabId , changeInfo . url , 'tabUpdated' ) ;
47
58
} ;
48
59
49
60
vAPI . tabs . onClosed = function ( tabId ) {
50
61
if ( tabId < 0 ) {
51
62
return ;
52
63
}
53
- µBlock . unbindTabFromPageStats ( tabId ) ;
64
+ µb . unbindTabFromPageStats ( tabId ) ;
54
65
} ;
55
66
56
67
// https://github.com/gorhill/uBlock/issues/297
57
68
vAPI . tabs . onPopup = function ( details ) {
58
- var pageStore = µBlock . pageStoreFromTabId ( details . sourceTabId ) ;
69
+ var pageStore = µb . pageStoreFromTabId ( details . sourceTabId ) ;
59
70
if ( ! pageStore ) {
60
71
return ;
61
72
}
@@ -64,8 +75,8 @@ vAPI.tabs.onPopup = function(details) {
64
75
65
76
// https://github.com/gorhill/uBlock/issues/323
66
77
// If popup URL is whitelisted, do not block it
67
- if ( µBlock . getNetFilteringSwitch ( requestURL ) ) {
68
- result = µBlock . netFilteringEngine . matchStringExactType ( pageStore , requestURL , 'popup' ) ;
78
+ if ( µb . getNetFilteringSwitch ( requestURL ) ) {
79
+ result = µb . netFilteringEngine . matchStringExactType ( pageStore , requestURL , 'popup' ) ;
69
80
}
70
81
71
82
// https://github.com/gorhill/uBlock/issues/91
@@ -81,7 +92,7 @@ vAPI.tabs.onPopup = function(details) {
81
92
// Blocked
82
93
83
94
// It is a popup, block and remove the tab.
84
- µBlock . unbindTabFromPageStats ( details . tabId ) ;
95
+ µb . unbindTabFromPageStats ( details . tabId ) ;
85
96
vAPI . tabs . remove ( details . tabId ) ;
86
97
87
98
// for Safari
@@ -90,7 +101,6 @@ vAPI.tabs.onPopup = function(details) {
90
101
91
102
vAPI . tabs . registerListeners ( ) ;
92
103
93
-
94
104
/******************************************************************************/
95
105
/******************************************************************************/
96
106
@@ -104,7 +114,7 @@ vAPI.tabs.registerListeners();
104
114
// hostname. This way, for a specific scheme you can create scope with
105
115
// rules which will apply only to that scheme.
106
116
107
- µBlock . normalizePageURL = function ( pageURL ) {
117
+ µb . normalizePageURL = function ( pageURL ) {
108
118
var uri = this . URI . set ( pageURL ) ;
109
119
if ( uri . scheme === 'https' || uri . scheme === 'http' ) {
110
120
return uri . normalizedURI ( ) ;
@@ -116,7 +126,7 @@ vAPI.tabs.registerListeners();
116
126
117
127
// Create an entry for the tab if it doesn't exist.
118
128
119
- µBlock . bindTabToPageStats = function ( tabId , pageURL , context ) {
129
+ µb . bindTabToPageStats = function ( tabId , pageURL , context ) {
120
130
this . updateBadgeAsync ( tabId ) ;
121
131
122
132
// https://github.com/gorhill/httpswitchboard/issues/303
@@ -144,7 +154,7 @@ vAPI.tabs.registerListeners();
144
154
return pageStore ;
145
155
} ;
146
156
147
- µBlock . unbindTabFromPageStats = function ( tabId ) {
157
+ µb . unbindTabFromPageStats = function ( tabId ) {
148
158
//console.debug('µBlock> unbindTabFromPageStats(%d)', tabId);
149
159
var pageStore = this . pageStores [ tabId ] ;
150
160
if ( pageStore !== undefined ) {
@@ -155,27 +165,60 @@ vAPI.tabs.registerListeners();
155
165
156
166
/******************************************************************************/
157
167
158
- µBlock . pageUrlFromTabId = function ( tabId ) {
168
+ µb . pageUrlFromTabId = function ( tabId ) {
159
169
var pageStore = this . pageStores [ tabId ] ;
160
170
return pageStore ? pageStore . pageURL : '' ;
161
171
} ;
162
172
163
- µBlock . pageUrlFromPageStats = function ( pageStats ) {
173
+ µb . pageUrlFromPageStats = function ( pageStats ) {
164
174
if ( pageStats ) {
165
175
return pageStats . pageURL ;
166
176
}
167
177
return '' ;
168
178
} ;
169
179
170
- µBlock . pageStoreFromTabId = function ( tabId ) {
180
+ µb . pageStoreFromTabId = function ( tabId ) {
171
181
return this . pageStores [ tabId ] ;
172
182
} ;
173
183
184
+ /******************************************************************************/
185
+ /******************************************************************************/
186
+
187
+ // Stale page store entries janitor
188
+ // https://github.com/gorhill/uBlock/issues/455
189
+
190
+ var pageStoreJanitorPeriod = 15 * 60 * 1000 ;
191
+ var pageStoreJanitorSampleAt = 0 ;
192
+ var pageStoreJanitorSampleSize = 10 ;
193
+
194
+ var pageStoreJanitor = function ( ) {
195
+ var vapiTabs = vAPI . tabs ;
196
+ var tabIds = Object . keys ( µb . pageStores ) . sort ( ) ;
197
+ var checkTab = function ( tabId ) {
198
+ vapiTabs . get ( tabId , function ( tab ) {
199
+ if ( ! tab ) {
200
+ //console.error('tab.js> pageStoreJanitor(): stale page store found:', µb.pageUrlFromTabId(tabId));
201
+ µb . unbindTabFromPageStats ( tabId ) ;
202
+ }
203
+ } ) ;
204
+ } ;
205
+ if ( pageStoreJanitorSampleAt >= tabIds . length ) {
206
+ pageStoreJanitorSampleAt = 0 ;
207
+ }
208
+ var n = Math . min ( pageStoreJanitorSampleAt + pageStoreJanitorSampleSize , tabIds . length ) ;
209
+ for ( var i = pageStoreJanitorSampleAt ; i < n ; i ++ ) {
210
+ checkTab ( tabIds [ i ] ) ;
211
+ }
212
+ pageStoreJanitorSampleAt = n ;
213
+
214
+ setTimeout ( pageStoreJanitor , pageStoreJanitorPeriod ) ;
215
+ } ;
216
+
217
+ setTimeout ( pageStoreJanitor , pageStoreJanitorPeriod ) ;
218
+
219
+ /******************************************************************************/
174
220
/******************************************************************************/
175
221
176
- // µBlock.forceReload = function(pageURL) {
177
- // var tabId = this.tabIdFromPageUrl(pageURL);
178
- // if ( tabId ) {
179
- // chrome.tabs.reload(tabId, { bypassCache: true });
180
- // }
181
- // };
222
+ } ) ( ) ;
223
+
224
+ /******************************************************************************/
0 commit comments