diff options
author | gorhill <rhill@raymondhill.net> | 2015-03-05 12:52:12 -0500 |
---|---|---|
committer | gorhill <rhill@raymondhill.net> | 2015-03-05 12:52:12 -0500 |
commit | e23c2977d749f88bbff1fa1024c8f3339ae97949 (patch) | |
tree | 547ccde36f9e081747ca42a24e989699243b4535 | |
parent | b01dfcbf6247e094418875c17abcbc45d48a6cc6 (diff) | |
download | uBlock-e23c2977d749f88bbff1fa1024c8f3339ae97949.zip uBlock-e23c2977d749f88bbff1fa1024c8f3339ae97949.tar.gz uBlock-e23c2977d749f88bbff1fa1024c8f3339ae97949.tar.bz2 |
this fixes #945
-rw-r--r-- | src/js/element-picker.js | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/src/js/element-picker.js b/src/js/element-picker.js index b3be0ff..79c85f3 100644 --- a/src/js/element-picker.js +++ b/src/js/element-picker.js @@ -423,16 +423,47 @@ var elementsFromFilter = function(filter) { // Net filters: we need to lookup manually -- translating into a // foolproof CSS selector is just not possible - if ( filter.slice(0, 2) === '||' ) { - filter = filter.replace('||', ''); + + // https://github.com/gorhill/uBlock/issues/945 + // Transform into a regular expression, this allows the user to edit and + // insert wildcard(s) into the proposed filter + var reStr = ''; + if ( filter.length > 1 && filter.charAt(0) === '/' && filter.slice(-1) === '/' ) { + reStr = filter.slice(1, -1); + } + else { + var rePrefix = '', reSuffix = ''; + if ( filter.slice(0, 2) === '||' ) { + filter = filter.replace('||', ''); + } else { + if ( filter.charAt(0) === '|' ) { + rePrefix = '^'; + filter = filter.slice(1); + } + } + if ( filter.slice(-1) === '|' ) { + reSuffix = '$'; + filter = filter.slice(0, -1); + } + reStr = rePrefix + + filter.replace(/[.+?${}()|[\]\\]/g, '\\$&').replace(/[\*^]+/g, '.*') + + reSuffix; + } + var reFilter = null; + try { + reFilter = new RegExp(reStr); + } catch (e) { + return out; } - var elems = document.querySelectorAll('iframe, img, object, embed'); + + var props = netFilterSources; + var elems = document.querySelectorAll(Object.keys(props).join()); var i = elems.length; var elem, src; while ( i-- ) { elem = elems[i]; - src = elem[netFilterSources[elem.tagName.toLowerCase()]]; - if ( src && src.indexOf(filter) !== -1 ) { + src = elem[props[elem.tagName.toLowerCase()]]; + if ( src && reFilter.test(src) ) { out.push(elem); } } |