aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/popup.js
blob: 29b1ce0acdf2bef0809ccfbb03ffebf51f371e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*******************************************************************************

    µBlock - a Chromium browser extension to block requests.
    Copyright (C) 2014 Raymond Hill

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

/* global vAPI, uDom */

/******************************************************************************/

(function() {

'use strict';

/******************************************************************************/

var popupData;
var dfPaneBuilt = false;
var dfTypes = [
    'image',
    'inline-script',
    '1p-script',
    '3p-script',
    '3p-frame'
];
var popupHeight;
var reIP = /^\d+(?:\.\d+){1,3}$/;
var reSrcHostnameFromResult = /^d[abn]:([^ ]+) ([^ ]+)/;
var scopeToSrcHostnameMap = {
    '/': '*',
    '.': ''
};
var threePlus = '+++';
var threeMinus = '−−−';
var sixSpace = '\u2007\u2007\u2007\u2007\u2007\u2007';
var dfHotspots = null;
var hostnameToSortableTokenMap = {};
var allDomains = {};
var allDomainCount = 0;
var touchedDomainCount = 0;

/******************************************************************************/

// https://github.com/gorhill/httpswitchboard/issues/345

var messager = vAPI.messaging.channel('popup.js');

/******************************************************************************/

var cachePopupData = function(data) {
    popupData = {};
    scopeToSrcHostnameMap['.'] = '';
    hostnameToSortableTokenMap = {};

    if ( typeof data !== 'object' ) {
        return popupData;
    } 
    popupData = data;
    scopeToSrcHostnameMap['.'] = popupData.pageHostname || '';
    var hostnameDict = popupData.hostnameDict;
    if ( typeof hostnameDict !== 'object' ) {
        return popupData;
    }
    var domain, prefix;
    for ( var hostname in hostnameDict ) {
        if ( hostnameDict.hasOwnProperty(hostname) === false ) {
            continue;
        }
        domain = hostnameDict[hostname].domain;
        if ( domain === popupData.pageDomain ) {
            domain = '\u0020';
        }
        prefix = hostname.slice(0, 0 - domain.length);
        hostnameToSortableTokenMap[hostname] = domain + prefix.split('.').reverse().join('.');
    }
    return popupData;
};

/******************************************************************************/

var formatNumber = function(count) {
    if ( typeof count !== 'number' ) {
        return '';
    }
    return count.toLocaleString();
};

/******************************************************************************/

var rulekeyCompare = function(a, b) {
    var ha = a.slice(2, a.indexOf(' ', 2));
    if ( !reIP.test(ha) ) {
        ha = hostnameToSortableTokenMap[ha] || '';
    }
    var hb = b.slice(2, b.indexOf(' ', 2));
    if ( !reIP.test(hb) ) {
        hb = hostnameToSortableTokenMap[hb] || '';
    }
    return ha.localeCompare(hb);
};

var reRulekeyCompareNoise = /[^a-z0-9.]/g;

/******************************************************************************/

var addDynamicFilterRow = function(des) {
    var row = uDom('#templates > div:nth-of-type(1)').clone();
    row.descendants('[data-des]').attr('data-des', des);
    row.descendants('span:nth-of-type(1)').text(des);

    var hnDetails = popupData.hostnameDict[des] || {};
    var isDomain = des === hnDetails.domain;
    row.toggleClass('isDomain', isDomain);
    if ( allDomains.hasOwnProperty(hnDetails.domain) === false ) {
        allDomains[hnDetails.domain] = false;
        allDomainCount += 1;
    }
    if ( hnDetails.allowCount !== 0 ) {
        if ( allDomains[hnDetails.domain] === false ) {
            allDomains[hnDetails.domain] = true;
            touchedDomainCount += 1;
        }
        row.addClass('allowed');
    }
    if ( hnDetails.blockCount !== 0 ) {
        row.addClass('blocked');
    }

    row.appendTo('#dynamicFilteringContainer');

    // Hacky? I couldn't figure a CSS recipe for this problem.
    // I do not want the left pane -- optional and hidden by defaut -- to
    // dictate the height of the popup. The right pane dictates the height 
    // of the popup, and the left pane will have a scrollbar if ever its 
    // height is larger than what is available.
    if ( popupHeight === undefined ) {
        popupHeight = uDom('#panes > div:nth-of-type(1)').nodeAt(0).offsetHeight;
        uDom('#panes > div:nth-of-type(2)').css('height', popupHeight + 'px');
    }
    return row;
};

/******************************************************************************/

var syncDynamicFilterCell = function(scope, des, type, result) {
    var selector = '#dynamicFilteringContainer span[data-src="' + scope + '"][data-des="' + des + '"][data-type="' + type + '"]';
    var cell = uDom(selector);

    // Create the row?
    if ( cell.length === 0 ) {
        cell = addDynamicFilterRow(des).descendants(selector);
    }

    cell.removeClass();
    var action = result.charAt(1);
    if ( action !== '' ) {
        cell.toggleClass(action + 'Rule', true);
    }

    // Use dark shade visual cue if the filter is specific to the cell.
    var ownRule = false;
    var matches = reSrcHostnameFromResult.exec(result);
    if ( matches !== null ) {
        ownRule =  matches[2] === des &&
                   matches[1] === scopeToSrcHostnameMap[scope];
    }
    cell.toggleClass('ownRule', ownRule);

    if ( scope !== '.' || type !== '*' ) {
        return;
    }
    if ( popupData.hostnameDict.hasOwnProperty(des) === false ) {
        return;
    }
    var hnDetails = popupData.hostnameDict[des];
    var aCount = hnDetails.allowCount;
    var bCount = hnDetails.blockCount;
    if ( aCount === 0 && bCount === 0 ) {
        return;
    }
    aCount = Math.min(Math.ceil(Math.log10(aCount + 1)), 3);
    bCount = Math.min(Math.ceil(Math.log10(bCount + 1)), 3);
    // IMPORTANT: It is completely assumed the first node is a TEXT_NODE, so
    //            ensure this in the HTML file counterpart when you make
    //            changes
    cell.nodeAt(0).firstChild.nodeValue = threePlus.slice(0, aCount) +
                                          sixSpace.slice(aCount + bCount) +
                                          threeMinus.slice(0, bCount);
};

/******************************************************************************/

var syncAllDynamicFilters = function() {
    var hasRule = false;
    var rules = popupData.dynamicFilterRules;
    var type, result;
    var types = dfTypes;
    var i = types.length;
    while ( i-- ) {
        type = types[i];
        syncDynamicFilterCell('/', '*', type, rules['/ * ' + type] || '');
        result = rules['. * ' + type] || '';
        if ( result.charAt(1) !== '' ) {
            hasRule = true;
        }
        syncDynamicFilterCell('.', '*', type, result);
    }

    // Sort hostnames. First-party hostnames must always appear at the top
    // of the list.
    var keys = Object.keys(rules).sort(rulekeyCompare);
    var key;
    for ( var i = 0; i < keys.length; i++ ) {
        key = keys[i];
        // Specific-type rules -- they were processed above
        if ( key.slice(-1) !== '*' ) {
            continue;
        }
        syncDynamicFilterCell(key.charAt(0), key.slice(2, key.indexOf(' ', 2)), '*', rules[key]);
    }

    var summary = vAPI.i18n('popupHitDomainCountPrompt')
                      .replace('{{count}}', touchedDomainCount)
                      .replace('{{total}}', allDomainCount);
    uDom('#privacyInfo').text(summary);

    if ( dfPaneBuilt !== true ) {
        uDom('#dynamicFilteringContainer')
            .on('click', 'span[data-src]', unsetDynamicFilterHandler)
            .on('mouseenter', '[data-src]', mouseenterCellHandler)
            .on('mouseleave', '[data-src]', mouseleaveCellHandler);
        dfHotspots = uDom('#actionSelector')
            .on('click', 'span', setDynamicFilterHandler)
            .detach();
        dfPaneBuilt = true;
    }
};

/******************************************************************************/

var renderPopup = function() {
    uDom('#appname').text(popupData.appName);
    uDom('#version').text(popupData.appVersion);

    var isHTTP = /^https?:\/\/[0-9a-z]/.test(popupData.pageURL);

    // Condition for dynamic filtering toggler:
    // - Advanced user
    uDom('body').toggleClass('advancedUser', popupData.advancedUserEnabled);

    // Conditions for request log:
    // - `http` or `https` scheme
    uDom('#gotoLog').toggleClass('enabled', isHTTP);

    // Conditions for element picker:
    // - `http` or `https` scheme
    uDom('#gotoPick').toggleClass('enabled', isHTTP);

    var or = vAPI.i18n('popupOr');
    var blocked = popupData.pageBlockedRequestCount;
    var total = popupData.pageAllowedRequestCount + blocked;
    var html = [];
    if ( total === 0 ) {
        html.push('0');
    } else {
        html.push(
            formatNumber(blocked),
            '<span class="dim">&nbsp;',
            or,
            '&nbsp;',
            formatNumber(Math.floor(blocked * 100 / total)),
            '%</span>'
        );
    }
    uDom('#page-blocked').html(html.join(''));

    blocked = popupData.globalBlockedRequestCount;
    total = popupData.globalAllowedRequestCount + blocked;
    html = [];
    if ( total === 0 ) {
        html.push('0');
    } else {
        html.push(
            formatNumber(blocked),
            '<span class="dim">&nbsp;',
            or,
            '&nbsp;',
            formatNumber(Math.floor(blocked * 100 / total)),
            '%</span>'
        );
    }
    uDom('#total-blocked').html(html.join(''));

    // Build dynamic filtering pane only if in use
    if ( popupData.dfEnabled && popupData.advancedUserEnabled ) {
        syncAllDynamicFilters();
    }

    uDom('#switch .fa').toggleClass('off', popupData.pageURL === '' || !popupData.netFilteringSwitch);
    uDom('#panes').toggleClass('dfEnabled', popupData.dfEnabled && popupData.advancedUserEnabled);
};

/******************************************************************************/

var toggleNetFilteringSwitch = function(ev) {
    if ( !popupData || !popupData.pageURL ) {
        return;
    }
    messager.send({
        what: 'toggleNetFiltering',
        url: popupData.pageURL,
        scope: ev.ctrlKey || ev.metaKey ? 'page' : '',
        state: !uDom(this).toggleClass('off').hasClass('off'),
        tabId: popupData.tabId
    });
};

/******************************************************************************/

var gotoDashboard = function() {
    messager.send({
        what: 'gotoURL',
        details: {
            url: 'dashboard.html',
            select: true,
            index: -1
        }
    });
};

/******************************************************************************/

var gotoDevTools = function() {
    messager.send({
        what: 'gotoURL',
        details: {
            url: 'devtools.html?tabId=' + popupData.tabId,
            select: true,
            index: -1
        }
    });
};

/******************************************************************************/

var gotoPick = function() {
    messager.send({
        what: 'gotoPick',
        tabId: popupData.tabId
    });
    window.open('','_self').close();
};

/******************************************************************************/

var gotoLink = function(ev) {
    if (!ev.target.href) {
        return;
    }

    ev.preventDefault();

    messager.send({
        what: 'gotoURL',
        details: {
            url: ev.target.href,
            select: true,
            index: -1
        }
    });
};

/******************************************************************************/

var toggleDynamicFiltering = function(ev) {
    if ( uDom('body').hasClass('advancedUser') === false ) {
        return;
    }
    var el = uDom('#panes');
    popupData.dfEnabled = !popupData.dfEnabled;
    messager.send({
        what: 'userSettings',
        name: 'dynamicFilteringEnabled',
        value: popupData.dfEnabled
    }, renderPopup);
};

/******************************************************************************/

var mouseenterCellHandler = function() {
    if ( uDom(this).hasClass('ownRule') === false ) {
        dfHotspots.appendTo(this);
    }
};

var mouseleaveCellHandler = function() {
    dfHotspots.detach();
};

/******************************************************************************/

var setDynamicFilter = function(src, des, type, action) {
    // This can happen on pages where uBlock does not work
    if ( typeof popupData.pageHostname !== 'string' || popupData.pageHostname === '' ) {
        return;
    }
    var onDynamicFilterChanged = function(response) {
        cachePopupData(response);
        syncAllDynamicFilters();
    };
    messager.send({
        what: 'toggleDynamicFilter',
        tabId: popupData.tabId,
        pageHostname: popupData.pageHostname,
        srcHostname: src,
        desHostname: des,
        requestType: type,
        action: action
    }, onDynamicFilterChanged);
};

/******************************************************************************/

var unsetDynamicFilterHandler = function() {
    var cell = uDom(this);
    setDynamicFilter(
        cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
        cell.attr('data-des'),
        cell.attr('data-type'),
        0
    );
    dfHotspots.appendTo(cell);
};

/******************************************************************************/

var setDynamicFilterHandler = function() {
    var hotspot = uDom(this);
    var cell = hotspot.ancestors('[data-src]');
    if ( cell.length === 0 ) {
        return;
    }
    var action = 0;
    var hotspotId = hotspot.attr('id');
    if ( hotspotId === 'dynaAllow' ) {
        action = 2;
    } else if ( hotspotId === 'dynaNoop' ) {
        action = 3
    } else {
        action = 1;
    }
    setDynamicFilter(
        cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
        cell.attr('data-des'),
        cell.attr('data-type'),
        action
    );
    dfHotspots.detach();
};

/******************************************************************************/

// Make menu only when popup html is fully loaded

uDom.onLoad(function() {
    messager.send({ what: 'activeTabStats' }, function(response) {
        if ( !cachePopupData(response) ) {
            return;
        }
        renderPopup();
    });
    uDom('h1,h2,h3,h4').on('click', gotoDashboard);
    uDom('#switch .fa').on('click', toggleNetFilteringSwitch);
    uDom('#gotoLog').on('click', gotoDevTools);
    uDom('#gotoPick').on('click', gotoPick);
    uDom('a[href^=http]').on('click', gotoLink);
    uDom('#dfToggler').on('click', toggleDynamicFiltering);
});

/******************************************************************************/

})();