-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.js
225 lines (204 loc) · 8.58 KB
/
options.js
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
// ------------------ RegEx Formatting -----------------------------
function empty(string, mod) {
// console.log("empty(" + string + ", " + mod + ")");
var test = "This is a test string that will catch any empty regex.";
var regx = new RegExp(string, mod);
var matches = test.match(regx);
if(matches) {
for(var i=0; i<matches.length; i++) {
if(matches[i]=="") {
// console.log(" - Empty 2");
return true;
}
}
}
// console.log(" - Not Empty");
return false;
}
$(document).ready(function() {
//----------- VARIABLES -------------------------
var findWords = [], replaceWords = [], modifiers = []; // permanent replace pairs
var table, headerCheckbox, boxes, headerCheckHandler;
var mC; // bool to match case or not
var wW; // bool for whole words or not
var rX; // bool for regex or not
mC = wW = rX = false;
//----------- TABLE INITIALIZATION -------------------------
// get current find/replace pairs from storage
chrome.storage.local.get({"permanentF":[], "permanentR":[], "permanentM":[]}, function(data) {
if((typeof data.permanentF == "undefined")||(typeof data.permanentR == "undefined")||(typeof data.permanentM == "undefined")) { // error
}
else {
// save data locally
findWords = data.permanentF;
replaceWords = data.permanentR;
modifiers = data.permanentM;
// console.log("data retrieved");
// console.log(findWords);
// console.log(replaceWords);
// console.log(modifiers);
// load pairs into table
// console.log(findWords.length);
for(var i=0, length=findWords.length; i<length; i++) {
// console.log("NEXT LOAD");
var tblMC = (modifiers[i][0]?'<i class="material-icons">check</i>':'');
var tblWW = (modifiers[i][1]?'<i class="material-icons">check</i>':'');
var tblRX = (modifiers[i][2]?'<i class="material-icons">check</i>':'');
var tblMod = (modifiers[i][2]?modifiers[i][3]:'');
$("#data").append('<tr><td><label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row['+(i)+']"><input type="checkbox" id="row['+(i)+']" class="mdl-checkbox__input"></label></td><td class="mdl-data-table__cell--non-numeric">' + findWords[i] + '</td><td>' + replaceWords[i] + '</td><td>' + tblMC + '</td><td>' + tblWW + '</td><td>' + tblRX + '</td><td>' + tblMod + '</td></tr>');
}
var el = document.getElementById('data');
componentHandler.upgradeElements(el);
// initialize table
table = document.querySelector('table');
headerCheckbox = table.querySelector('thead .mdl-data-table__select input');
boxes = table.querySelectorAll('tbody .mdl-data-table__select');
headerCheckHandler = function(event) {
if(event.target.checked) {
for(var i=0, length=boxes.length; i<length; i++) {
boxes[i].MaterialCheckbox.check();
}
}
else {
for(var i=0, length=boxes.length; i<length; i++) {
boxes[i].MaterialCheckbox.uncheck();
}
}
};
headerCheckbox.addEventListener('change', headerCheckHandler);
}
});
// Deactivate button if find text is nonempty
$("#findT").keyup(function(ev) {
$("#status").text("");
$("#status").removeClass("error");
if($("#findT").val()=="") {
$("#saveButton").addClass("mdl-button--disabled");
$("#saveButton").prop("disabled", true);
}
else {
$("#saveButton").removeClass("mdl-button--disabled");
$("#saveButton").prop("disabled", false);
}
});
//----------- OPTIONS -------------------------
// Match Case
$("#matchCase").click(function() {
// console.log("Option: Match Case");
mC = !mC;
});
// Whole Words
$("#wholeWords").click(function() {
// console.log("Option: Whole Words");
wW = !wW;
});
// Use RegEx
$("#regex").click(function() {
// console.log("Option: use RegEx");
rX = !rX;
if(rX) { // disables match case, whole words; enables modifiers
document.querySelector("#matchCase").parentElement.MaterialCheckbox.disable();
document.querySelector("#wholeWords").parentElement.MaterialCheckbox.disable();
document.querySelector("#global-toggle").parentElement.MaterialIconToggle.enable();
document.querySelector("#case-toggle").parentElement.MaterialIconToggle.enable();
document.querySelector("#multiline-toggle").parentElement.MaterialIconToggle.enable();
}
else {
document.querySelector("#matchCase").parentElement.MaterialCheckbox.enable();
document.querySelector("#wholeWords").parentElement.MaterialCheckbox.enable();
document.querySelector("#global-toggle").parentElement.MaterialIconToggle.disable();
document.querySelector("#case-toggle").parentElement.MaterialIconToggle.disable();
document.querySelector("#multiline-toggle").parentElement.MaterialIconToggle.disable();
}
});
//----------- MODIFIERS (REGEX ONLY) -------------------------
// Use g
$("#global-toggle").click(function() {
});
// Use i
$("#case-toggle").click(function() {
});
// Use m
$("#multiline-toggle").click(function() {
});
//----------- BUTTONS -------------------------
// save new pair
$("#saveButton").click(function() {
// console.log("saveClicked");
var curF = $("#findT").val();
var curR = $("#replaceT").val();
var curM="";
if(rX) {
if($("#global-toggle").is(":checked")) curM+="g";
if($("#case-toggle").is(":checked")) curM+="i";
if($("#multiline-toggle").is(":checked")) curM+="m";
try {
new RegExp(curF);
} catch(e) {
// console.log("Invalid Regular Expression");
$("#status").text("Error");
$("#status").addClass("error");
return;
}
if(empty(curF, curM)) {
// console.log("Empty Regular Expression");
$("#status").text("Infinite");
$("#status").addClass("error");
return;
}
}
else {
curM = "g";
if(!mC) curM += "i";
if(findWords.indexOf(curF)>=0) {
// console.log("Repeat Error");
$("#status").text("Repeat");
$("#status").addClass("error");
return;
}
}
findWords.push(curF);
replaceWords.push(curR);
modifiers.push([mC, wW, rX, curM]);
// console.log("SAVING");
// console.log(curF);
var i = modifiers.length-1;
var tblMC = (modifiers[i][0]?'<i class="material-icons">check</i>':'');
var tblWW = (modifiers[i][1]?'<i class="material-icons">check</i>':'');
var tblRX = (modifiers[i][2]?'<i class="material-icons">check</i>':'');
var tblMod = (modifiers[i][2]?modifiers[i][3]:'');
$("#data").append('<tr><td><label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row['+(i)+']"><input type="checkbox" id="row['+(i)+']" class="mdl-checkbox__input"></label></td><td class="mdl-data-table__cell--non-numeric">' + findWords[i] + '</td><td>' + replaceWords[i] + '</td><td>' + tblMC + '</td><td>' + tblWW + '</td><td>' + tblRX + '</td><td>' + tblMod + '</td></tr>');
boxes = table.querySelectorAll('tbody .mdl-data-table__select');
var el = document.getElementById('data');
componentHandler.upgradeElements(el);
chrome.storage.local.set({
'permanentF': findWords,
'permanentR': replaceWords,
'permanentM': modifiers
}, function() {
chrome.runtime.sendMessage({type: "permSet"});
});
$("#findT").val('');
$("#replaceT").val('');
$("#findT").focus();
});
$("#deleteButton").click(function() {
for(var i=boxes.length-1; i>=0; i--) {
if($(boxes[i]).hasClass('is-checked')) {
// console.log("CHECKED");
$("#data").children().eq(i).remove();
findWords.splice(i,1);
replaceWords.splice(i,1);
modifiers.splice(i,1);
}
}
table.querySelector('thead .mdl-data-table__select').MaterialCheckbox.uncheck();
chrome.storage.local.set({
'permanentF': findWords,
'permanentR': replaceWords,
'permanentM': modifiers
}, function() {
chrome.runtime.sendMessage({type: "permSet"});
});
});
});