Skip to content

Commit d0738c0

Browse files
committed
Visually distinguish canonical names in popup panel
Further fine-tuning support for canonical names. Aliased canonical names will be rendered blue in the dynamic filtering pane of the popup panel.
1 parent e9abce6 commit d0738c0

File tree

7 files changed

+80
-62
lines changed

7 files changed

+80
-62
lines changed

platform/chromium/vapi-background.js

+2
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,8 @@ vAPI.Net = class {
12001200
denormalizeTypes(types) {
12011201
return types;
12021202
}
1203+
canonicalNameFromHostname(/* hn */) {
1204+
}
12031205
addListener(which, clientListener, filters, options) {
12041206
const actualFilters = this.denormalizeFilters(filters);
12051207
const actualListener = this.makeNewListenerProxy(clientListener);

platform/firefox/vapi-webrequest.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
this.cnameIgnore1stParty = true;
6666
this.cnameIgnoreExceptions = true;
6767
this.cnameIgnoreRootDocument = true;
68-
this.cnameMaxTTL = 60;
68+
this.cnameMaxTTL = 120;
6969
this.cnameReplayFullURL = false;
70-
this.cnameTimer = undefined;
70+
this.cnameFlushTime = Date.now() + this.cnameMaxTTL * 60000;
7171
this.cnameUncloak = browser.dns instanceof Object;
7272
}
7373
setOptions(options) {
@@ -81,6 +81,7 @@
8181
this.cnameMaxTTL = options.cnameMaxTTL || 120;
8282
this.cnameReplayFullURL = options.cnameReplayFullURL === true;
8383
this.cnames.clear(); this.cnames.set('', '');
84+
this.cnameFlushTime = Date.now() + this.cnameMaxTTL * 60000;
8485
}
8586
normalizeDetails(details) {
8687
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
@@ -130,6 +131,12 @@
130131
}
131132
return Array.from(out);
132133
}
134+
canonicalNameFromHostname(hn) {
135+
const cn = this.cnames.get(hn);
136+
if ( cn !== undefined && cn !== '' ) {
137+
return cn;
138+
}
139+
}
133140
processCanonicalName(hn, cn, details) {
134141
const hnBeg = details.url.indexOf(hn);
135142
if ( hnBeg === -1 ) { return; }
@@ -149,6 +156,13 @@
149156
return super.onBeforeSuspendableRequest(details);
150157
}
151158
recordCanonicalName(hn, record) {
159+
if ( (this.cnames.size & 0b111111) === 0 ) {
160+
const now = Date.now();
161+
if ( now >= this.cnameFlushTime ) {
162+
this.cnames.clear(); this.cnames.set('', '');
163+
this.cnameFlushTime = now + this.cnameMaxTTL * 60000;
164+
}
165+
}
152166
let cname =
153167
typeof record.canonicalName === 'string' &&
154168
record.canonicalName !== hn
@@ -169,15 +183,6 @@
169183
cname = '';
170184
}
171185
this.cnames.set(hn, cname);
172-
if ( this.cnameTimer === undefined ) {
173-
this.cnameTimer = self.setTimeout(
174-
( ) => {
175-
this.cnameTimer = undefined;
176-
this.cnames.clear(); this.cnames.set('', '');
177-
},
178-
this.cnameMaxTTL * 60000
179-
);
180-
}
181186
return cname;
182187
}
183188
regexFromStrList(list) {

src/css/popup.css

+3
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ body[dir="rtl"] #tooltip {
281281
padding-right: 2px;
282282
width: calc(100% - 4em);
283283
}
284+
#firewallContainer > div.isCname > span:first-of-type {
285+
color: mediumblue;
286+
}
284287
#firewallContainer > div > span:first-of-type > sup {
285288
color: #666;
286289
display: none;

