forked from panicsteve/cloud-to-butt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
78 lines (54 loc) · 2.08 KB
/
content_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
jqn2n = jQuery.noConflict(true);
// Make new jQuery selector :containsNC. A case-insensitive :contains.
jqn2n.extend(jqn2n.expr[":"], {
"containsNC": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
(function(jqn2n){
url = 'https://ianfitzpatrick.com/n2n/normalization-to-not-phrases.json'
n2n = {
init: function() {
n2n.get_phrases();
},
get_phrases: function() {
jqn2n.getJSON(url, n2n.phrases_retrieved);
},
phrases_retrieved: function(data){
for (var key in data) {
n2n.replace_phrases(key, data[key])
}
},
replace_phrases: function(old_phrase, new_phrase) {
// There must be a smarter way to build this selector
// But targeting body:contains can wreak havoc on a page
jqn2n(" h1:containsNC('" + old_phrase +"'), \
h2:containsNC('" + old_phrase +"'), \
h3:containsNC('" + old_phrase +"'), \
h4:containsNC('" + old_phrase +"'), \
h5:containsNC('" + old_phrase +"'), \
h6:containsNC('" + old_phrase +"'), \
p:containsNC('" + old_phrase +"'), \
blockquote:containsNC('" + old_phrase +"')")
.html(function(_, html) {
regex = new RegExp("[^<\\/>]\\b(" + old_phrase + ")\\b", "gi");
return html.replace(regex, function(match){
new_phrase = n2n.match_capitalization(match, new_phrase);
return '<span style="text-decoration: line-through; text-shadow: none; color: #9c9c9c;">' + match + '</span><span style="color: red;"> ' + new_phrase +'</span>'
});
});
},
match_capitalization: function(match_phrase, new_phrase) {
// Try our best to make new phrase match capitalization of matched phrase
if (match_phrase === match_phrase.toUpperCase() ) {
return new_phrase.toUpperCase();
} else if (match_phrase === match_phrase.toLowerCase() ) {
return new_phrase.toLowerCase();
} else {
// Title Case
return new_phrase.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
}
};
n2n.init();
})(jqn2n);