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
|
// $Id$
var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
const Cc = Components.classes;
const Ci = Components.interfaces;
var Settings = {
lstRedirects: null,
btnDelete : null,
btnEdit : null,
btnUp : null,
btnDown : null,
chkEnableRedirector : null,
chkShowStatusBarIcon : null,
chkShowContextMenu : null,
chkEnableDebugOutput : null,
onLoad : function() {
try {
//Get references to controls
this.lstRedirects = document.getElementById('lstRedirects');
this.btnDelete = document.getElementById('btnDelete');
this.btnEdit = document.getElementById('btnEdit');
this.btnUp = document.getElementById('btnUp');
this.btnDown = document.getElementById('btnDown');
this.chkEnableRedirector = document.getElementById('chkEnableRedirector');
this.chkShowStatusBarIcon = document.getElementById('chkShowStatusBarIcon');
this.chkShowContextMenu = document.getElementById('chkShowContextMenu');
this.chkEnableDebugOutput = document.getElementById('chkEnableDebugOutput');
//Preferences
this.setPrefs(Redirector.prefs);
Redirector.prefs.addListener(this);
//Redirect list
this.lstRedirects.selType = 'single';
this.template = document.getElementsByTagName('richlistitem')[0];
this.lstRedirects.removeChild(this.template);
this.addItemsToListBox(Redirector.list);
this.strings = document.getElementById('redirector-strings');
} catch(e) {
alert(e);
}
},
onUnload : function() {
Redirector.prefs.removeListener(this);
},
changedPrefs : function(prefs) {
this.setPrefs(prefs);
},
setPrefs : function(prefs) {
this.chkEnableRedirector.setAttribute('checked', prefs.enabled);
this.chkShowStatusBarIcon.setAttribute('checked', prefs.showStatusBarIcon);
this.chkShowContextMenu.setAttribute('checked', prefs.showContextMenu);
this.chkEnableDebugOutput.setAttribute('checked', prefs.debugEnabled);
},
addItemsToListBox : function(items) {
var item, row, value, newItem;
for each (item in items) {
newItem = this.template.cloneNode(true);
newItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.includePattern);
newItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
newItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
var checkEnabled = newItem.getElementsByAttribute('name', 'chkEnabled')[0];
checkEnabled.setAttribute('checked', !item.disabled);
newItem.setAttribute('class', item.disabled ? 'disabledRedirect' : '');
newItem.item = item;
this.lstRedirects.appendChild(newItem);
newItem.setAttribute('selected', false)
}
//Enable, disable functionality
this.lstRedirects.addEventListener('click', function(ev) {
if (ev.originalTarget && ev.originalTarget.tagName == 'checkbox') {
var parent = ev.originalTarget.parentNode;
while (!parent.item) {
parent = parent.parentNode;
}
parent.item.disabled = !ev.originalTarget.hasAttribute('checked');
parent.setAttribute('class', parent.item.disabled ? 'disabledRedirect' : '');
Redirector.save();
}
},false);
},
moveUp : function(){
if (this.lstRedirects.selectedIndex <= 0) {
return;
}
this.switchItems(this.lstRedirects.selectedIndex-1);
},
moveDown : function() {
if (this.lstRedirects.selectedIndex == Redirector.list.length-1) {
return;
}
this.switchItems(this.lstRedirects.selectedIndex);
},
switchItems : function(firstIndex) {
var firstRedirect = Redirector.list[firstIndex];
var secondRedirect = Redirector.list[firstIndex+1];
Redirector.list[firstIndex] = secondRedirect;
Redirector.list[firstIndex+1] = firstRedirect;
var firstItem = this.lstRedirects.children[firstIndex];
var secondItem = this.lstRedirects.children[firstIndex+1];
this.lstRedirects.removeChild(secondItem);
this.lstRedirects.insertBefore(secondItem, firstItem);
Redirector.save();
this.selectionChange();
},
setListItemValues : function(listItem, item){
listItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.includePattern);
listItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
listItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
},
preferenceChange : function(event) {
Redirector.prefs[event.originalTarget.getAttribute('preference')] = event.originalTarget.hasAttribute('checked');
},
addRedirect : function() {
var args = { saved : false, redirect : new Redirect() };
window.openDialog("chrome://redirector/content/ui/editRedirect.xul", "redirect", "chrome,dialog,modal,centerscreen", args);
if (args.saved) {
this.addItemsToListBox([args.redirect]);
Redirector.addRedirect(args.redirect);
this.selectionChange();
}
},
editRedirect : function() {
if (this.lstRedirects.selectedIndex == -1) {
return;
}
//.selectedItem is still there after it has been removed, that's why we have the .selectedIndex check above as well
var listItem = this.lstRedirects.selectedItem;
if (!listItem) {
return;
}
var redirect = listItem.item;
var args = { saved: false, "redirect":redirect.clone()};
window.openDialog("chrome://redirector/content/ui/editRedirect.xul", "redirect", "chrome,dialog,modal,centerscreen", args);
if (args.saved) {
redirect.copyValues(args.redirect);
this.setListItemValues(listItem, redirect);
Redirector.save();
this.selectionChange();
}
},
deleteRedirect : function() {
var index = this.lstRedirects.selectedIndex;
if (index == -1) {
return;
}
var text = this.strings.getString("deleteConfirmationText");
var title = this.strings.getString("deleteConfirmationTitle");
var reallyDelete = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService).confirm(null, title, text);
if (!reallyDelete) {
return;
}
try {
this.lstRedirects.removeChild(this.lstRedirects.children[index]);
Redirector.deleteAt(index);
this.selectionChange();
} catch(e) {
alert(e);
}
},
selectionChange : function() {
if (!this.lstRedirects) {
return;
}
var index = this.lstRedirects.selectedIndex;
this.btnEdit.disabled = (index == -1);
this.btnDelete.disabled = (index == -1);
this.btnUp.disabled = (index <= 0);
this.btnDown.disabled = (index == -1 || index >= Redirector.list.length-1);
},
importExport : function(mode, captionKey, func) {
//Mostly borrowed from Adblock Plus
var picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
picker.init(window, Redirector.getString(captionKey), mode);
picker.defaultExtension = ".rdx";
var dir = Redirector.getDefaultDir();
if (dir) {
picker.displayDirectory = dir;
}
picker.appendFilter(Redirector.getString('redirectorFiles'), '*.rdx');
if (picker.show() == picker.returnCancel) {
return;
}
try {
func(picker.file);
} catch (e) {
alert(e);
}
},
export : function() {
this.importExport(Ci.nsIFilePicker.modeSave, 'exportCaption', function(file) {
Redirector.exportRedirects(file);
});
},
import : function() {
this.importExport(Ci.nsIFilePicker.modeOpen, 'importCaption', function(file) {
Redirector.importRedirects(file);
});
}
};
|