src/js/background.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const µBlock = (( ) => { // jshint ignore:line
5050
cnameIgnore1stParty: true,
5151
cnameIgnoreExceptions: true,
5252
cnameIgnoreRootDocument: true,
53-
cnameMaxTTL: 60,
53+
cnameMaxTTL: 120,
5454
cnameReplayFullURL: false,
5555
cnameUncloak: true,
5656
consoleLogLevel: 'unset',

src/js/messaging.js

+48-48
Original file line numberDiff line numberDiff line change
@@ -187,78 +187,78 @@ vAPI.messaging.setup(onMessage);
187187

188188
const µb = µBlock;
189189

190-
const getHostnameDict = function(hostnameToCountMap) {
191-
const r = Object.create(null);
192-
const domainFromHostname = µb.URI.domainFromHostname;
193-
// Note: destructuring assignment not supported before Chromium 49.
190+
const getHostnameDict = function(hostnameToCountMap, out) {
191+
const hnDict = Object.create(null);
192+
const cnSet = [];
194193
for ( const [ hostname, hnCounts ] of hostnameToCountMap ) {
195-
if ( r[hostname] !== undefined ) { continue; }
196-
const domain = domainFromHostname(hostname) || hostname;
194+
if ( hnDict[hostname] !== undefined ) { continue; }
195+
const domain = vAPI.domainFromHostname(hostname) || hostname;
197196
const dnCounts = hostnameToCountMap.get(domain) || 0;
198197
let blockCount = dnCounts & 0xFFFF;
199198
let allowCount = dnCounts >>> 16 & 0xFFFF;
200-
if ( r[domain] === undefined ) {
201-
r[domain] = {
202-
domain: domain,
203-
blockCount: blockCount,
204-
allowCount: allowCount,
199+
if ( hnDict[domain] === undefined ) {
200+
hnDict[domain] = {
201+
domain,
202+
blockCount,
203+
allowCount,
205204
totalBlockCount: blockCount,
206-
totalAllowCount: allowCount
205+
totalAllowCount: allowCount,
207206
};
207+
const cname = vAPI.net.canonicalNameFromHostname(domain);
208+
if ( cname !== undefined ) {
209+
cnSet.push(cname);
210+
}
208211
}
209-
const domainEntry = r[domain];
212+
const domainEntry = hnDict[domain];
210213
blockCount = hnCounts & 0xFFFF;
211214
allowCount = hnCounts >>> 16 & 0xFFFF;
212215
domainEntry.totalBlockCount += blockCount;
213216
domainEntry.totalAllowCount += allowCount;
214217
if ( hostname === domain ) { continue; }
215-
r[hostname] = {
216-
domain: domain,
217-
blockCount: blockCount,
218-
allowCount: allowCount,
218+
hnDict[hostname] = {
219+
domain,
220+
blockCount,
221+
allowCount,
219222
totalBlockCount: 0,
220223
totalAllowCount: 0,
221224
};
225+
const cname = vAPI.net.canonicalNameFromHostname(hostname);
226+
if ( cname !== undefined ) {
227+
cnSet.push(cname);
228+
}
222229
}
223-
return r;
230+
out.hostnameDict = hnDict;
231+
out.cnameSet = cnSet;
224232
};
225233

226234
const getFirewallRules = function(srcHostname, desHostnames) {
227-
var r = {};
228-
var df = µb.sessionFirewall;
229-
r['/ * *'] = df.lookupRuleData('*', '*', '*');
230-
r['/ * image'] = df.lookupRuleData('*', '*', 'image');
231-
r['/ * 3p'] = df.lookupRuleData('*', '*', '3p');
232-
r['/ * inline-script'] = df.lookupRuleData('*', '*', 'inline-script');
233-
r['/ * 1p-script'] = df.lookupRuleData('*', '*', '1p-script');
234-
r['/ * 3p-script'] = df.lookupRuleData('*', '*', '3p-script');
235-
r['/ * 3p-frame'] = df.lookupRuleData('*', '*', '3p-frame');
236-
if ( typeof srcHostname !== 'string' ) { return r; }
237-
238-
r['. * *'] = df.lookupRuleData(srcHostname, '*', '*');
239-
r['. * image'] = df.lookupRuleData(srcHostname, '*', 'image');
240-
r['. * 3p'] = df.lookupRuleData(srcHostname, '*', '3p');
241-
r['. * inline-script'] = df.lookupRuleData(srcHostname,
235+
const out = {};
236+
const df = µb.sessionFirewall;
237+
out['/ * *'] = df.lookupRuleData('*', '*', '*');
238+
out['/ * image'] = df.lookupRuleData('*', '*', 'image');
239+
out['/ * 3p'] = df.lookupRuleData('*', '*', '3p');
240+
out['/ * inline-script'] = df.lookupRuleData('*', '*', 'inline-script');
241+
out['/ * 1p-script'] = df.lookupRuleData('*', '*', '1p-script');
242+
out['/ * 3p-script'] = df.lookupRuleData('*', '*', '3p-script');
243+
out['/ * 3p-frame'] = df.lookupRuleData('*', '*', '3p-frame');
244+
if ( typeof srcHostname !== 'string' ) { return out; }
245+
246+
out['. * *'] = df.lookupRuleData(srcHostname, '*', '*');
247+
out['. * image'] = df.lookupRuleData(srcHostname, '*', 'image');
248+
out['. * 3p'] = df.lookupRuleData(srcHostname, '*', '3p');
249+
out['. * inline-script'] = df.lookupRuleData(srcHostname,
242250
'*',
243251
'inline-script'
244252
);
245-
r['. * 1p-script'] = df.lookupRuleData(srcHostname, '*', '1p-script');
246-
r['. * 3p-script'] = df.lookupRuleData(srcHostname, '*', '3p-script');
247-
r['. * 3p-frame'] = df.lookupRuleData(srcHostname, '*', '3p-frame');
253+
out['. * 1p-script'] = df.lookupRuleData(srcHostname, '*', '1p-script');
254+
out['. * 3p-script'] = df.lookupRuleData(srcHostname, '*', '3p-script');
255+
out['. * 3p-frame'] = df.lookupRuleData(srcHostname, '*', '3p-frame');
248256

249257
for ( const desHostname in desHostnames ) {
250-
r[`/ ${desHostname} *`] = df.lookupRuleData(
251-
'*',
252-
desHostname,
253-
'*'
254-
);
255-
r[`. ${desHostname} *`] = df.lookupRuleData(
256-
srcHostname,
257-
desHostname,
258-
'*'
259-
);
258+
out[`/ ${desHostname} *`] = df.lookupRuleData('*', desHostname, '*');
259+
out[`. ${desHostname} *`] = df.lookupRuleData(srcHostname, desHostname, '*');
260260
}
261-
return r;
261+
return out;
262262
};
263263

264264
const popupDataFromTabId = function(tabId, tabTitle) {
@@ -304,7 +304,7 @@ const popupDataFromTabId = function(tabId, tabTitle) {
304304
r.pageBlockedRequestCount = pageStore.perLoadBlockedRequestCount;
305305
r.pageAllowedRequestCount = pageStore.perLoadAllowedRequestCount;
306306
r.netFilteringSwitch = pageStore.getNetFilteringSwitch();
307-
r.hostnameDict = getHostnameDict(pageStore.hostnameToCountMap);
307+
getHostnameDict(pageStore.hostnameToCountMap, r);
308308
r.contentLastModified = pageStore.contentLastModified;
309309
r.firewallRules = getFirewallRules(rootHostname, r.hostnameDict);
310310
r.canElementPicker = µb.URI.isNetworkURI(r.rawURL);

src/js/popup.js

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ const cachePopupData = function(data) {
117117
return popupData;
118118
}
119119
popupData = data;
120+
if ( Array.isArray(popupData.cnameSet) ) {
121+
popupData.cnameSet = new Set(popupData.cnameSet);
122+
} else if ( popupData.cnameSet === undefined ) {
123+
popupData.cnameSet = new Set();
124+
}
120125
scopeToSrcHostnameMap['.'] = popupData.pageHostname || '';
121126
const hostnameDict = popupData.hostnameDict;
122127
if ( typeof hostnameDict !== 'object' ) {
@@ -334,6 +339,7 @@ const buildAllFirewallRows = function() {
334339
classList.toggle('isRootContext', des === popupData.pageHostname);
335340
classList.toggle('isDomain', isDomain);
336341
classList.toggle('isSubDomain', !isDomain);
342+
classList.toggle('isCname', popupData.cnameSet.has(des));
337343
classList.toggle('allowed', hnDetails.allowCount !== 0);
338344
classList.toggle('blocked', hnDetails.blockCount !== 0);
339345
classList.toggle('totalAllowed', hnDetails.totalAllowCount !== 0);

src/js/traffic.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ const onBeforeRequest = function(details) {
103103
) {
104104
pageStore.setFrame(details.frameId, details.url);
105105
}
106-
if ( result !== 2 ) { return; }
107-
return { cancel: false };
106+
if ( result === 2 ) {
107+
return { cancel: false };
108+
}
109+
return;
108110
}
109111

110112
// Blocked

0 commit comments

Comments
 (0)