2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-01-07 23:53:33 +01:00
|
|
|
Copyright (C) 2014-2018 Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
/* global punycode, uDom */
|
2014-07-07 03:52:16 +02:00
|
|
|
|
2016-08-06 18:05:01 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
2015-03-16 14:20:58 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-11-06 22:27:21 +01:00
|
|
|
var popupFontSize = vAPI.localStorage.getItem('popupFontSize');
|
|
|
|
if ( typeof popupFontSize === 'string' && popupFontSize !== 'unset' ) {
|
|
|
|
document.body.style.setProperty('font-size', popupFontSize);
|
|
|
|
}
|
|
|
|
|
2015-03-22 13:51:31 +01:00
|
|
|
// Ensure the popup is properly sized as soon as possible. It is assume the DOM
|
|
|
|
// content is ready at this point, which should be the case given where this
|
|
|
|
// script file is included in the HTML file.
|
2015-03-16 14:20:58 +01:00
|
|
|
|
2015-03-22 13:51:31 +01:00
|
|
|
var dfPaneVisibleStored = vAPI.localStorage.getItem('popupFirewallPane') === 'true';
|
|
|
|
|
2016-12-29 05:39:15 +01:00
|
|
|
// No restriction on vertical size?
|
|
|
|
if ( /[\?&]fullsize=1/.test(window.location.search) ) {
|
|
|
|
document.body.classList.add('fullsize');
|
|
|
|
}
|
2016-02-06 15:50:02 +01:00
|
|
|
|
2016-12-29 05:39:15 +01:00
|
|
|
// Mobile device?
|
2017-09-18 16:52:30 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3032
|
|
|
|
// - If at least one of the window's viewport dimension is larger than the
|
|
|
|
// corresponding device's screen dimension, assume uBO's popup panel sits in
|
|
|
|
// its own tab.
|
|
|
|
if (
|
|
|
|
/[\?&]mobile=1/.test(window.location.search) ||
|
|
|
|
window.innerWidth >= window.screen.availWidth ||
|
|
|
|
window.innerHeight >= window.screen.availHeight
|
|
|
|
) {
|
2016-12-29 05:39:15 +01:00
|
|
|
document.body.classList.add('mobile');
|
|
|
|
}
|
2015-03-22 13:51:31 +01:00
|
|
|
|
2015-05-23 13:31:46 +02:00
|
|
|
// The padlock/eraser must be manually positioned:
|
|
|
|
// - Its vertical position depends on the height of the popup title bar
|
|
|
|
// - Its horizontal position depends on whether there is a vertical scrollbar.
|
|
|
|
document.getElementById('rulesetTools').style.setProperty(
|
|
|
|
'top',
|
2017-06-21 21:49:42 +02:00
|
|
|
(document.getElementById('appinfo').getBoundingClientRect().bottom + 3) + 'px'
|
2015-05-23 13:31:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
var positionRulesetTools = function() {
|
|
|
|
document.getElementById('rulesetTools').style.setProperty(
|
|
|
|
'left',
|
|
|
|
(document.getElementById('firewallContainer').getBoundingClientRect().left + 3) + 'px'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/996
|
2015-03-22 13:51:31 +01:00
|
|
|
// Experimental: mitigate glitchy popup UI: immediately set the firewall pane
|
|
|
|
// visibility to its last known state. By default the pane is hidden.
|
|
|
|
// Will remove if it makes no difference.
|
|
|
|
if ( dfPaneVisibleStored ) {
|
|
|
|
document.getElementById('panes').classList.add('dfEnabled');
|
|
|
|
}
|
2015-03-14 22:30:44 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
var messaging = vAPI.messaging;
|
2015-04-24 16:55:11 +02:00
|
|
|
var popupData = {};
|
2015-01-06 14:01:15 +01:00
|
|
|
var dfPaneBuilt = false;
|
2014-12-30 22:36:29 +01:00
|
|
|
var reIP = /^\d+(?:\.\d+){1,3}$/;
|
|
|
|
var scopeToSrcHostnameMap = {
|
|
|
|
'/': '*',
|
|
|
|
'.': ''
|
|
|
|
};
|
2015-01-06 14:01:15 +01:00
|
|
|
var dfHotspots = null;
|
2014-12-31 23:26:17 +01:00
|
|
|
var hostnameToSortableTokenMap = {};
|
2015-01-07 01:51:50 +01:00
|
|
|
var allDomains = {};
|
|
|
|
var allDomainCount = 0;
|
2015-02-06 23:46:20 +01:00
|
|
|
var allHostnameRows = [];
|
2015-01-07 01:51:50 +01:00
|
|
|
var touchedDomainCount = 0;
|
2015-01-10 17:23:28 +01:00
|
|
|
var rowsToRecycle = uDom();
|
|
|
|
var cachedPopupHash = '';
|
2015-03-11 16:21:29 +01:00
|
|
|
var statsStr = vAPI.i18n('popupBlockedStats');
|
2015-02-07 00:20:33 +01:00
|
|
|
var domainsHitStr = vAPI.i18n('popupHitDomainCount');
|
2017-04-22 20:26:17 +02:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2550
|
|
|
|
// Solution inspired from
|
2017-04-23 01:23:11 +02:00
|
|
|
// - https://bugs.chromium.org/p/chromium/issues/detail?id=683314
|
2017-04-22 20:26:17 +02:00
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1332714#c17
|
|
|
|
// Confusable character set from:
|
|
|
|
// - http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%D0%B0%D1%81%D4%81%D0%B5%D2%BB%D1%96%D1%98%D3%8F%D0%BE%D1%80%D4%9B%D1%95%D4%9D%D1%85%D1%83%D1%8A%D0%AC%D2%BD%D0%BF%D0%B3%D1%B5%D1%A1%5D&g=gc&i=
|
|
|
|
// Linked from:
|
|
|
|
// - https://www.chromium.org/developers/design-documents/idn-in-google-chrome
|
2017-04-23 01:23:11 +02:00
|
|
|
var reCyrillicNonAmbiguous = /[\u0400-\u042b\u042d-\u042f\u0431\u0432\u0434\u0436-\u043d\u0442\u0444\u0446-\u0449\u044b-\u0454\u0457\u0459-\u0460\u0462-\u0474\u0476-\u04ba\u04bc\u04be-\u04ce\u04d0-\u0500\u0502-\u051a\u051c\u051e-\u052f]/;
|
|
|
|
var reCyrillicAmbiguous = /[\u042c\u0430\u0433\u0435\u043e\u043f\u0440\u0441\u0443\u0445\u044a\u0455\u0456\u0458\u0461\u0475\u04bb\u04bd\u04cf\u0501\u051b\u051d]/;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-30 22:36:29 +01:00
|
|
|
var cachePopupData = function(data) {
|
2015-01-06 14:01:15 +01:00
|
|
|
popupData = {};
|
2014-12-31 23:26:17 +01:00
|
|
|
scopeToSrcHostnameMap['.'] = '';
|
|
|
|
hostnameToSortableTokenMap = {};
|
2015-01-07 01:51:50 +01:00
|
|
|
|
2014-12-31 23:26:17 +01:00
|
|
|
if ( typeof data !== 'object' ) {
|
2015-01-06 14:01:15 +01:00
|
|
|
return popupData;
|
2014-12-25 14:53:30 +01:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
popupData = data;
|
|
|
|
scopeToSrcHostnameMap['.'] = popupData.pageHostname || '';
|
|
|
|
var hostnameDict = popupData.hostnameDict;
|
2015-01-07 01:51:50 +01:00
|
|
|
if ( typeof hostnameDict !== 'object' ) {
|
|
|
|
return popupData;
|
|
|
|
}
|
|
|
|
var domain, prefix;
|
|
|
|
for ( var hostname in hostnameDict ) {
|
|
|
|
if ( hostnameDict.hasOwnProperty(hostname) === false ) {
|
|
|
|
continue;
|
2014-12-31 23:26:17 +01:00
|
|
|
}
|
2015-01-07 01:51:50 +01:00
|
|
|
domain = hostnameDict[hostname].domain;
|
2015-10-25 13:18:10 +01:00
|
|
|
prefix = hostname.slice(0, 0 - domain.length);
|
|
|
|
// Prefix with space char for 1st-party hostnames: this ensure these
|
|
|
|
// will come first in list.
|
2015-01-07 01:51:50 +01:00
|
|
|
if ( domain === popupData.pageDomain ) {
|
|
|
|
domain = '\u0020';
|
|
|
|
}
|
|
|
|
hostnameToSortableTokenMap[hostname] = domain + prefix.split('.').reverse().join('.');
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
return popupData;
|
2014-12-30 22:36:29 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-10 17:23:28 +01:00
|
|
|
var hashFromPopupData = function(reset) {
|
2015-01-24 18:06:22 +01:00
|
|
|
// It makes no sense to offer to refresh the behind-the-scene scope
|
|
|
|
if ( popupData.pageHostname === 'behind-the-scene' ) {
|
|
|
|
uDom('body').toggleClass('dirty', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
var hasher = [],
|
|
|
|
rules = popupData.firewallRules;
|
2015-01-10 17:23:28 +01:00
|
|
|
for ( var key in rules ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
var rule = rules[key];
|
|
|
|
if ( rule !== null ) {
|
|
|
|
hasher.push(rule.src + ' ' + rule.des + ' ' + rule.type + ' ' + rule.action);
|
2015-01-10 17:23:28 +01:00
|
|
|
}
|
2014-06-24 07:20:59 +02:00
|
|
|
}
|
2015-04-05 18:03:14 +02:00
|
|
|
hasher.sort();
|
2015-02-09 21:24:24 +01:00
|
|
|
hasher.push(uDom('body').hasClass('off'));
|
2016-01-17 19:30:43 +01:00
|
|
|
hasher.push(uDom.nodeFromId('no-large-media').classList.contains('on'));
|
2015-06-10 15:23:48 +02:00
|
|
|
hasher.push(uDom.nodeFromId('no-cosmetic-filtering').classList.contains('on'));
|
|
|
|
hasher.push(uDom.nodeFromId('no-remote-fonts').classList.contains('on'));
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-04-05 18:03:14 +02:00
|
|
|
var hash = hasher.join('');
|
2015-01-10 17:23:28 +01:00
|
|
|
if ( reset ) {
|
|
|
|
cachedPopupHash = hash;
|
|
|
|
}
|
|
|
|
uDom('body').toggleClass('dirty', hash !== cachedPopupHash);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var formatNumber = function(count) {
|
|
|
|
return typeof count === 'number' ? count.toLocaleString() : '';
|
2014-06-24 07:20:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-30 22:36:29 +01:00
|
|
|
var rulekeyCompare = function(a, b) {
|
|
|
|
var ha = a.slice(2, a.indexOf(' ', 2));
|
|
|
|
if ( !reIP.test(ha) ) {
|
2015-10-25 14:38:48 +01:00
|
|
|
ha = hostnameToSortableTokenMap[ha] || ' ';
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
|
|
|
var hb = b.slice(2, b.indexOf(' ', 2));
|
|
|
|
if ( !reIP.test(hb) ) {
|
2015-10-25 14:38:48 +01:00
|
|
|
hb = hostnameToSortableTokenMap[hb] || ' ';
|
|
|
|
}
|
|
|
|
var ca = ha.charCodeAt(0),
|
|
|
|
cb = hb.charCodeAt(0);
|
|
|
|
if ( ca !== cb ) {
|
|
|
|
return ca - cb;
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
|
|
|
return ha.localeCompare(hb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
var addFirewallRow = function(des) {
|
2015-01-10 17:23:28 +01:00
|
|
|
var row = rowsToRecycle.pop();
|
|
|
|
if ( row.length === 0 ) {
|
|
|
|
row = uDom('#templates > div:nth-of-type(1)').clone();
|
|
|
|
}
|
|
|
|
|
2014-12-30 22:36:29 +01:00
|
|
|
row.descendants('[data-des]').attr('data-des', des);
|
|
|
|
|
2017-04-22 18:57:56 +02:00
|
|
|
var hnDetails = popupData.hostnameDict[des] || {},
|
2017-04-22 20:26:17 +02:00
|
|
|
isDomain = des === hnDetails.domain,
|
|
|
|
prettyDomainName = punycode.toUnicode(des),
|
|
|
|
isPunycoded = prettyDomainName !== des;
|
2017-04-22 18:57:56 +02:00
|
|
|
var span = row.nodeAt(0).querySelector('span:first-of-type');
|
2017-04-22 20:26:17 +02:00
|
|
|
span.classList.toggle(
|
|
|
|
'isIDN',
|
2017-04-23 01:23:11 +02:00
|
|
|
isPunycoded && reCyrillicAmbiguous.test(prettyDomainName) === true &&
|
|
|
|
reCyrillicNonAmbiguous.test(prettyDomainName) === false
|
2017-04-22 20:26:17 +02:00
|
|
|
);
|
2017-04-22 18:57:56 +02:00
|
|
|
span.querySelector('span').textContent = prettyDomainName;
|
|
|
|
span.title = isDomain && isPunycoded ? des : '';
|
|
|
|
|
2015-03-25 15:12:56 +01:00
|
|
|
row.toggleClass('isDomain', isDomain)
|
|
|
|
.toggleClass('isSubDomain', !isDomain)
|
|
|
|
.toggleClass('allowed', hnDetails.allowCount !== 0)
|
|
|
|
.toggleClass('blocked', hnDetails.blockCount !== 0)
|
|
|
|
.toggleClass('totalAllowed', hnDetails.totalAllowCount !== 0)
|
|
|
|
.toggleClass('totalBlocked', hnDetails.totalBlockCount !== 0);
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
row.appendTo('#firewallContainer');
|
2014-12-30 22:36:29 +01:00
|
|
|
|
|
|
|
return row;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
var updateFirewallCell = function(scope, des, type, rule) {
|
|
|
|
var selector = '#firewallContainer span[data-src="' + scope + '"][data-des="' + des + '"][data-type="' + type + '"]';
|
2016-11-05 19:48:42 +01:00
|
|
|
var cells = uDom(selector);
|
|
|
|
if ( cells.length === 0 ) {
|
2015-01-10 17:23:28 +01:00
|
|
|
return;
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:48:42 +01:00
|
|
|
cells.removeClass();
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( rule !== null ) {
|
|
|
|
cells.toggleClass(rule.action + 'Rule', true);
|
2014-12-31 16:47:19 +01:00
|
|
|
}
|
2014-10-27 17:14:57 +01:00
|
|
|
|
2016-09-27 14:31:12 +02:00
|
|
|
// Use dark shade visual cue if the rule is specific to the cell.
|
2014-12-31 16:47:19 +01:00
|
|
|
var ownRule = false;
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( rule !== null ) {
|
|
|
|
ownRule = (rule.des !== '*' || rule.type === type) &&
|
|
|
|
(rule.des === des) &&
|
|
|
|
(rule.src === scopeToSrcHostnameMap[scope]);
|
2014-10-27 17:14:57 +01:00
|
|
|
}
|
2016-11-05 19:48:42 +01:00
|
|
|
cells.toggleClass('ownRule', ownRule);
|
2014-12-30 22:36:29 +01:00
|
|
|
|
2015-02-07 21:39:46 +01:00
|
|
|
if ( scope !== '.' || des === '*' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember this may be a cell from a reused row, we need to clear text
|
2015-02-07 21:32:20 +01:00
|
|
|
// content if we can't compute request counts.
|
2015-02-07 21:39:46 +01:00
|
|
|
if ( popupData.hostnameDict.hasOwnProperty(des) === false ) {
|
2016-11-05 19:48:42 +01:00
|
|
|
cells.removeAttr('data-acount');
|
|
|
|
cells.removeAttr('data-acount');
|
2014-12-30 22:36:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 21:32:20 +01:00
|
|
|
|
2016-11-05 19:48:42 +01:00
|
|
|
var hnDetails = popupData.hostnameDict[des],
|
|
|
|
cell = cells.nodeAt(0);
|
|
|
|
if ( hnDetails.allowCount !== 0 ) {
|
|
|
|
cell.setAttribute('data-acount', Math.min(Math.ceil(Math.log(hnDetails.allowCount + 1) / Math.LN10), 3));
|
2015-02-17 03:15:09 +01:00
|
|
|
} else {
|
2016-11-05 19:48:42 +01:00
|
|
|
cell.removeAttribute('data-acount');
|
|
|
|
}
|
|
|
|
if ( hnDetails.blockCount !== 0 ) {
|
|
|
|
cell.setAttribute('data-bcount', Math.min(Math.ceil(Math.log(hnDetails.blockCount + 1) / Math.LN10), 3));
|
|
|
|
} else {
|
|
|
|
cell.removeAttribute('data-bcount');
|
2015-02-17 03:15:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( hnDetails.domain !== des ) {
|
2015-01-01 13:29:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 21:32:20 +01:00
|
|
|
|
2016-11-05 19:48:42 +01:00
|
|
|
cell = cells.nodeAt(1);
|
|
|
|
if ( hnDetails.totalAllowCount !== 0 ) {
|
|
|
|
cell.setAttribute('data-acount', Math.min(Math.ceil(Math.log(hnDetails.totalAllowCount + 1) / Math.LN10), 3));
|
|
|
|
} else {
|
|
|
|
cell.removeAttribute('data-acount');
|
|
|
|
}
|
|
|
|
if ( hnDetails.totalBlockCount !== 0 ) {
|
|
|
|
cell.setAttribute('data-bcount', Math.min(Math.ceil(Math.log(hnDetails.totalBlockCount + 1) / Math.LN10), 3));
|
2015-02-17 03:15:09 +01:00
|
|
|
} else {
|
2016-11-05 19:48:42 +01:00
|
|
|
cell.removeAttribute('data-bcount');
|
2015-02-17 03:15:09 +01:00
|
|
|
}
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
var updateAllFirewallCells = function() {
|
|
|
|
var rules = popupData.firewallRules;
|
2015-01-10 17:23:28 +01:00
|
|
|
for ( var key in rules ) {
|
2015-01-10 17:44:37 +01:00
|
|
|
if ( rules.hasOwnProperty(key) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-11 06:26:45 +01:00
|
|
|
updateFirewallCell(
|
2015-01-10 17:23:28 +01:00
|
|
|
key.charAt(0),
|
|
|
|
key.slice(2, key.indexOf(' ', 2)),
|
|
|
|
key.slice(key.lastIndexOf(' ') + 1),
|
|
|
|
rules[key]
|
|
|
|
);
|
|
|
|
}
|
2015-02-11 06:26:45 +01:00
|
|
|
|
2015-05-23 13:31:46 +02:00
|
|
|
positionRulesetTools();
|
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('firewallContainer').classList.toggle(
|
2015-02-11 06:26:45 +01:00
|
|
|
'dirty',
|
|
|
|
popupData.matrixIsDirty === true
|
|
|
|
);
|
2015-01-10 17:23:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
var buildAllFirewallRows = function() {
|
2015-01-10 17:23:28 +01:00
|
|
|
// Do this before removing the rows
|
|
|
|
if ( dfHotspots === null ) {
|
2015-04-22 16:46:10 +02:00
|
|
|
dfHotspots = uDom('#actionSelector')
|
|
|
|
.toggleClass('colorBlind', popupData.colorBlindFriendly)
|
|
|
|
.on('click', 'span', setFirewallRuleHandler);
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
2015-01-10 17:23:28 +01:00
|
|
|
dfHotspots.detach();
|
|
|
|
|
|
|
|
// Remove and reuse all rows: the order may have changed, we can't just
|
|
|
|
// reuse them in-place.
|
2015-07-27 15:20:47 +02:00
|
|
|
rowsToRecycle = uDom('#firewallContainer > div:nth-of-type(7) ~ div').detach();
|
2015-02-06 23:42:09 +01:00
|
|
|
|
2015-02-06 23:46:20 +01:00
|
|
|
var n = allHostnameRows.length;
|
2015-02-06 23:42:09 +01:00
|
|
|
for ( var i = 0; i < n; i++ ) {
|
2015-02-11 06:26:45 +01:00
|
|
|
addFirewallRow(allHostnameRows[i]);
|
2015-02-06 23:42:09 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:48:42 +01:00
|
|
|
if ( dfPaneBuilt !== true && popupData.advancedUserEnabled ) {
|
2015-02-11 06:26:45 +01:00
|
|
|
uDom('#firewallContainer')
|
|
|
|
.on('click', 'span[data-src]', unsetFirewallRuleHandler)
|
2015-02-06 23:42:09 +01:00
|
|
|
.on('mouseenter', '[data-src]', mouseenterCellHandler)
|
|
|
|
.on('mouseleave', '[data-src]', mouseleaveCellHandler);
|
|
|
|
dfPaneBuilt = true;
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
updateAllFirewallCells();
|
2015-02-06 23:42:09 +01:00
|
|
|
};
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-02-06 23:42:09 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var renderPrivacyExposure = function() {
|
2015-01-10 17:23:28 +01:00
|
|
|
allDomains = {};
|
|
|
|
allDomainCount = touchedDomainCount = 0;
|
2015-02-06 23:46:20 +01:00
|
|
|
allHostnameRows = [];
|
2014-12-30 22:36:29 +01:00
|
|
|
|
|
|
|
// Sort hostnames. First-party hostnames must always appear at the top
|
|
|
|
// of the list.
|
2015-01-10 17:23:28 +01:00
|
|
|
var desHostnameDone = {};
|
2015-02-11 06:26:45 +01:00
|
|
|
var keys = Object.keys(popupData.firewallRules)
|
2015-01-10 17:23:28 +01:00
|
|
|
.sort(rulekeyCompare);
|
2015-02-06 23:42:09 +01:00
|
|
|
var key, des, hnDetails;
|
2014-12-30 22:36:29 +01:00
|
|
|
for ( var i = 0; i < keys.length; i++ ) {
|
|
|
|
key = keys[i];
|
2015-01-10 17:23:28 +01:00
|
|
|
des = key.slice(2, key.indexOf(' ', 2));
|
2015-02-04 15:03:34 +01:00
|
|
|
// Specific-type rules -- these are built-in
|
|
|
|
if ( des === '*' || desHostnameDone.hasOwnProperty(des) ) {
|
2015-01-10 17:23:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-06 23:42:09 +01:00
|
|
|
hnDetails = popupData.hostnameDict[des] || {};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 23:46:20 +01:00
|
|
|
allHostnameRows.push(des);
|
2015-01-10 17:23:28 +01:00
|
|
|
desHostnameDone[des] = true;
|
2014-10-06 20:02:44 +02:00
|
|
|
}
|
2014-12-30 22:36:29 +01:00
|
|
|
|
2015-02-06 23:42:09 +01:00
|
|
|
var summary = domainsHitStr.replace('{{count}}', touchedDomainCount.toLocaleString())
|
|
|
|
.replace('{{total}}', allDomainCount.toLocaleString());
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('popupHitDomainCount').textContent = summary;
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-10 17:23:28 +01:00
|
|
|
// Assume everything has to be done incrementally.
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
var renderPopup = function() {
|
2017-11-07 22:38:26 +01:00
|
|
|
var elem, text;
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( popupData.tabTitle ) {
|
|
|
|
document.title = popupData.appName + ' - ' + popupData.tabTitle;
|
2015-03-02 19:49:34 +01:00
|
|
|
}
|
2015-03-12 18:48:43 +01:00
|
|
|
|
2017-11-07 22:38:26 +01:00
|
|
|
elem = document.body;
|
|
|
|
elem.classList.toggle('advancedUser', popupData.advancedUserEnabled);
|
|
|
|
elem.classList.toggle(
|
|
|
|
'off',
|
|
|
|
popupData.pageURL === '' ||
|
|
|
|
!popupData.netFilteringSwitch ||
|
|
|
|
popupData.pageHostname === 'behind-the-scene' && !popupData.advancedUserEnabled
|
|
|
|
);
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-01-21 17:13:32 +01:00
|
|
|
// If you think the `=== true` is pointless, you are mistaken
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('gotoPick').classList.toggle('enabled', popupData.canElementPicker === true);
|
2017-05-27 17:51:24 +02:00
|
|
|
uDom.nodeFromId('gotoZap').classList.toggle('enabled', popupData.canElementPicker === true);
|
2014-07-02 18:02:29 +02:00
|
|
|
|
2017-11-07 22:38:26 +01:00
|
|
|
var blocked = popupData.pageBlockedRequestCount,
|
2016-12-29 05:39:15 +01:00
|
|
|
total = popupData.pageAllowedRequestCount + blocked;
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( total === 0 ) {
|
2015-03-11 16:21:29 +01:00
|
|
|
text = formatNumber(0);
|
2014-06-24 00:42:43 +02:00
|
|
|
} else {
|
2015-03-11 16:21:29 +01:00
|
|
|
text = statsStr.replace('{{count}}', formatNumber(blocked))
|
|
|
|
.replace('{{percent}}', formatNumber(Math.floor(blocked * 100 / total)));
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('page-blocked').textContent = text;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
blocked = popupData.globalBlockedRequestCount;
|
|
|
|
total = popupData.globalAllowedRequestCount + blocked;
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( total === 0 ) {
|
2015-03-11 16:21:29 +01:00
|
|
|
text = formatNumber(0);
|
2014-06-24 00:42:43 +02:00
|
|
|
} else {
|
2015-03-11 16:21:29 +01:00
|
|
|
text = statsStr.replace('{{count}}', formatNumber(blocked))
|
|
|
|
.replace('{{percent}}', formatNumber(Math.floor(blocked * 100 / total)));
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('total-blocked').textContent = text;
|
2015-02-06 23:42:09 +01:00
|
|
|
|
2015-08-01 17:30:54 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/507
|
|
|
|
// Convenience: open the logger with current tab automatically selected
|
|
|
|
if ( popupData.tabId ) {
|
2017-05-28 19:32:08 +02:00
|
|
|
uDom.nodeFromSelector('#basicTools > a[href^="logger-ui.html"]').setAttribute(
|
2015-08-01 17:30:54 +02:00
|
|
|
'href',
|
|
|
|
'logger-ui.html#tab_' + popupData.tabId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-06 23:42:09 +01:00
|
|
|
// This will collate all domains, touched or not
|
|
|
|
renderPrivacyExposure();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
// Extra tools
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('no-popups').classList.toggle('on', popupData.noPopups === true);
|
2016-01-17 19:30:43 +01:00
|
|
|
uDom.nodeFromId('no-large-media').classList.toggle('on', popupData.noLargeMedia === true);
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('no-cosmetic-filtering').classList.toggle('on', popupData.noCosmeticFiltering === true);
|
|
|
|
uDom.nodeFromId('no-remote-fonts').classList.toggle('on', popupData.noRemoteFonts === true);
|
|
|
|
|
2016-01-04 16:48:28 +01:00
|
|
|
// Report blocked popup count on badge
|
|
|
|
total = popupData.popupBlockedCount;
|
|
|
|
uDom.nodeFromSelector('#no-popups > span.badge')
|
|
|
|
.textContent = total ? total.toLocaleString() : '';
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
// Report large media count on badge
|
|
|
|
total = popupData.largeMediaCount;
|
|
|
|
uDom.nodeFromSelector('#no-large-media > span.badge')
|
|
|
|
.textContent = total ? total.toLocaleString() : '';
|
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
// Report remote font count on badge
|
|
|
|
total = popupData.remoteFontCount;
|
|
|
|
uDom.nodeFromSelector('#no-remote-fonts > span.badge')
|
|
|
|
.textContent = total ? total.toLocaleString() : '';
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/470
|
2015-01-10 17:23:28 +01:00
|
|
|
// This must be done here, to be sure the popup is resized properly
|
2016-11-05 19:48:42 +01:00
|
|
|
var dfPaneVisible = popupData.dfEnabled;
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1068
|
2015-03-22 13:51:31 +01:00
|
|
|
// Remember the last state of the firewall pane. This allows to
|
|
|
|
// configure the popup size early next time it is opened, which means a
|
|
|
|
// less glitchy popup at open time.
|
|
|
|
if ( dfPaneVisible !== dfPaneVisibleStored ) {
|
|
|
|
dfPaneVisibleStored = dfPaneVisible;
|
|
|
|
vAPI.localStorage.setItem('popupFirewallPane', dfPaneVisibleStored);
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('panes').classList.toggle('dfEnabled', dfPaneVisible);
|
2017-11-07 22:38:26 +01:00
|
|
|
|
|
|
|
elem = uDom.nodeFromId('firewallContainer');
|
|
|
|
elem.classList.toggle('minimized', popupData.firewallPaneMinimized);
|
|
|
|
elem.classList.toggle('colorBlind', popupData.colorBlindFriendly);
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
// Build dynamic filtering pane only if in use
|
2015-01-10 17:23:28 +01:00
|
|
|
if ( dfPaneVisible ) {
|
2015-02-11 06:26:45 +01:00
|
|
|
buildAllFirewallRows();
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
2017-11-07 22:38:26 +01:00
|
|
|
|
|
|
|
renderTooltips();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2889
|
|
|
|
// Use tooltip for ARIA purpose.
|
|
|
|
|
2017-11-07 23:03:24 +01:00
|
|
|
var renderTooltips = function(selector) {
|
2017-11-08 13:34:22 +01:00
|
|
|
var elem, text;
|
|
|
|
for ( var entry of tooltipTargetSelectors ) {
|
|
|
|
if ( selector !== undefined && entry[0] !== selector ) { continue; }
|
|
|
|
text = vAPI.i18n(
|
|
|
|
entry[1].i18n +
|
|
|
|
(uDom.nodeFromSelector(entry[1].state) === null ? '1' : '2')
|
|
|
|
);
|
|
|
|
elem = uDom.nodeFromSelector(entry[0]);
|
2017-11-07 22:38:26 +01:00
|
|
|
elem.setAttribute('aria-label', text);
|
|
|
|
elem.setAttribute('data-tip', text);
|
2017-11-08 13:34:22 +01:00
|
|
|
if ( selector !== undefined ) {
|
|
|
|
uDom.nodeFromId('tooltip').textContent =
|
|
|
|
elem.getAttribute('data-tip');
|
|
|
|
}
|
2017-11-07 23:03:24 +01:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2017-11-08 13:34:22 +01:00
|
|
|
var tooltipTargetSelectors = new Map([
|
|
|
|
[
|
|
|
|
'#switch',
|
|
|
|
{
|
|
|
|
state: 'body.off',
|
|
|
|
i18n: 'popupPowerSwitchInfo',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#no-popups',
|
|
|
|
{
|
|
|
|
state: '#no-popups.on',
|
|
|
|
i18n: 'popupTipNoPopups'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#no-large-media',
|
|
|
|
{
|
|
|
|
state: '#no-large-media.on',
|
|
|
|
i18n: 'popupTipNoLargeMedia'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#no-cosmetic-filtering',
|
|
|
|
{
|
|
|
|
state: '#no-cosmetic-filtering.on',
|
|
|
|
i18n: 'popupTipNoCosmeticFiltering'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#no-remote-fonts',
|
|
|
|
{
|
|
|
|
state: '#no-remote-fonts.on',
|
|
|
|
i18n: 'popupTipNoRemoteFonts'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-12-29 05:39:15 +01:00
|
|
|
// All rendering code which need to be executed only once.
|
|
|
|
|
|
|
|
var renderOnce = function() {
|
2016-12-29 20:43:20 +01:00
|
|
|
renderOnce = function(){};
|
|
|
|
|
2016-12-29 05:39:15 +01:00
|
|
|
if ( popupData.fontSize !== popupFontSize ) {
|
|
|
|
popupFontSize = popupData.fontSize;
|
|
|
|
if ( popupFontSize !== 'unset' ) {
|
|
|
|
document.body.style.setProperty('font-size', popupFontSize);
|
|
|
|
vAPI.localStorage.setItem('popupFontSize', popupFontSize);
|
|
|
|
} else {
|
|
|
|
document.body.style.removeProperty('font-size');
|
|
|
|
vAPI.localStorage.removeItem('popupFontSize');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uDom.nodeFromId('appname').textContent = popupData.appName;
|
|
|
|
uDom.nodeFromId('version').textContent = popupData.appVersion;
|
|
|
|
|
2016-12-29 20:43:20 +01:00
|
|
|
// For large displays: we 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 more than what is available.
|
|
|
|
// For small displays: we use the whole viewport.
|
|
|
|
|
2017-09-19 18:58:11 +02:00
|
|
|
var panes = uDom.nodeFromId('panes'),
|
|
|
|
rpane = uDom.nodeFromSelector('#panes > div:first-of-type'),
|
2016-12-29 05:39:15 +01:00
|
|
|
lpane = uDom.nodeFromSelector('#panes > div:last-of-type');
|
|
|
|
|
2016-12-29 20:43:20 +01:00
|
|
|
var fillViewport = function() {
|
2017-09-20 05:25:08 +02:00
|
|
|
var newHeight = Math.max(
|
|
|
|
window.innerHeight - uDom.nodeFromSelector('#appinfo').offsetHeight,
|
|
|
|
rpane.offsetHeight
|
2016-12-29 20:43:20 +01:00
|
|
|
);
|
2017-09-20 05:25:08 +02:00
|
|
|
if ( newHeight !== lpane.offsetHeight ) {
|
|
|
|
lpane.style.setProperty('height', newHeight + 'px');
|
|
|
|
}
|
2017-09-19 18:58:11 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3038
|
|
|
|
// - Resize the firewall pane while minding the space between the panes.
|
2017-09-20 05:25:08 +02:00
|
|
|
var newWidth = window.innerWidth - panes.offsetWidth + lpane.offsetWidth;
|
|
|
|
if ( newWidth !== lpane.offsetWidth ) {
|
|
|
|
lpane.style.setProperty('width', newWidth + 'px');
|
|
|
|
}
|
2016-12-29 20:43:20 +01:00
|
|
|
};
|
2016-12-29 05:39:15 +01:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2274
|
|
|
|
// Make use of the whole viewport on mobile devices.
|
|
|
|
if ( document.body.classList.contains('mobile') ) {
|
2016-12-29 20:43:20 +01:00
|
|
|
fillViewport();
|
|
|
|
window.addEventListener('resize', fillViewport);
|
|
|
|
return;
|
2016-12-29 05:39:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 19:02:33 +01:00
|
|
|
if ( document.body.classList.contains('fullsize') === false ) {
|
|
|
|
lpane.style.setProperty('height', rpane.offsetHeight + 'px');
|
|
|
|
}
|
2016-12-29 05:39:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-05 18:03:14 +02:00
|
|
|
var renderPopupLazy = function() {
|
2016-08-06 18:05:01 +02:00
|
|
|
messaging.send('popupPanel', { what: 'getPopupLazyData', tabId: popupData.tabId });
|
|
|
|
};
|
|
|
|
|
|
|
|
var onPopupMessage = function(data) {
|
|
|
|
if ( !data ) { return; }
|
|
|
|
if ( data.tabId !== popupData.tabId ) { return; }
|
|
|
|
|
|
|
|
switch ( data.what ) {
|
|
|
|
case 'cosmeticallyFilteredElementCountChanged':
|
|
|
|
var v = data.count || '';
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromSelector('#no-cosmetic-filtering > span.badge')
|
|
|
|
.textContent = typeof v === 'number' ? v.toLocaleString() : v;
|
2016-08-06 18:05:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-04-05 18:03:14 +02:00
|
|
|
};
|
|
|
|
|
2016-08-06 18:05:01 +02:00
|
|
|
messaging.addChannelListener('popup', onPopupMessage);
|
|
|
|
|
2015-04-05 18:03:14 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 17:40:27 +02:00
|
|
|
var toggleNetFilteringSwitch = function(ev) {
|
2017-11-07 22:38:26 +01:00
|
|
|
if ( !popupData || !popupData.pageURL ) { return; }
|
|
|
|
if (
|
|
|
|
popupData.pageHostname === 'behind-the-scene' &&
|
|
|
|
!popupData.advancedUserEnabled
|
|
|
|
) {
|
2015-01-24 18:06:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'toggleNetFiltering',
|
|
|
|
url: popupData.pageURL,
|
|
|
|
scope: ev.ctrlKey || ev.metaKey ? 'page' : '',
|
|
|
|
state: !uDom('body').toggleClass('off').hasClass('off'),
|
|
|
|
tabId: popupData.tabId
|
|
|
|
}
|
|
|
|
);
|
2017-11-07 23:03:24 +01:00
|
|
|
renderTooltips('#switch');
|
2015-01-10 17:23:28 +01:00
|
|
|
hashFromPopupData();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-27 17:51:24 +02:00
|
|
|
var gotoZap = function() {
|
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'launchElementPicker',
|
|
|
|
tabId: popupData.tabId,
|
|
|
|
zap: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
vAPI.closePopup();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
var gotoPick = function() {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'launchElementPicker',
|
|
|
|
tabId: popupData.tabId
|
|
|
|
}
|
|
|
|
);
|
2014-12-25 14:53:30 +01:00
|
|
|
|
|
|
|
vAPI.closePopup();
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-25 14:53:30 +01:00
|
|
|
var gotoURL = function(ev) {
|
2017-09-26 22:09:35 +02:00
|
|
|
if ( this.hasAttribute('href') === false ) {
|
2014-10-20 17:40:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'gotoURL',
|
|
|
|
details: {
|
|
|
|
url: this.getAttribute('href'),
|
|
|
|
select: true,
|
|
|
|
index: -1,
|
2016-09-16 23:41:17 +02:00
|
|
|
shiftKey: ev.shiftKey
|
2016-03-06 16:51:06 +01:00
|
|
|
}
|
2014-10-20 17:40:52 +02:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
);
|
2014-12-25 14:53:30 +01:00
|
|
|
|
|
|
|
vAPI.closePopup();
|
2014-07-02 18:02:29 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
var toggleFirewallPane = function() {
|
2015-01-06 14:01:15 +01:00
|
|
|
popupData.dfEnabled = !popupData.dfEnabled;
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'userSettings',
|
|
|
|
name: 'dynamicFilteringEnabled',
|
|
|
|
value: popupData.dfEnabled
|
|
|
|
}
|
|
|
|
);
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/996
|
2015-03-22 13:51:31 +01:00
|
|
|
// Remember the last state of the firewall pane. This allows to
|
|
|
|
// configure the popup size early next time it is opened, which means a
|
|
|
|
// less glitchy popup at open time.
|
|
|
|
dfPaneVisibleStored = popupData.dfEnabled;
|
|
|
|
vAPI.localStorage.setItem('popupFirewallPane', dfPaneVisibleStored);
|
|
|
|
|
2015-01-10 17:23:28 +01:00
|
|
|
// Dynamic filtering pane may not have been built yet
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('panes').classList.toggle('dfEnabled', popupData.dfEnabled);
|
2015-01-10 17:23:28 +01:00
|
|
|
if ( popupData.dfEnabled && dfPaneBuilt === false ) {
|
2015-02-11 06:26:45 +01:00
|
|
|
buildAllFirewallRows();
|
2015-01-10 17:23:28 +01:00
|
|
|
}
|
2014-12-31 16:47:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var mouseenterCellHandler = function() {
|
|
|
|
if ( uDom(this).hasClass('ownRule') === false ) {
|
2015-01-06 14:01:15 +01:00
|
|
|
dfHotspots.appendTo(this);
|
2014-12-31 16:47:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var mouseleaveCellHandler = function() {
|
2015-01-06 14:01:15 +01:00
|
|
|
dfHotspots.detach();
|
2014-12-31 16:47:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 21:48:39 +01:00
|
|
|
var setFirewallRule = function(src, des, type, action, persist) {
|
2014-12-28 16:07:43 +01:00
|
|
|
// This can happen on pages where uBlock does not work
|
2015-01-06 14:01:15 +01:00
|
|
|
if ( typeof popupData.pageHostname !== 'string' || popupData.pageHostname === '' ) {
|
2014-12-28 16:07:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-11 06:26:45 +01:00
|
|
|
var onFirewallRuleChanged = function(response) {
|
2015-01-06 14:01:15 +01:00
|
|
|
cachePopupData(response);
|
2015-02-11 06:26:45 +01:00
|
|
|
updateAllFirewallCells();
|
2015-01-10 17:23:28 +01:00
|
|
|
hashFromPopupData();
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'toggleFirewallRule',
|
|
|
|
tabId: popupData.tabId,
|
|
|
|
pageHostname: popupData.pageHostname,
|
|
|
|
srcHostname: src,
|
|
|
|
desHostname: des,
|
|
|
|
requestType: type,
|
|
|
|
action: action,
|
|
|
|
persist: persist
|
|
|
|
},
|
|
|
|
onFirewallRuleChanged
|
|
|
|
);
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 21:48:39 +01:00
|
|
|
var unsetFirewallRuleHandler = function(ev) {
|
2014-12-31 16:47:19 +01:00
|
|
|
var cell = uDom(this);
|
2015-02-11 06:26:45 +01:00
|
|
|
setFirewallRule(
|
2015-01-06 14:01:15 +01:00
|
|
|
cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
|
2014-12-31 16:47:19 +01:00
|
|
|
cell.attr('data-des'),
|
|
|
|
cell.attr('data-type'),
|
2015-02-11 21:48:39 +01:00
|
|
|
0,
|
|
|
|
ev.ctrlKey || ev.metaKey
|
2014-12-31 16:47:19 +01:00
|
|
|
);
|
2015-01-06 14:01:15 +01:00
|
|
|
dfHotspots.appendTo(cell);
|
2014-12-31 16:47:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 21:48:39 +01:00
|
|
|
var setFirewallRuleHandler = function(ev) {
|
2014-12-31 16:47:19 +01:00
|
|
|
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' ) {
|
2015-01-10 17:44:37 +01:00
|
|
|
action = 3;
|
2014-12-31 16:47:19 +01:00
|
|
|
} else {
|
|
|
|
action = 1;
|
|
|
|
}
|
2015-02-11 06:26:45 +01:00
|
|
|
setFirewallRule(
|
2015-01-06 14:01:15 +01:00
|
|
|
cell.attr('data-src') === '/' ? '*' : popupData.pageHostname,
|
2014-12-31 16:47:19 +01:00
|
|
|
cell.attr('data-des'),
|
|
|
|
cell.attr('data-type'),
|
2015-02-11 21:48:39 +01:00
|
|
|
action,
|
|
|
|
ev.ctrlKey || ev.metaKey
|
2014-12-31 16:47:19 +01:00
|
|
|
);
|
2015-01-06 14:01:15 +01:00
|
|
|
dfHotspots.detach();
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-01-07 23:53:33 +01:00
|
|
|
var reloadTab = function(ev) {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'reloadTab',
|
|
|
|
tabId: popupData.tabId,
|
2018-01-07 23:53:33 +01:00
|
|
|
select: true,
|
2018-01-08 13:33:38 +01:00
|
|
|
bypassCache: ev.ctrlKey || ev.metaKey || ev.shiftKey
|
2016-03-06 16:51:06 +01:00
|
|
|
}
|
|
|
|
);
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2015-01-10 17:23:28 +01:00
|
|
|
// Polling will take care of refreshing the popup content
|
2015-02-09 21:24:24 +01:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/748
|
2015-02-09 21:24:24 +01:00
|
|
|
// User forces a reload, assume the popup has to be updated regardless if
|
|
|
|
// there were changes or not.
|
|
|
|
popupData.contentLastModified = -1;
|
|
|
|
|
2015-02-09 21:35:47 +01:00
|
|
|
// No need to wait to remove this.
|
2015-02-09 21:24:24 +01:00
|
|
|
uDom('body').toggleClass('dirty', false);
|
2015-01-10 17:23:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-02-06 15:50:02 +01:00
|
|
|
var toggleMinimize = function(ev) {
|
|
|
|
// Special display mode: in its own tab/window, with no vertical restraint.
|
|
|
|
// Useful to take snapshots of the whole list of domains -- example:
|
|
|
|
// https://github.com/gorhill/uBlock/issues/736#issuecomment-178879944
|
|
|
|
if ( ev.shiftKey && ev.ctrlKey ) {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'gotoURL',
|
|
|
|
details: {
|
|
|
|
url: 'popup.html?tabId=' + popupData.tabId + '&fullsize=1',
|
|
|
|
select: true,
|
|
|
|
index: -1
|
|
|
|
}
|
2016-02-06 15:50:02 +01:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
);
|
2016-02-06 15:50:02 +01:00
|
|
|
vAPI.closePopup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
popupData.firewallPaneMinimized = uDom.nodeFromId('firewallContainer')
|
|
|
|
.classList
|
|
|
|
.toggle('minimized');
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'userSettings',
|
|
|
|
name: 'firewallPaneMinimized',
|
|
|
|
value: popupData.firewallPaneMinimized
|
|
|
|
}
|
|
|
|
);
|
2015-05-23 13:31:46 +02:00
|
|
|
positionRulesetTools();
|
2015-02-17 03:15:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 21:48:39 +01:00
|
|
|
var saveFirewallRules = function() {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'saveFirewallRules',
|
|
|
|
srcHostname: popupData.pageHostname,
|
|
|
|
desHostnames: popupData.hostnameDict
|
|
|
|
}
|
|
|
|
);
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('firewallContainer').classList.remove('dirty');
|
2015-02-11 06:26:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-24 14:36:19 +02:00
|
|
|
var revertFirewallRules = function() {
|
|
|
|
var onFirewallRuleChanged = function(response) {
|
|
|
|
cachePopupData(response);
|
|
|
|
updateAllFirewallCells();
|
|
|
|
hashFromPopupData();
|
|
|
|
};
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'revertFirewallRules',
|
|
|
|
srcHostname: popupData.pageHostname,
|
|
|
|
desHostnames: popupData.hostnameDict,
|
|
|
|
tabId: popupData.tabId
|
|
|
|
},
|
|
|
|
onFirewallRuleChanged
|
|
|
|
);
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('firewallContainer').classList.remove('dirty');
|
2015-04-24 14:36:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
var toggleHostnameSwitch = function(ev) {
|
|
|
|
var target = ev.currentTarget;
|
|
|
|
var switchName = target.getAttribute('id');
|
2017-11-08 13:34:22 +01:00
|
|
|
if ( !switchName ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
target.classList.toggle('on');
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{
|
|
|
|
what: 'toggleHostnameSwitch',
|
|
|
|
name: switchName,
|
|
|
|
hostname: popupData.pageHostname,
|
|
|
|
state: target.classList.contains('on'),
|
|
|
|
tabId: popupData.tabId
|
|
|
|
}
|
|
|
|
);
|
2017-11-08 13:34:22 +01:00
|
|
|
renderTooltips('#' + switchName);
|
2015-04-05 18:03:14 +02:00
|
|
|
hashFromPopupData();
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-10 17:23:28 +01:00
|
|
|
// Poll for changes.
|
|
|
|
//
|
|
|
|
// I couldn't find a better way to be notified of changes which can affect
|
|
|
|
// popup content, as the messaging API doesn't support firing events accurately
|
|
|
|
// from the main extension process to a specific auxiliary extension process:
|
|
|
|
//
|
|
|
|
// - broadcasting() is not an option given there could be a lot of tabs opened,
|
2014-12-25 14:53:30 +01:00
|
|
|
// and maybe even many frames within these tabs, i.e. unacceptable overhead
|
2015-01-10 17:32:24 +01:00
|
|
|
// regardless of whether the popup is opened or not.
|
2015-01-10 17:23:28 +01:00
|
|
|
//
|
|
|
|
// - Modifying the messaging API is not an option, as this would require
|
|
|
|
// revisiting all platform-specific code to support targeted broadcasting,
|
|
|
|
// which who knows could be not so trivial for some platforms.
|
|
|
|
//
|
2014-12-25 14:53:30 +01:00
|
|
|
// A well done polling is a better anyways IMO, I prefer that data is pulled
|
2015-01-10 17:32:24 +01:00
|
|
|
// on demand rather than forcing the main process to assume a client may need
|
|
|
|
// it and thus having to push it all the time unconditionally.
|
2015-01-10 17:23:28 +01:00
|
|
|
|
|
|
|
var pollForContentChange = (function() {
|
|
|
|
var pollTimer = null;
|
|
|
|
|
|
|
|
var pollCallback = function() {
|
|
|
|
pollTimer = null;
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
2015-01-10 17:23:28 +01:00
|
|
|
{
|
|
|
|
what: 'hasPopupContentChanged',
|
|
|
|
tabId: popupData.tabId,
|
|
|
|
contentLastModified: popupData.contentLastModified
|
|
|
|
},
|
|
|
|
queryCallback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var queryCallback = function(response) {
|
|
|
|
if ( response ) {
|
2015-03-02 19:49:34 +01:00
|
|
|
getPopupData(popupData.tabId);
|
2015-01-10 17:23:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
poll();
|
|
|
|
};
|
|
|
|
|
|
|
|
var poll = function() {
|
|
|
|
if ( pollTimer !== null ) {
|
2015-01-06 14:01:15 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-05-17 19:02:56 +02:00
|
|
|
pollTimer = vAPI.setTimeout(pollCallback, 1500);
|
2015-01-10 17:23:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return poll;
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-02 19:49:34 +01:00
|
|
|
var getPopupData = function(tabId) {
|
2015-01-10 17:23:28 +01:00
|
|
|
var onDataReceived = function(response) {
|
|
|
|
cachePopupData(response);
|
2016-12-29 05:39:15 +01:00
|
|
|
renderOnce();
|
2015-01-06 14:01:15 +01:00
|
|
|
renderPopup();
|
2015-04-05 18:03:14 +02:00
|
|
|
renderPopupLazy(); // low priority rendering
|
2015-01-10 17:23:28 +01:00
|
|
|
hashFromPopupData(true);
|
|
|
|
pollForContentChange();
|
|
|
|
};
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'popupPanel',
|
|
|
|
{ what: 'getPopupData', tabId: tabId },
|
|
|
|
onDataReceived
|
|
|
|
);
|
2015-01-10 17:23:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-05 20:44:41 +02:00
|
|
|
var onShowTooltip = function() {
|
2015-12-13 06:56:30 +01:00
|
|
|
if ( popupData.tooltipsDisabled ) {
|
2015-04-05 20:44:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var target = this;
|
|
|
|
|
2015-12-13 06:56:30 +01:00
|
|
|
// Tooltip container
|
|
|
|
var ttc = uDom(target).ancestors('.tooltipContainer').nodeAt(0) ||
|
|
|
|
document.body;
|
|
|
|
var ttcRect = ttc.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Tooltip itself
|
|
|
|
var tip = uDom.nodeFromId('tooltip');
|
2015-04-05 20:44:41 +02:00
|
|
|
tip.textContent = target.getAttribute('data-tip');
|
|
|
|
tip.style.removeProperty('top');
|
|
|
|
tip.style.removeProperty('bottom');
|
2015-12-13 06:56:30 +01:00
|
|
|
ttc.appendChild(tip);
|
|
|
|
|
|
|
|
// Target rect
|
|
|
|
var targetRect = target.getBoundingClientRect();
|
2015-04-05 20:44:41 +02:00
|
|
|
|
|
|
|
// Default is "over"
|
|
|
|
var pos;
|
|
|
|
var over = target.getAttribute('data-tip-position') !== 'under';
|
|
|
|
if ( over ) {
|
2015-12-13 06:56:30 +01:00
|
|
|
pos = ttcRect.height - targetRect.top + ttcRect.top;
|
2015-04-05 20:44:41 +02:00
|
|
|
tip.style.setProperty('bottom', pos + 'px');
|
|
|
|
} else {
|
2015-12-13 06:56:30 +01:00
|
|
|
pos = targetRect.bottom - ttcRect.top;
|
2015-04-05 20:44:41 +02:00
|
|
|
tip.style.setProperty('top', pos + 'px');
|
|
|
|
}
|
|
|
|
|
2015-12-13 06:56:30 +01:00
|
|
|
tip.classList.add('show');
|
2015-04-05 20:44:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var onHideTooltip = function() {
|
2015-06-10 15:23:48 +02:00
|
|
|
uDom.nodeFromId('tooltip').classList.remove('show');
|
2015-04-05 20:44:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-01 17:30:54 +02:00
|
|
|
// Popup DOM is assumed to be loaded at this point -- because this script
|
|
|
|
// is loaded after everything else..
|
2015-01-10 17:23:28 +01:00
|
|
|
|
2015-08-01 17:34:31 +02:00
|
|
|
(function() {
|
|
|
|
// If there's no tab id specified in the query string,
|
|
|
|
// it will default to current tab.
|
|
|
|
var tabId = null;
|
|
|
|
|
|
|
|
// Extract the tab id of the page this popup is for
|
|
|
|
var matches = window.location.search.match(/[\?&]tabId=([^&]+)/);
|
|
|
|
if ( matches && matches.length === 2 ) {
|
|
|
|
tabId = matches[1];
|
|
|
|
}
|
|
|
|
getPopupData(tabId);
|
|
|
|
|
|
|
|
uDom('#switch').on('click', toggleNetFilteringSwitch);
|
2017-05-27 17:51:24 +02:00
|
|
|
uDom('#gotoZap').on('click', gotoZap);
|
2015-08-01 17:34:31 +02:00
|
|
|
uDom('#gotoPick').on('click', gotoPick);
|
|
|
|
uDom('h2').on('click', toggleFirewallPane);
|
|
|
|
uDom('#refresh').on('click', reloadTab);
|
|
|
|
uDom('.hnSwitch').on('click', toggleHostnameSwitch);
|
|
|
|
uDom('#saveRules').on('click', saveFirewallRules);
|
|
|
|
uDom('#revertRules').on('click', revertFirewallRules);
|
|
|
|
uDom('[data-i18n="popupAnyRulePrompt"]').on('click', toggleMinimize);
|
|
|
|
|
|
|
|
uDom('body').on('mouseenter', '[data-tip]', onShowTooltip)
|
|
|
|
.on('mouseleave', '[data-tip]', onHideTooltip);
|
2017-07-03 15:51:34 +02:00
|
|
|
|
2017-07-07 17:23:24 +02:00
|
|
|
uDom('a[href]').on('click', gotoURL);
|
2015-08-01 17:34:31 +02:00
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|