Index: lib/filterClasses.js |
=================================================================== |
--- a/lib/filterClasses.js |
+++ b/lib/filterClasses.js |
@@ -766,16 +766,17 @@ |
let contentType = null; |
let matchCase = null; |
let domains = null; |
let sitekeys = null; |
let thirdParty = null; |
let collapse = null; |
let csp = null; |
+ let rewrite = null; |
let options; |
let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
if (match) |
{ |
options = match[1].split(","); |
text = match.input.substr(0, match.index); |
for (let option of options) |
{ |
@@ -813,30 +814,32 @@ |
else if (option == "~THIRD_PARTY") |
thirdParty = false; |
else if (option == "COLLAPSE") |
collapse = true; |
else if (option == "~COLLAPSE") |
collapse = false; |
else if (option == "SITEKEY" && value) |
sitekeys = value.toUpperCase(); |
+ else if (option == "REWRITE" && value) |
+ rewrite = value; |
else |
return new InvalidFilter(origText, "filter_unknown_option"); |
} |
} |
try |
{ |
if (blocking) |
{ |
if (csp && Filter.invalidCSPRegExp.test(csp)) |
return new InvalidFilter(origText, "filter_invalid_csp"); |
return new BlockingFilter(origText, text, contentType, matchCase, domains, |
- thirdParty, sitekeys, collapse, csp); |
+ thirdParty, sitekeys, collapse, csp, rewrite); |
} |
return new WhitelistFilter(origText, text, contentType, matchCase, domains, |
thirdParty, sitekeys); |
} |
catch (e) |
{ |
return new InvalidFilter(origText, "filter_invalid_regexp"); |
} |
@@ -889,27 +892,30 @@ |
* @param {boolean} matchCase see RegExpFilter() |
* @param {string} domains see RegExpFilter() |
* @param {boolean} thirdParty see RegExpFilter() |
* @param {string} sitekeys see RegExpFilter() |
* @param {boolean} collapse |
* defines whether the filter should collapse blocked content, can be null |
* @param {string} [csp] |
* Content Security Policy to inject when the filter matches |
+ * @param {string} [rewrite] |
+ * The rewrite expression |
* @constructor |
* @augments RegExpFilter |
*/ |
function BlockingFilter(text, regexpSource, contentType, matchCase, domains, |
- thirdParty, sitekeys, collapse, csp) |
+ thirdParty, sitekeys, collapse, csp, rewrite) |
{ |
RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
thirdParty, sitekeys); |
this.collapse = collapse; |
this.csp = csp; |
+ this.rewrite = rewrite; |
} |
exports.BlockingFilter = BlockingFilter; |
BlockingFilter.prototype = extend(RegExpFilter, { |
type: "blocking", |
/** |
* Defines whether the filter should collapse blocked content. |
@@ -917,17 +923,37 @@ |
* @type {boolean} |
*/ |
collapse: null, |
/** |
* Content Security Policy to inject for matching requests. |
* @type {string} |
*/ |
- csp: null |
+ csp: null, |
+ |
+ /** |
+ * The rewrite expression |
+ * @type {string} |
+ */ |
+ rewrite: null, |
+ |
+ /** |
+ * Perform the URL rewrite |
+ * @param {string} urlString the string URL to rewrite |
+ * @returns {string?} the rewritten URL, or null if it doesn't match. |
+ */ |
+ doRewrite(urlString) |
Manish Jethani
2018/05/02 17:52:23
For what it's worth I would rather call this rewri
hub
2018/05/02 22:14:32
Done.
|
+ { |
+ let matches = this.regexp.exec(urlString); |
+ if (matches) |
+ return this.rewrite.replace("$1", matches[0]); |
hub
2018/04/27 00:48:22
This is simplistic though. It does work, but the i
Manish Jethani
2018/04/30 20:07:11
What's wrong with the following?
doRewrite(urlS
hub
2018/05/01 18:32:15
I thought about this initially but then couldn't m
Manish Jethani
2018/05/02 15:31:09
Agreed.
hub
2018/05/02 22:14:32
Done.
|
+ |
+ return null; |
+ } |
}); |
/** |
* Class for whitelist filters |
* @param {string} text see Filter() |
* @param {string} regexpSource see RegExpFilter() |
* @param {number} contentType see RegExpFilter() |
* @param {boolean} matchCase see RegExpFilter() |