1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
const kRedirectorWildcard = 'W';
const kRedirectorRegex= 'R';
var Redirector = {
list : [],
enabled : true,
init : function() {
this.load();
this.prefObserver.register();
},
unload : function() {
this.prefObserver.unregister();
},
save : function() {
var r
, tempList = [];
for each (r in this.list) {
tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.onlyIfLinkExists, r.patternType, r.excludePattern]);
}
RedirLib.setCharPref('redirects', tempList.toSource());
},
load : function() {
var tempList = eval(RedirLib.getCharPref('redirects'));
var arr;
this.list = [];
for each (arr in tempList) {
this.list.push({
exampleUrl : arr[0],
pattern : arr[1],
redirectUrl : arr[2],
onlyIfLinkExists : arr[3],
patternType : arr[4],
excludePattern : arr[5] || ''
});
}
},
addRedirect : function(redirect) {
this.list.push(redirect);
this.save();
},
deleteAt : function(index) {
this.list.splice(index, 1);
this.save();
},
getRedirectUrlForInstantRedirect : function(url) {
var redirect, link, links, redirectUrl;
if (this.enabled) {
for each (redirect in this.list) {
redirectUrl = this.getRedirectUrl(url, redirect);
//Can't do fast redirect if it requires that link exists
//we need the original page to verify that it exists.
//Slow redirect will be done automatically.
if (redirectUrl) {
if (redirect.redirectUrl.startsWith('xpath:')) {
RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getString('xpathDeprecated'));
} else if (!redirect.onlyIfLinkExists) {
RedirLib.debug('%1 matches %2, and it\'s not only if link exists. Can do instant redirect.'._(redirect.pattern, url));
return { 'url' : redirectUrl, 'pattern' : redirect.pattern};
} else {
RedirLib.debug('%1 matches %2, but it\'s "only if link exists" and so has to be a slow redirect'._(redirect.pattern, url));
}
}
}
}
return { 'url' : null, 'pattern' : null};
},
getRedirectUrl: function(url, redirect) {
if (redirect.patternType == kRedirectorWildcard) {
if (this.wildcardMatch(redirect.excludePattern, url, 'whatever')) {
RedirLib.debug('%1 matches exclude pattern %2'._(url, redirect.excludePattern));
return null;
}
return this.wildcardMatch(redirect.pattern, url, redirect.redirectUrl);
} else if (redirect.patternType == kRedirectorRegex) {
if (this.regexMatch(redirect.excludePattern, url, 'whatever')) {
RedirLib.debug('%1 matches exclude pattern %2'._(url, redirect.excludePattern));
return null;
}
return this.regexMatch(redirect.pattern, url, redirect.redirectUrl);
}
return null;
},
processUrl : function(url, doc) {
var redirect, link, links, redirectUrl;
if (!this.enabled) {
return;
}
for each (redirect in this.list) {
redirectUrl = this.getRedirectUrl(url, redirect);
if (redirectUrl) {
RedirLib.debug('%1 matches %2'._(redirect.pattern, url));
if (redirect.onlyIfLinkExists) {
links = window.content.document.getElementsByTagName('a');
for each(link in links) {
if (link.href && link.href.toString() == redirectUrl) {
RedirLib.debug('Found a link for %1'._(redirectUrl));
this.goto(redirectUrl, redirect.pattern, url, doc);
return;
}
}
RedirLib.debug('Did not find a link for %1'._(redirectUrl));
} else {
this.goto(redirectUrl, redirect.pattern, url, doc);
}
}
}
},
makeAbsoluteUrl : function(currentUrl, relativeUrl) {
if (relativeUrl.startsWith('http://') || relativeUrl.startsWith('https://')) {
return relativeUrl;
}
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
RedirLib.debug(currentUrl);
var uri = ioService.newURI(currentUrl, null, null);
return uri.resolve(relativeUrl);
},
goto : function(redirectUrl, pattern, url, doc) {
redirectUrl = this.makeAbsoluteUrl(url, redirectUrl);
if (redirectUrl.startsWith('xpath:')) {
//Do nothing, the instant redirect will have popped up a message
} else if (redirectUrl == url) {
RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [pattern, redirectUrl]));
} else {
doc.location.href = redirectUrl;
}
},
regexMatch : function(pattern, text, redirectUrl) {
if (!pattern) {
return null;
}
var strings, rx, match;
try {
rx = new RegExp(pattern, 'gi');
match = rx.exec(text);
} catch(e) {
//HACK, need to make this better
if (window.RedirectorOverlay) {
strings = window.RedirectorOverlay.strings;
} else if(window.Redirect) {
strings = window.Redirect.strings;
}
RedirLib.msgBox(strings.getString('extensionName'), strings.getFormattedString('regexPatternError', [pattern, e.toString()]));
return null;
}
var rxrepl;
if (match) {
for (var i = 1; i < match.length; i++) {
rxrepl = new RegExp('\\$' + i, 'gi');
redirectUrl = redirectUrl.replace(rxrepl, match[i]);
}
return redirectUrl;
}
return null;
},
wildcardMatch : function(pattern, text, redirectUrl) {
var parts
, part
, i
, pos
, originalText
, stars;
if (!pattern) {
return null;
}
parts = pattern.split('*');
stars = [];
originalText = text;
var starStart = -1;
for (i in parts) {
part = parts[i];
pos = text.indexOf(part);
if (pos == -1) {
return null;
}
if (i == 0 && pos != 0) {
return null;
}
if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) {
return null;
}
if (i == 0) {
//Do nothing, part will be added on next run
} else if (i == parts.length-1 && parts[i] == '') {
stars.push(text);
} else {
stars.push(text.substr(0, pos));
}
text = text.substr(pos + part.length);
}
for (var i = 1; i <= stars.length; i++) {
redirectUrl = redirectUrl.replace(new RegExp('\\$' + i, 'gi'), stars[i-1]);
}
return redirectUrl;
},
openHelp : function() {
var windowName = "redirectorHelp";
var windowsMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var win;
var iter = windowsMediator.getEnumerator(null);
while (iter.hasMoreElements()) {
win = iter.getNext();
if (win.name == windowName) {
win.focus();
return;
}
}
window.openDialog("chrome://redirector/content/help.html", windowName, "chrome,dialog,resizable=yes,location=0,toolbar=0,status=0,width=800px,height=600px,centerscreen", this);
},
openSettings : function() {
var windowName = "redirectorSettings";
var windowsMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var win = windowsMediator.getMostRecentWindow(windowName);
if (win) {
win.focus();
} else {
window.openDialog("chrome://redirector/content/redirectList.xul",
windowName,
"chrome,dialog,resizable=no,centerscreen", this);
}
},
prefObserver : {
getService : function() {
return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranchInternal);
},
register: function() {
this.getService().addObserver('extensions.redirector', this, false);
},
unregister: function() {
this.getService().removeObserver('extensions.redirector', this);
},
observe : function(subject, topic, data) {
if (topic != 'nsPref:changed') {
return;
}
if (!window.Redirector) {
return;
}
if (data == 'extensions.redirector.redirects') {
Redirector.load();
} else if (data == 'extensions.redirector.enabled') {
Redirector.enabled = RedirLib.getBoolPref('enabled');
}
}
}
};
|