Skip to content

Commit 651c25c

Browse files
committed
fixed popup blocker switch; added ability to manually edit switches
1 parent fcfdfdf commit 651c25c

11 files changed

Lines changed: 120 additions & 78 deletions

File tree

src/dyna-rules.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h2 data-i18n="rulesPermanentHeader"></h2>
2020
<button type="button" id="revertButton" data-i18n="rulesRevert"></button>
2121
</div>
2222
<div class="rulesContainer">
23-
<ul></ul>
23+
<ul></ul>
2424
</div>
2525
</div>
2626
<div class="pane right">
@@ -33,8 +33,8 @@ <h2 data-i18n="rulesTemporaryHeader"></h2>
3333
<button type="button" id="importButton" data-i18n="rulesImport"></button>
3434
</div>
3535
<div class="rulesContainer">
36-
<textarea spellcheck="false"></textarea>
37-
<ul></ul>
36+
<textarea spellcheck="false"></textarea>
37+
<ul></ul>
3838
</div>
3939
</div>
4040

src/js/document-blocked.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var proceedTemporary = function() {
7171
var proceedPermanent = function() {
7272
messager.send({
7373
what: 'toggleHostnameSwitch',
74-
name: 'noStrictBlocking',
74+
name: 'no-strict-blocking',
7575
hostname: getTargetHostname(),
7676
deep: true,
7777
state: true

src/js/dyna-rules.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,23 @@ var messager = vAPI.messaging.channel('dyna-rules.js');
3434
/******************************************************************************/
3535

3636
var renderRules = function(details) {
37+
var liTemplate = uDom('#templates > ul > li');
38+
var ulLeft = uDom('#diff > .left ul').empty().remove();
39+
var ulRight = uDom('#diff > .right ul').empty().remove();
40+
var liLeft, liRight;
3741
var rules, rule, i;
42+
43+
// Switches always displayed first -- just like in uMatrix
44+
rules = details.hnSwitches.split(/\n+/).sort();
45+
for ( i = 0; i < rules.length; i++ ) {
46+
rule = rules[i];
47+
liLeft = liTemplate.clone().text(rule);
48+
liRight = liTemplate.clone().text(rule);
49+
ulLeft.append(liLeft);
50+
ulRight.append(liRight);
51+
}
52+
53+
// Firewall rules follow
3854
var allRules = {};
3955
var permanentRules = {};
4056
var sessionRules = {};
@@ -62,11 +78,6 @@ var renderRules = function(details) {
6278
}
6379
details.permanentRules = rules.sort().join('\n');
6480

65-
var liTemplate = uDom('#templates > ul > li');
66-
var ulLeft = uDom('#diff > .left ul').empty();
67-
var ulRight = uDom('#diff > .right ul').empty();
68-
var liLeft, liRight;
69-
7081
rules = Object.keys(allRules).sort();
7182
for ( i = 0; i < rules.length; i++ ) {
7283
rule = rules[i];
@@ -87,6 +98,8 @@ var renderRules = function(details) {
8798
ulRight.append(liRight);
8899
}
89100

101+
uDom('#diff > .left > .rulesContainer').append(ulLeft);
102+
uDom('#diff > .right > .rulesContainer').append(ulRight);
90103
uDom('#diff').toggleClass('dirty', details.sessionRules !== details.permanentRules);
91104
};
92105

src/js/hnswitches.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ var HnSwitches = function() {
3737
/******************************************************************************/
3838

3939
var switchBitOffsets = {
40-
'noStrictBlocking': 0,
41-
'noPopups': 2,
42-
'noCosmeticFiltering': 4
40+
'no-strict-blocking': 0,
41+
'no-popups': 2,
42+
'no-cosmetic-filtering': 4
4343
};
4444

4545
var fromLegacySwitchNames = {
46-
'dontBlockDoc': 'noStrictBlocking',
47-
'doBlockAllPopups': 'noPopups'
46+
'dontBlockDoc': 'no-strict-blocking',
47+
'doBlockAllPopups': 'no-popups',
48+
'noStrictBlocking': 'no-strict-blocking',
49+
'noPopups': 'no-popups',
50+
'noCosmeticFiltering': 'no-cosmetic-filtering'
4851
};
4952

5053
var switchStateToNameMap = {
@@ -279,6 +282,8 @@ HnSwitches.prototype.fromString = function(text) {
279282
var fields;
280283
var switchName, hostname, state;
281284

285+
this.reset();
286+
282287
while ( lineBeg < textEnd ) {
283288
lineEnd = text.indexOf('\n', lineBeg);
284289
if ( lineEnd < 0 ) {

src/js/messaging.js

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ var getStats = function(tabId, tabTitle) {
234234
r.firewallRules = getFirewallRules(tabContext.rootHostname, r.hostnameDict);
235235
r.canElementPicker = tabContext.rootHostname.indexOf('.') !== -1;
236236
r.canRequestLog = canRequestLog;
237-
r.noPopups = µb.hnSwitches.evaluateZ('noPopups', tabContext.rootHostname);
238-
r.noStrictBlocking = µb.hnSwitches.evaluateZ('noStrictBlocking', tabContext.rootHostname);
239-
r.noCosmeticFiltering = µb.hnSwitches.evaluateZ('noCosmeticFiltering', tabContext.rootHostname);
237+
r.noPopups = µb.hnSwitches.evaluateZ('no-popups', tabContext.rootHostname);
238+
r.noStrictBlocking = µb.hnSwitches.evaluateZ('no-strict-blocking', tabContext.rootHostname);
239+
r.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname);
240240
} else {
241241
r.hostnameDict = {};
242242
r.firewallRules = getFirewallRules();
@@ -832,10 +832,44 @@ var µb = µBlock;
832832

833833
/******************************************************************************/
834834

835-
var getFirewallRules = function() {
835+
var getRules = function() {
836836
return {
837837
permanentRules: µb.permanentFirewall.toString(),
838-
sessionRules: µb.sessionFirewall.toString()
838+
sessionRules: µb.sessionFirewall.toString(),
839+
hnSwitches: µb.hnSwitches.toString()
840+
};
841+
};
842+
843+
// Untangle rules and switches.
844+
var untangle = function(s) {
845+
var textEnd = s.length;
846+
var lineBeg = 0, lineEnd;
847+
var line;
848+
var rules = [];
849+
var switches = [];
850+
851+
while ( lineBeg < textEnd ) {
852+
lineEnd = s.indexOf('\n', lineBeg);
853+
if ( lineEnd < 0 ) {
854+
lineEnd = s.indexOf('\r', lineBeg);
855+
if ( lineEnd < 0 ) {
856+
lineEnd = textEnd;
857+
}
858+
}
859+
line = s.slice(lineBeg, lineEnd).trim();
860+
lineBeg = lineEnd + 1;
861+
862+
// Switches always contain a ':'
863+
if ( line.indexOf(':') === -1 ) {
864+
rules.push(line);
865+
} else {
866+
switches.push(line);
867+
}
868+
}
869+
870+
return {
871+
rules: rules.join('\n'),
872+
switches: switches.join('\n')
839873
};
840874
};
841875

@@ -844,34 +878,40 @@ var getFirewallRules = function() {
844878
var onMessage = function(request, sender, callback) {
845879
// Async
846880
switch ( request.what ) {
847-
default:
848-
break;
881+
default:
882+
break;
849883
}
850884

851885
// Sync
886+
var r;
852887
var response;
853888

854889
switch ( request.what ) {
855-
case 'getFirewallRules':
856-
response = getFirewallRules();
857-
break;
858-
859-
case 'setSessionFirewallRules':
860-
// https://github.com/chrisaljoudi/uBlock/issues/772
861-
µb.cosmeticFilteringEngine.removeFromSelectorCache('*');
862-
863-
µb.sessionFirewall.fromString(request.rules);
864-
response = getFirewallRules();
865-
break;
866-
867-
case 'setPermanentFirewallRules':
868-
µb.permanentFirewall.fromString(request.rules);
869-
µb.savePermanentFirewallRules();
870-
response = getFirewallRules();
871-
break;
872-
873-
default:
874-
return vAPI.messaging.UNHANDLED;
890+
case 'getFirewallRules':
891+
response = getRules();
892+
break;
893+
894+
case 'setSessionFirewallRules':
895+
// https://github.com/chrisaljoudi/uBlock/issues/772
896+
µb.cosmeticFilteringEngine.removeFromSelectorCache('*');
897+
r = untangle(request.rules);
898+
µb.sessionFirewall.fromString(r.rules);
899+
µb.hnSwitches.fromString(r.switches);
900+
µb.saveHostnameSwitches();
901+
response = getRules();
902+
break;
903+
904+
case 'setPermanentFirewallRules':
905+
r = untangle(request.rules);
906+
µb.permanentFirewall.fromString(r.rules);
907+
µb.savePermanentFirewallRules();
908+
µb.hnSwitches.fromString(r.switches);
909+
µb.saveHostnameSwitches();
910+
response = getRules();
911+
break;
912+
913+
default:
914+
return vAPI.messaging.UNHANDLED;
875915
}
876916

877917
callback(response);

src/js/pagestore.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,7 @@ PageStore.prototype.init = function(tabId) {
497497
/******************************************************************************/
498498

499499
PageStore.prototype.reuse = function(context) {
500-
// We can't do this: when force refreshing a page, the page store data
501-
// needs to be reset
502-
//if ( pageURL === this.pageURL ) {
503-
// return this;
504-
//}
500+
// When force refreshing a page, the page store data needs to be reset.
505501

506502
// If the hostname changes, we can't merely just update the context.
507503
var tabContext = µb.tabContextManager.lookup(this.tabId);
@@ -635,7 +631,7 @@ PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
635631

636632
var tabContext = µb.tabContextManager.lookup(this.tabId);
637633

638-
if ( µb.hnSwitches.evaluateZ('noCosmeticFiltering', tabContext.rootHostname) ) {
634+
if ( µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) ) {
639635
return false;
640636
}
641637

src/js/popup.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ var hashFromPopupData = function(reset) {
145145
}
146146
hasher.sort();
147147
hasher.push(uDom('body').hasClass('off'));
148-
hasher.push(uDom('#noCosmeticFiltering').hasClass('on'));
148+
hasher.push(uDom('#no-cosmetic-filtering').hasClass('on'));
149149

150150
var hash = hasher.join('');
151151
if ( reset ) {
@@ -432,9 +432,9 @@ var renderPopup = function() {
432432
renderPrivacyExposure();
433433

434434
// Extra tools
435-
uDom('#noPopups').toggleClass('on', popupData.noPopups === true);
436-
uDom('#noStrictBlocking').toggleClass('on', popupData.noStrictBlocking === true);
437-
uDom('#noCosmeticFiltering').toggleClass('on', popupData.noCosmeticFiltering === true);
435+
uDom('#no-popups').toggleClass('on', popupData.noPopups === true);
436+
uDom('#no-strict-blocking').toggleClass('on', popupData.noStrictBlocking === true);
437+
uDom('#no-cosmetic-filtering').toggleClass('on', popupData.noCosmeticFiltering === true);
438438

439439
// https://github.com/chrisaljoudi/uBlock/issues/470
440440
// This must be done here, to be sure the popup is resized properly
@@ -463,7 +463,7 @@ var renderPopup = function() {
463463
var renderPopupLazy = function() {
464464
var onDataReady = function(data) {
465465
var v = data.hiddenElementCount || '';
466-
uDom('#noCosmeticFiltering > span.badge').text(
466+
uDom('#no-cosmetic-filtering > span.badge').text(
467467
typeof v === 'number' ? v.toLocaleString() : v
468468
);
469469
};

src/js/tab.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,10 @@ vAPI.tabs.onClosed = function(tabId) {
413413
vAPI.tabs.onPopup = function(details) {
414414
//console.debug('vAPI.tabs.onPopup: details = %o', details);
415415

416-
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
416+
var tabContext = µb.tabContextManager.lookup(details.openerTabId);
417417
var openerURL = details.openerURL || '';
418-
419-
if ( openerURL === '' && pageStore ) {
420-
openerURL = pageStore.pageURL;
418+
if ( openerURL === '' && tabContext.tabId === details.openerTabId ) {
419+
openerURL = tabContext.normalURL;
421420
}
422421

423422
if ( openerURL === '' ) {
@@ -452,8 +451,8 @@ vAPI.tabs.onPopup = function(details) {
452451
var result = '';
453452

454453
// Check user switch first
455-
if ( µb.hnSwitches.evaluateZ('noPopups', openerHostname) ) {
456-
result = 'ub:noPopups true';
454+
if ( µb.hnSwitches.evaluateZ('no-popups', openerHostname) ) {
455+
result = 'ub:no-popups true';
457456
}
458457

459458
// https://github.com/chrisaljoudi/uBlock/issues/323
@@ -468,6 +467,7 @@ vAPI.tabs.onPopup = function(details) {
468467
}
469468

470469
// https://github.com/chrisaljoudi/uBlock/issues/91
470+
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
471471
if ( pageStore ) {
472472
pageStore.logRequest(context, result);
473473
}
@@ -545,18 +545,6 @@ vAPI.tabs.registerListeners();
545545

546546
/******************************************************************************/
547547

548-
µb.pageUrlFromTabId = function(tabId) {
549-
var pageStore = this.pageStores[tabId];
550-
return pageStore ? pageStore.pageURL : '';
551-
};
552-
553-
µb.pageUrlFromPageStats = function(pageStats) {
554-
if ( pageStats ) {
555-
return pageStats.pageURL;
556-
}
557-
return '';
558-
};
559-
560548
µb.pageStoreFromTabId = function(tabId) {
561549
return this.pageStores[tabId];
562550
};
@@ -583,7 +571,7 @@ var pageStoreJanitor = function() {
583571
var checkTab = function(tabId) {
584572
vapiTabs.get(tabId, function(tab) {
585573
if ( !tab ) {
586-
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µb.pageUrlFromTabId(tabId));
574+
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µtabId);
587575
µb.unbindTabFromPageStats(tabId);
588576
}
589577
});

src/js/traffic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ var onBeforeRootFrameRequest = function(details) {
154154
var result = '';
155155

156156
// Permanently unrestricted?
157-
if ( result === '' && µb.hnSwitches.evaluateZ('noStrictBlocking', requestHostname) ) {
158-
result = 'ua:noStrictBlocking true';
157+
if ( result === '' && µb.hnSwitches.evaluateZ('no-strict-blocking', requestHostname) ) {
158+
result = 'ua:no-strict-blocking true';
159159
}
160160

161161
// Temporarily whitelisted?

src/js/ublock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ var matchWhitelistDirective = function(url, hostname, directive) {
324324
}
325325

326326
// Take action if needed
327-
if ( details.name === 'noCosmeticFiltering' ) {
327+
if ( details.name === 'no-cosmetic-filtering' ) {
328328
vAPI.tabs.injectScript(details.tabId, {
329329
file: 'js/cosmetic-' + (details.state ? 'off' : 'on') + '.js',
330330
allFrames: true

0 commit comments

Comments
 (0)