Skip to content

Commit

Permalink
fixed #27 and #28
Browse files Browse the repository at this point in the history
  • Loading branch information
sylingd committed Oct 1, 2017
1 parent 6d624b9 commit e9d1994
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
6 changes: 3 additions & 3 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ browser.webRequest.onBeforeRequest.addListener(function(e) {
} else {
if (item.isFunction) {
runTryCatch(() => {
let r = item.func_body(redirectTo, detail);
let r = item._func(redirectTo, detail);
if (typeof(r) === 'string') {
redirectTo = r;
}
});
} else {
if (item.matchType === 'regexp') {
redirectTo = redirectTo.replace(new RegExp(item.pattern), item.to);
redirectTo = redirectTo.replace(item._reg, item.to);
} else {
redirectTo = item.to;
}
Expand Down Expand Up @@ -156,7 +156,7 @@ function modifyHeaders(headers, rules, details) {
for (let item of rules) {
if (item.isFunction) {
runTryCatch(() => {
item.func_body(headers, detail);
item._func(headers, detail);
});
}
}
Expand Down
22 changes: 12 additions & 10 deletions scripts/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,12 @@ function onRealtimeTest() {
isMatch = 1;
break;
case 'regexp':
var r = runTryCatch(function() {
var reg = new RegExp(matchRule);
return reg.test(url);
});
isMatch = (r === undefined ? -1 : (r ? 1 : 0));
try {
let reg = new RegExp(matchRule);
isMatch = reg.test(url) ? 1 : 0;
} catch (e) {
isMatch = -1;
}
break;
case 'prefix':
isMatch = url.indexOf(matchRule) === 0 ? 1 : 0;
Expand All @@ -607,11 +608,12 @@ function onRealtimeTest() {
break;
}
if (isMatch === 1 && typeof(excludeRule) === 'string' && excludeRule.length > 0) {
var r = runTryCatch(function() {
var reg = new RegExp(excludeRule);
return reg.test(url);
});
isMatch = (typeof(r) === 'undefined' || r) ? 1 : (r ? 2 : 1);
try {
let reg = new RegExp(matchRule);
isMatch = reg.test(url) ? 2 : 1;
} catch (e) {
isMatch = 1;
}
}
if (isMatch === -1) {
resultArea.innerHTML = t('test_invalid_regexp');
Expand Down
42 changes: 28 additions & 14 deletions scripts/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,35 @@ function updateCache(type) {
os.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
var s = cursor.value;
let s = cursor.value;
let isValidRule = true;
s.id = cursor.key;
// Init function here
if (s.isFunction) {
s.func_body = new Function('val', 'detail', s.code);
try {
s._func = new Function('val', 'detail', s.code);
} catch (e) {
isValidRule = false;
}
}
// Init regexp
if (s.matchType === 'regexp') {
try {
s._reg = new RegExp(s.pattern);
} catch (e) {
isValidRule = false;
}
}
if (typeof(s.exclude) === 'string' && s.exclude.length > 0) {
try {
s._exclude = new RegExp(s.exclude);
} catch (e) {
isValidRule = false;
}
}
if (isValidRule) {
all.push(s);
}
all.push(s);
cursor.continue();
} else {
cachedRules[type] = all;
Expand Down Expand Up @@ -93,11 +115,7 @@ function filterRules(rules, options) {
result = true;
break;
case 'regexp':
var r = runTryCatch(() => {
var reg = new RegExp(rule.pattern);
return reg.test(url);
});
result = (r === undefined ? false : r);
result = rule._reg.test(url);
break;
case 'prefix':
result = url.indexOf(rule.pattern) === 0;
Expand All @@ -111,12 +129,8 @@ function filterRules(rules, options) {
default:
break;
}
if (result && typeof(rule.exclude) === 'string' && rule.exclude.length > 0) {
var r = runTryCatch(function() {
var reg = new RegExp(rule.exclude);
return reg.test(url);
});
return (typeof(r) === 'undefined' || r) ? false : true;
if (result && rule._exclude) {
return !(rule._exclude.test(url));
} else {
return result;
}
Expand Down

0 comments on commit e9d1994

Please sign in to comment.