aboutsummaryrefslogtreecommitdiffstats
path: root/src/js
diff options
context:
space:
mode:
authorgorhill <rhill@raymondhill.net>2014-12-14 17:21:59 -0500
committergorhill <rhill@raymondhill.net>2014-12-14 17:21:59 -0500
commitb12bc6a7cf7c975c1aec52d3d54901a9de500899 (patch)
treead11353e0c4d03b51fceb9e8c881286a5f50b48b /src/js
parentd5a3226066c7f641148f25640a631cf4d0e7cc3d (diff)
downloaduBlock-b12bc6a7cf7c975c1aec52d3d54901a9de500899.zip
uBlock-b12bc6a7cf7c975c1aec52d3d54901a9de500899.tar.gz
uBlock-b12bc6a7cf7c975c1aec52d3d54901a9de500899.tar.bz2
code review for #405
Diffstat (limited to 'src/js')
-rw-r--r--src/js/pagestore.js2
-rw-r--r--src/js/ublock.js97
2 files changed, 47 insertions, 52 deletions
diff --git a/src/js/pagestore.js b/src/js/pagestore.js
index 3fb2d7f..5fcf8af 100644
--- a/src/js/pagestore.js
+++ b/src/js/pagestore.js
@@ -405,7 +405,7 @@ PageStore.prototype.getFrame = function(frameId) {
PageStore.prototype.getNetFilteringSwitch = function() {
if ( this.netFilteringReadTime < µb.netWhitelistModifyTime ) {
- this.netFiltering = µb.getNetFilteringSwitch(this.pageURL, this.pageDomain);
+ this.netFiltering = µb.getNetFilteringSwitch(this.pageURL);
this.netFilteringReadTime = Date.now();
}
return this.netFiltering;
diff --git a/src/js/ublock.js b/src/js/ublock.js
index eab3ca9..3e48a7f 100644
--- a/src/js/ublock.js
+++ b/src/js/ublock.js
@@ -29,31 +29,9 @@
/******************************************************************************/
-var µb = µBlock;
-
-/******************************************************************************/
-
// https://github.com/gorhill/uBlock/issues/405
// Be more flexible with whitelist syntax
-var matchWhitelistException = function(url, exception) {
- // Exception is a plain hostname
- if ( exception.indexOf('/') === -1 ) {
- return µb.URI.hostnameFromURI(url).slice(-exception.length) === exception;
- }
- // Match URL exactly
- if ( exception.indexOf('*') === -1 ) {
- return url === exception;
- }
- // Regex escape code inspired from:
- // "Is there a RegExp.escape function in Javascript?"
- // http://stackoverflow.com/a/3561711
- var reStr = exception.replace(whitelistDirectiveEscape, '\\$&')
- .replace(whitelistDirectiveEscapeAsterisk, '.*');
- var re = new RegExp(reStr);
- return re.test(url);
-};
-
// Any special regexp char will be escaped
var whitelistDirectiveEscape = /[-\/\\^$+?.()|[\]{}]/g;
@@ -62,36 +40,53 @@ var whitelistDirectiveEscapeAsterisk = /\*/g;
// Probably manually entered whitelist directive
var isHandcraftedWhitelistDirective = function(directive) {
+ return directive.indexOf('/') !== -1 &&
+ directive.indexOf('*') !== -1;
+};
+
+var matchWhitelistDirective = function(url, hostname, directive) {
+ // Directive is a plain hostname
if ( directive.indexOf('/') === -1 ) {
- return false;
+ return hostname.slice(-directive.length) === directive;
+ }
+ // Match URL exactly
+ if ( directive.indexOf('*') === -1 ) {
+ return url === directive;
}
- return directive.indexOf('*') !== -1 || directive.slice(0, 4) !== 'http';
+ // Regex escape code inspired from:
+ // "Is there a RegExp.escape function in Javascript?"
+ // http://stackoverflow.com/a/3561711
+ var reStr = directive.replace(whitelistDirectiveEscape, '\\$&')
+ .replace(whitelistDirectiveEscapeAsterisk, '.*');
+ var re = new RegExp(reStr);
+ return re.test(url);
};
/******************************************************************************/
µBlock.getNetFilteringSwitch = function(url) {
+ var netWhitelist = this.netWhitelist;
var buckets, i;
- var hostname = this.URI.hostnameFromURI(url);
var pos = url.indexOf('#');
- url = pos !== -1 ? url.slice(0, pos) : url;
- var netWhitelist = this.netWhitelist;
+ var targetURL = pos !== -1 ? url.slice(0, pos) : url;
+ var targetHostname = this.URI.hostnameFromURI(targetURL);
+ var key = targetHostname;
for (;;) {
- if ( netWhitelist.hasOwnProperty(hostname) ) {
- buckets = netWhitelist[hostname];
+ if ( netWhitelist.hasOwnProperty(key) ) {
+ buckets = netWhitelist[key];
i = buckets.length;
while ( i-- ) {
- if ( matchWhitelistException(url, buckets[i]) ) {
- // console.log('"%s" matche url "%s"', buckets[i], keyURL);
+ if ( matchWhitelistDirective(targetURL, targetHostname, buckets[i]) ) {
+ // console.log('"%s" matche url "%s"', buckets[i], targetURL);
return false;
}
}
}
- pos = hostname.indexOf('.');
+ pos = key.indexOf('.');
if ( pos === -1 ) {
break;
}
- hostname = hostname.slice(pos + 1);
+ key = key.slice(pos + 1);
}
return true;
};
@@ -107,33 +102,33 @@ var isHandcraftedWhitelistDirective = function(directive) {
return currentState;
}
- var hostname = this.URI.hostnameFromURI(url);
- var pos = url.indexOf('#');
- url = pos !== -1 ? url.slice(0, pos) : url;
-
- var directive = scope === 'page' ? url : hostname;
var netWhitelist = this.netWhitelist;
+ var pos = url.indexOf('#');
+ var targetURL = pos !== -1 ? url.slice(0, pos) : url;
+ var targetHostname = this.URI.hostnameFromURI(targetURL);
+ var key = targetHostname;
+ var directive = scope === 'page' ? targetURL : targetHostname;
var buckets;
- // Add to exception list
+ // Add to directive list
if ( newState === false ) {
- if ( netWhitelist.hasOwnProperty(hostname) === false ) {
- buckets = netWhitelist[hostname] = [];
+ if ( netWhitelist.hasOwnProperty(key) === false ) {
+ buckets = netWhitelist[key] = [];
}
buckets.push(directive);
this.saveWhitelist();
return true;
}
- // Remove from exception list whatever causes current URL to be whitelisted
+ // Remove from directive list whatever causes current URL to be whitelisted
var i;
for (;;) {
- if ( netWhitelist.hasOwnProperty(hostname) ) {
- buckets = netWhitelist[hostname];
+ if ( netWhitelist.hasOwnProperty(key) ) {
+ buckets = netWhitelist[key];
i = buckets.length;
while ( i-- ) {
directive = buckets[i];
- if ( !matchWhitelistException(url, directive) ) {
+ if ( !matchWhitelistDirective(targetURL, targetHostname, directive) ) {
continue;
}
buckets.splice(i, 1);
@@ -145,14 +140,14 @@ var isHandcraftedWhitelistDirective = function(directive) {
}
}
if ( buckets.length === 0 ) {
- delete netWhitelist[hostname];
+ delete netWhitelist[key];
}
}
- pos = hostname.indexOf('.');
+ pos = key.indexOf('.');
if ( pos === -1 ) {
break;
}
- hostname = hostname.slice(pos + 1);
+ key = key.slice(pos + 1);
}
this.saveWhitelist();
return true;
@@ -160,10 +155,10 @@ var isHandcraftedWhitelistDirective = function(directive) {
/******************************************************************************/
-// For now we will use the net exception list
+// For now we will use the net whitelist
-µBlock.getCosmeticFilteringSwitch = function(url, domain) {
- return this.getNetFilteringSwitch(url, domain);
+µBlock.getCosmeticFilteringSwitch = function(url) {
+ return this.getNetFilteringSwitch(url);
};
/******************************************************************************/