Skip to content

Commit e9d1994

Browse files
committed
fixed #27 and #28
1 parent 6d624b9 commit e9d1994

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

scripts/background.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ browser.webRequest.onBeforeRequest.addListener(function(e) {
100100
} else {
101101
if (item.isFunction) {
102102
runTryCatch(() => {
103-
let r = item.func_body(redirectTo, detail);
103+
let r = item._func(redirectTo, detail);
104104
if (typeof(r) === 'string') {
105105
redirectTo = r;
106106
}
107107
});
108108
} else {
109109
if (item.matchType === 'regexp') {
110-
redirectTo = redirectTo.replace(new RegExp(item.pattern), item.to);
110+
redirectTo = redirectTo.replace(item._reg, item.to);
111111
} else {
112112
redirectTo = item.to;
113113
}
@@ -156,7 +156,7 @@ function modifyHeaders(headers, rules, details) {
156156
for (let item of rules) {
157157
if (item.isFunction) {
158158
runTryCatch(() => {
159-
item.func_body(headers, detail);
159+
item._func(headers, detail);
160160
});
161161
}
162162
}

scripts/manage.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,12 @@ function onRealtimeTest() {
588588
isMatch = 1;
589589
break;
590590
case 'regexp':
591-
var r = runTryCatch(function() {
592-
var reg = new RegExp(matchRule);
593-
return reg.test(url);
594-
});
595-
isMatch = (r === undefined ? -1 : (r ? 1 : 0));
591+
try {
592+
let reg = new RegExp(matchRule);
593+
isMatch = reg.test(url) ? 1 : 0;
594+
} catch (e) {
595+
isMatch = -1;
596+
}
596597
break;
597598
case 'prefix':
598599
isMatch = url.indexOf(matchRule) === 0 ? 1 : 0;
@@ -607,11 +608,12 @@ function onRealtimeTest() {
607608
break;
608609
}
609610
if (isMatch === 1 && typeof(excludeRule) === 'string' && excludeRule.length > 0) {
610-
var r = runTryCatch(function() {
611-
var reg = new RegExp(excludeRule);
612-
return reg.test(url);
613-
});
614-
isMatch = (typeof(r) === 'undefined' || r) ? 1 : (r ? 2 : 1);
611+
try {
612+
let reg = new RegExp(matchRule);
613+
isMatch = reg.test(url) ? 2 : 1;
614+
} catch (e) {
615+
isMatch = 1;
616+
}
615617
}
616618
if (isMatch === -1) {
617619
resultArea.innerHTML = t('test_invalid_regexp');

scripts/storage.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,35 @@ function updateCache(type) {
4545
os.openCursor().onsuccess = function(event) {
4646
var cursor = event.target.result;
4747
if (cursor) {
48-
var s = cursor.value;
48+
let s = cursor.value;
49+
let isValidRule = true;
4950
s.id = cursor.key;
5051
// Init function here
5152
if (s.isFunction) {
52-
s.func_body = new Function('val', 'detail', s.code);
53+
try {
54+
s._func = new Function('val', 'detail', s.code);
55+
} catch (e) {
56+
isValidRule = false;
57+
}
58+
}
59+
// Init regexp
60+
if (s.matchType === 'regexp') {
61+
try {
62+
s._reg = new RegExp(s.pattern);
63+
} catch (e) {
64+
isValidRule = false;
65+
}
66+
}
67+
if (typeof(s.exclude) === 'string' && s.exclude.length > 0) {
68+
try {
69+
s._exclude = new RegExp(s.exclude);
70+
} catch (e) {
71+
isValidRule = false;
72+
}
73+
}
74+
if (isValidRule) {
75+
all.push(s);
5376
}
54-
all.push(s);
5577
cursor.continue();
5678
} else {
5779
cachedRules[type] = all;
@@ -93,11 +115,7 @@ function filterRules(rules, options) {
93115
result = true;
94116
break;
95117
case 'regexp':
96-
var r = runTryCatch(() => {
97-
var reg = new RegExp(rule.pattern);
98-
return reg.test(url);
99-
});
100-
result = (r === undefined ? false : r);
118+
result = rule._reg.test(url);
101119
break;
102120
case 'prefix':
103121
result = url.indexOf(rule.pattern) === 0;
@@ -111,12 +129,8 @@ function filterRules(rules, options) {
111129
default:
112130
break;
113131
}
114-
if (result && typeof(rule.exclude) === 'string' && rule.exclude.length > 0) {
115-
var r = runTryCatch(function() {
116-
var reg = new RegExp(rule.exclude);
117-
return reg.test(url);
118-
});
119-
return (typeof(r) === 'undefined' || r) ? false : true;
132+
if (result && rule._exclude) {
133+
return !(rule._exclude.test(url));
120134
} else {
121135
return result;
122136
}

0 commit comments

Comments
 (0)