-
Notifications
You must be signed in to change notification settings - Fork 3
/
c_script.js
350 lines (320 loc) · 11.7 KB
/
c_script.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// ------------------ CHECK IF CONTENT SCRIPT IS LOADED -------------------
// chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// if(request.ping) {
// console.log("Message from background caught in content");
// sendResponse({pong: true});
// }
// });
// ------------------ State of Matches -----------------------------
var numMatches; // number of matches; set by highlightText, replaceText
var curPos; // current position of focus; modified by all functions
// setValues: Set the current values into memory
function setValues() {
if(numMatches==0) curPos=0;
// console.log(curPos.toString() + " of " + numMatches.toString());
chrome.storage.local.set({
'numMatches':numMatches,
'curPos':curPos
}, function() {
chrome.runtime.sendMessage({type: "setValues"});
});
}
// ------------------ Visibility Functions -----------------------------
// isElementInViewport: determines whether el is currently in the viewport
function isElementInViewport (el) {
el = el[0];
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= $(window).height() &&
rect.right <= $(window).width() &&
rect.top<rect.bottom &&
rect.left<rect.right
);
}
// isElementVisible: determines whether el is visible on page
function isElementVisible (el) {
var o = el.offset();
return (
el.is(":visible") &&
!(el.css("visibility") == "hidden") &&
!(el.css("visibility") == "collapse") &&
el.height()>0 &&
el.width()>0 &&
o.left+el.width()>0 &&
o.top+el.height()>0
);
}
// countMatches: counts the number of visible matches and puts them into numMatches
function countMatches() {
$("span.foundLELELEL").each(function() {
if(isElementVisible($(this))) numMatches++;
});
}
// ------------------ Helper Functions -----------------------------
// testUHL(): unhighlights all matches
function testUHL() {
// console.log("testUHL called");
$("span.foundLELELEL").each(function() {
var pNode = this.parentNode;
pNode.replaceChild(this.firstChild, this);
pNode.normalize();
});
}
// ------------------ Button Functions -----------------------------
// fAll(): finds all matches and focuses on the first one
function fAll(reg, mod) {
var toFind = new RegExp(reg, mod);
// console.log("fAll(" + reg + ", " + mod + ") called: " + toFind.toString())
numMatches=0;
testUHL(); // unhighlights previous matches
$("body *").not("style").not("script").not("noscript").highlightText(toFind); // highlights new matches
focusFirst(); // sets focus on the first one
countMatches();
setValues();
}
// rAll(): unhighlights all previous matches and replaces everything (including tab title)
function rAll(reg, mod, rep) {
var toFind = new RegExp(reg, mod);
// console.log("rAll() called: " + toFind.toString() + " -> " + rep);
testUHL(); // unhighlights previous matches
$("*").not("style").not("script").replaceText(toFind, rep); // replaces everything
$("input, textarea, select").each(function() {
if($(this).val()=="") {
var new_val = $(this).attr("placeholder");
if(typeof new_val === 'string') new_val = new_val.replace(toFind, rep);
$(this).attr("placeholder", new_val);
}
});
numMatches=0;
curPos=0;
setValues();
}
// rInput(): unhighlights all previous matches and replaces everything inside an input field
function rInput(reg, mod, rep) {
var toFind = new RegExp(reg, mod);
// console.log("rInput() called: " + toFind.toString() + " -> " + rep);
testUHL(); // unhighlight previous matches (just in case)
$("input, textarea, select").each(function() {
if($(this).val()=="") {
// console.log(" - Placeholder");
var new_val = $(this).attr("placeholder");
if(typeof new_val === 'string') new_val = new_val.replace(toFind, rep);
$(this).attr("placeholder", new_val);
}
else {
// console.log(" - Value");
var old_val = $(this).val();
var new_val;
if(typeof old_val === 'string') new_val = old_val.replace(toFind, rep);
else if(Array.isArray(old_val)) {
$.each(old_val, function() {
if($.type($(this)) === 'string') new_val.push(this.replace(toFind, rep));
else new_val.push(this);
});
}
$(this).val(new_val);
}
});
// value text of button, email, number, range, search, submit, tel, text, url, week
}
// focusFirst(): focuses on first match
function focusFirst() {
curPos=0;
// console.log("focusFirst called");
$("span.foundLELELEL").each(function() {
if(isElementVisible($(this))) {
curPos++;
$(this).addClass("curLELELEL");
//$("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
return false;
}
});
}
// focusLast(): focuses on last match
function focusLast() {
curPos=numMatches+1;
// console.log("focusLast called");
$($("span.foundLELELEL").get().reverse()).each(function() {
if(isElementVisible($(this))) {
curPos--;
$(this).addClass("curLELELEL");
// $("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
return false;
}
});
if(numMatches==0) curPos=0;
}
// focusNext(): focuses on next match
function focusNext() {
// console.log("focusNext called");
var next=false;
$("span.foundLELELEL").each(function() {
if(next && isElementVisible($(this))) {
curPos++;
$(this).addClass("curLELELEL");
// $("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
next=false;
return false;
}
if($(this).hasClass("curLELELEL")) {
$(this).removeClass("curLELELEL");
next=true;
}
});
if(next) focusFirst();
setValues();
}
// focusPrev(): focuses on previous match
function focusPrev() {
// console.log("focusPrev called");
var next=false;
$($("span.foundLELELEL").get().reverse()).each(function() {
if(next && isElementVisible($(this))) {
curPos--;
$(this).addClass("curLELELEL");
// $("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
next=false;
return false;
}
if($(this).hasClass("curLELELEL")) {
next=true;
$(this).removeClass("curLELELEL");
}
});
if(next) focusLast();
setValues();
};
// replacePrev(): replaces current match and focuses on previous
function replacePrev(rstr) {
// console.log("replacePrev called");
var next=false;
var node;
$($("span.foundLELELEL").get().reverse()).each(function() {
if(next && isElementVisible($(this))) {
curPos--;
$(this).addClass("curLELELEL");
// $("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
next=false;
return false;
}
if($(this).hasClass("curLELELEL")) {
numMatches--;
node=this;
next=true;
$(this).removeClass("curLELELEL");
}
});
if(next) focusLast();
if(node) {
var pNode = node.parentNode;
pNode.replaceChild(document.createTextNode(rstr), node);
pNode.normalize();
}
setValues();
}
// replaceNext(): replaces current match and focuses on next
function replaceNext(rstr) {
// console.log("replaceNext called");
var next=false;
var node;
$("span.foundLELELEL").each(function() {
if(next && isElementVisible($(this))) {
$(this).addClass("curLELELEL");
// $("html, body").scrollTop($(this).offset().top);
if(!isElementInViewport($(this))) $("html, body").scrollTop(-0.5*($(window).height()) + $(this).offset().top)
next=false;
return false;
}
if($(this).hasClass("curLELELEL")) {
numMatches--;
node = this;
$(this).removeClass("curLELELEL");
next=true;
}
});
if(next) focusFirst();
if(node) {
var pNode = node.parentNode;
pNode.replaceChild(document.createTextNode(rstr), node);
pNode.normalize();
}
setValues();
}
function escapeHTML(s) {
return s.replace(/[&"<>]/g, function(c) {
return {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
}[c];
});
}
function unescapeHTML(s) {
return s.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/&/g, '&');
}
// ------------------ Replacement Plugins -----------------------------
// $(...).highlightText(): highlights all instances of 'search' in the given elements - only considers text nodes
$.fn.highlightText = function (search) {
return this.each(function () {
// if(isElementVisible($(this))) {
// this.normalize(); // ******* CHECK IF THIS IS NECESSARY *******
var node = this.firstChild,
val,
new_val,
remove = [];
if (node) {
do {
if (node.nodeType === 3) {
val = node.nodeValue;
//val = escapeHTML(val);
//numMatches += ((val.match(search)||[]).length);
// var notmatch = val.split(search);
// new_val = val.replace(search, function(match) {
// return '<span class="foundLELELEL">' + escapeHTML(match) + '</span>';
// });
new_val = val.replace(search, 'foundLELELEL_s$&foundLELELEL_e');
new_val = escapeHTML(new_val);
new_val = new_val.replace(/foundLELELEL_s/g, '<span class="foundLELELEL">').replace(/foundLELELEL_e/g, "</span>");
if (new_val !== val) {
$(node).before((new_val));
remove.push(node);
}
}
} while (node = node.nextSibling);
}
remove.length && $(remove).remove();
// this.normalize(); // ******* CHECK IF THIS IS NECESSARY *******
// }
});
};
// $(...).replaceText(): replaces all instances of 'search' with 'replace' in the given elements - only considers text nodes
$.fn.replaceText = function (search, replace) {
return this.each(function () {
var node = this.firstChild,
val,
new_val;
if (node) {
do {
if (node.nodeType === 3) {
val = node.nodeValue;
// val = escapeHTML(val);
new_val = val.replace(search, replace);
if (new_val !== val) {
node.nodeValue = new_val;
}
}
} while (node = node.nextSibling);
}
});
};