1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-06 19:02:30 +01:00

fixed popup blocker switch; added ability to manually edit switches

This commit is contained in:
gorhill 2015-04-09 11:19:31 -04:00
parent fcfdfdf9c3
commit 651c25c839
11 changed files with 117 additions and 75 deletions

View File

@ -71,7 +71,7 @@ var proceedTemporary = function() {
var proceedPermanent = function() {
messager.send({
what: 'toggleHostnameSwitch',
name: 'noStrictBlocking',
name: 'no-strict-blocking',
hostname: getTargetHostname(),
deep: true,
state: true

View File

@ -34,7 +34,23 @@ var messager = vAPI.messaging.channel('dyna-rules.js');
/******************************************************************************/
var renderRules = function(details) {
var liTemplate = uDom('#templates > ul > li');
var ulLeft = uDom('#diff > .left ul').empty().remove();
var ulRight = uDom('#diff > .right ul').empty().remove();
var liLeft, liRight;
var rules, rule, i;
// Switches always displayed first -- just like in uMatrix
rules = details.hnSwitches.split(/\n+/).sort();
for ( i = 0; i < rules.length; i++ ) {
rule = rules[i];
liLeft = liTemplate.clone().text(rule);
liRight = liTemplate.clone().text(rule);
ulLeft.append(liLeft);
ulRight.append(liRight);
}
// Firewall rules follow
var allRules = {};
var permanentRules = {};
var sessionRules = {};
@ -62,11 +78,6 @@ var renderRules = function(details) {
}
details.permanentRules = rules.sort().join('\n');
var liTemplate = uDom('#templates > ul > li');
var ulLeft = uDom('#diff > .left ul').empty();
var ulRight = uDom('#diff > .right ul').empty();
var liLeft, liRight;
rules = Object.keys(allRules).sort();
for ( i = 0; i < rules.length; i++ ) {
rule = rules[i];
@ -87,6 +98,8 @@ var renderRules = function(details) {
ulRight.append(liRight);
}
uDom('#diff > .left > .rulesContainer').append(ulLeft);
uDom('#diff > .right > .rulesContainer').append(ulRight);
uDom('#diff').toggleClass('dirty', details.sessionRules !== details.permanentRules);
};

View File

@ -37,14 +37,17 @@ var HnSwitches = function() {
/******************************************************************************/
var switchBitOffsets = {
'noStrictBlocking': 0,
'noPopups': 2,
'noCosmeticFiltering': 4
'no-strict-blocking': 0,
'no-popups': 2,
'no-cosmetic-filtering': 4
};
var fromLegacySwitchNames = {
'dontBlockDoc': 'noStrictBlocking',
'doBlockAllPopups': 'noPopups'
'dontBlockDoc': 'no-strict-blocking',
'doBlockAllPopups': 'no-popups',
'noStrictBlocking': 'no-strict-blocking',
'noPopups': 'no-popups',
'noCosmeticFiltering': 'no-cosmetic-filtering'
};
var switchStateToNameMap = {
@ -279,6 +282,8 @@ HnSwitches.prototype.fromString = function(text) {
var fields;
var switchName, hostname, state;
this.reset();
while ( lineBeg < textEnd ) {
lineEnd = text.indexOf('\n', lineBeg);
if ( lineEnd < 0 ) {

View File

@ -234,9 +234,9 @@ var getStats = function(tabId, tabTitle) {
r.firewallRules = getFirewallRules(tabContext.rootHostname, r.hostnameDict);
r.canElementPicker = tabContext.rootHostname.indexOf('.') !== -1;
r.canRequestLog = canRequestLog;
r.noPopups = µb.hnSwitches.evaluateZ('noPopups', tabContext.rootHostname);
r.noStrictBlocking = µb.hnSwitches.evaluateZ('noStrictBlocking', tabContext.rootHostname);
r.noCosmeticFiltering = µb.hnSwitches.evaluateZ('noCosmeticFiltering', tabContext.rootHostname);
r.noPopups = µb.hnSwitches.evaluateZ('no-popups', tabContext.rootHostname);
r.noStrictBlocking = µb.hnSwitches.evaluateZ('no-strict-blocking', tabContext.rootHostname);
r.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname);
} else {
r.hostnameDict = {};
r.firewallRules = getFirewallRules();
@ -832,10 +832,44 @@ var µb = µBlock;
/******************************************************************************/
var getFirewallRules = function() {
var getRules = function() {
return {
permanentRules: µb.permanentFirewall.toString(),
sessionRules: µb.sessionFirewall.toString()
sessionRules: µb.sessionFirewall.toString(),
hnSwitches: µb.hnSwitches.toString()
};
};
// Untangle rules and switches.
var untangle = function(s) {
var textEnd = s.length;
var lineBeg = 0, lineEnd;
var line;
var rules = [];
var switches = [];
while ( lineBeg < textEnd ) {
lineEnd = s.indexOf('\n', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = s.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = s.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
// Switches always contain a ':'
if ( line.indexOf(':') === -1 ) {
rules.push(line);
} else {
switches.push(line);
}
}
return {
rules: rules.join('\n'),
switches: switches.join('\n')
};
};
@ -849,25 +883,31 @@ var onMessage = function(request, sender, callback) {
}
// Sync
var r;
var response;
switch ( request.what ) {
case 'getFirewallRules':
response = getFirewallRules();
response = getRules();
break;
case 'setSessionFirewallRules':
// https://github.com/chrisaljoudi/uBlock/issues/772
µb.cosmeticFilteringEngine.removeFromSelectorCache('*');
µb.sessionFirewall.fromString(request.rules);
response = getFirewallRules();
r = untangle(request.rules);
µb.sessionFirewall.fromString(r.rules);
µb.hnSwitches.fromString(r.switches);
µb.saveHostnameSwitches();
response = getRules();
break;
case 'setPermanentFirewallRules':
µb.permanentFirewall.fromString(request.rules);
r = untangle(request.rules);
µb.permanentFirewall.fromString(r.rules);
µb.savePermanentFirewallRules();
response = getFirewallRules();
µb.hnSwitches.fromString(r.switches);
µb.saveHostnameSwitches();
response = getRules();
break;
default:

View File

@ -497,11 +497,7 @@ PageStore.prototype.init = function(tabId) {
/******************************************************************************/
PageStore.prototype.reuse = function(context) {
// We can't do this: when force refreshing a page, the page store data
// needs to be reset
//if ( pageURL === this.pageURL ) {
// return this;
//}
// When force refreshing a page, the page store data needs to be reset.
// If the hostname changes, we can't merely just update the context.
var tabContext = µb.tabContextManager.lookup(this.tabId);
@ -635,7 +631,7 @@ PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
var tabContext = µb.tabContextManager.lookup(this.tabId);
if ( µb.hnSwitches.evaluateZ('noCosmeticFiltering', tabContext.rootHostname) ) {
if ( µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) ) {
return false;
}

View File

@ -145,7 +145,7 @@ var hashFromPopupData = function(reset) {
}
hasher.sort();
hasher.push(uDom('body').hasClass('off'));
hasher.push(uDom('#noCosmeticFiltering').hasClass('on'));
hasher.push(uDom('#no-cosmetic-filtering').hasClass('on'));
var hash = hasher.join('');
if ( reset ) {
@ -432,9 +432,9 @@ var renderPopup = function() {
renderPrivacyExposure();
// Extra tools
uDom('#noPopups').toggleClass('on', popupData.noPopups === true);
uDom('#noStrictBlocking').toggleClass('on', popupData.noStrictBlocking === true);
uDom('#noCosmeticFiltering').toggleClass('on', popupData.noCosmeticFiltering === true);
uDom('#no-popups').toggleClass('on', popupData.noPopups === true);
uDom('#no-strict-blocking').toggleClass('on', popupData.noStrictBlocking === true);
uDom('#no-cosmetic-filtering').toggleClass('on', popupData.noCosmeticFiltering === true);
// https://github.com/chrisaljoudi/uBlock/issues/470
// This must be done here, to be sure the popup is resized properly
@ -463,7 +463,7 @@ var renderPopup = function() {
var renderPopupLazy = function() {
var onDataReady = function(data) {
var v = data.hiddenElementCount || '';
uDom('#noCosmeticFiltering > span.badge').text(
uDom('#no-cosmetic-filtering > span.badge').text(
typeof v === 'number' ? v.toLocaleString() : v
);
};

View File

@ -413,11 +413,10 @@ vAPI.tabs.onClosed = function(tabId) {
vAPI.tabs.onPopup = function(details) {
//console.debug('vAPI.tabs.onPopup: details = %o', details);
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
var tabContext = µb.tabContextManager.lookup(details.openerTabId);
var openerURL = details.openerURL || '';
if ( openerURL === '' && pageStore ) {
openerURL = pageStore.pageURL;
if ( openerURL === '' && tabContext.tabId === details.openerTabId ) {
openerURL = tabContext.normalURL;
}
if ( openerURL === '' ) {
@ -452,8 +451,8 @@ vAPI.tabs.onPopup = function(details) {
var result = '';
// Check user switch first
if ( µb.hnSwitches.evaluateZ('noPopups', openerHostname) ) {
result = 'ub:noPopups true';
if ( µb.hnSwitches.evaluateZ('no-popups', openerHostname) ) {
result = 'ub:no-popups true';
}
// https://github.com/chrisaljoudi/uBlock/issues/323
@ -468,6 +467,7 @@ vAPI.tabs.onPopup = function(details) {
}
// https://github.com/chrisaljoudi/uBlock/issues/91
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
if ( pageStore ) {
pageStore.logRequest(context, result);
}
@ -545,18 +545,6 @@ vAPI.tabs.registerListeners();
/******************************************************************************/
µb.pageUrlFromTabId = function(tabId) {
var pageStore = this.pageStores[tabId];
return pageStore ? pageStore.pageURL : '';
};
µb.pageUrlFromPageStats = function(pageStats) {
if ( pageStats ) {
return pageStats.pageURL;
}
return '';
};
µb.pageStoreFromTabId = function(tabId) {
return this.pageStores[tabId];
};
@ -583,7 +571,7 @@ var pageStoreJanitor = function() {
var checkTab = function(tabId) {
vapiTabs.get(tabId, function(tab) {
if ( !tab ) {
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µb.pageUrlFromTabId(tabId));
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µtabId);
µb.unbindTabFromPageStats(tabId);
}
});

View File

@ -154,8 +154,8 @@ var onBeforeRootFrameRequest = function(details) {
var result = '';
// Permanently unrestricted?
if ( result === '' && µb.hnSwitches.evaluateZ('noStrictBlocking', requestHostname) ) {
result = 'ua:noStrictBlocking true';
if ( result === '' && µb.hnSwitches.evaluateZ('no-strict-blocking', requestHostname) ) {
result = 'ua:no-strict-blocking true';
}
// Temporarily whitelisted?

View File

@ -324,7 +324,7 @@ var matchWhitelistDirective = function(url, hostname, directive) {
}
// Take action if needed
if ( details.name === 'noCosmeticFiltering' ) {
if ( details.name === 'no-cosmetic-filtering' ) {
vAPI.tabs.injectScript(details.tabId, {
file: 'js/cosmetic-' + (details.state ? 'off' : 'on') + '.js',
allFrames: true

View File

@ -18,7 +18,7 @@
<p class="statName">
<span data-i18n="popupBlockedOnThisPagePrompt">&nbsp;</span>&ensp;
<span id="gotoPick" class="fa tool" data-i18n-tip="popupTipPicker">&#xf1fb;</span>&ensp;
<a href="#" target="_blank" id="gotoLog" class="fa tool" data-i18n-tip="popupTipLog">&#xf06e;</a>
<a href="#" target="_blank" id="gotoLog" class="fa tool" data-i18n-tip="popupTipLog">&#xf022;</a>
</p>
<p class="statValue" id="page-blocked">?</p>
<p class="statName" data-i18n="popupBlockedSinceInstallPrompt">&nbsp;</p>
@ -26,9 +26,9 @@
<h2 data-i18n="popupHitDomainCountPrompt">&nbsp;</h2>
<p class="statValue" id="popupHitDomainCount">&nbsp;</p>
<div id="extraTools">
<span id="noPopups" class="hnSwitch fa" data-i18n-tip="popupTipNoPopups">&#xf0c5;<span></span></span>
<span id="noStrictBlocking" class="hnSwitch fa" data-i18n-tip="popupTipNoStrictBlocking">&#xf071;<span></span></span>
<span id="noCosmeticFiltering" class="hnSwitch fa" data-i18n-tip="popupTipNoCosmeticFiltering">&#xf070;<span class="badge"></span><span></span></span>
<span id="no-popups" class="hnSwitch fa" data-i18n-tip="popupTipNoPopups">&#xf0c5;<span></span></span>
<span id="no-strict-blocking" class="hnSwitch fa" data-i18n-tip="popupTipNoStrictBlocking">&#xf071;<span></span></span>
<span id="no-cosmetic-filtering" class="hnSwitch fa" data-i18n-tip="popupTipNoCosmeticFiltering">&#xf070;<span class="badge"></span><span></span></span>
</div>
<div id="refresh" class="fa">&#xf021;</div>
<div id="tooltip"></div>