forked from FrostCo/AdvancedProfanityFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_filter.js
More file actions
150 lines (129 loc) · 4.43 KB
/
dev_filter.js
File metadata and controls
150 lines (129 loc) · 4.43 KB
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
var defaults = {'wordList': 'asshole,bastard,bitch,cunt,damn,fuck,piss,slut,shit,tits,whore', 'preserveFirst': false, 'filterSubstring': true, 'showCounter': true};
var settings = {};
function cleanPage() {
chrome.storage.sync.get(defaults, function(storage) {
// Load settings and setup environment
settings.wordList = storage.wordList.split(',');
settings.filterSubstring = storage.filterSubstring;
settings.preserveFirst = storage.preserveFirst;
settings.showCounter = storage.showCounter;
settings.counter = 0;
settings.wordRegExps = [];
generateRegexpList();
// Start cleaning the page
walkAndObserve(document);
});
}
// Build a RegExp for each word in the wordList
function generateRegexpList() {
var wordList = settings.wordList;
if (settings.filterSubstring) {
for (var x = 0; x < wordList.length; x++) {
settings.wordRegExps.push(new RegExp('(' + wordList[x][0] + ')' + wordList[x].substring(1), 'gi' ));
}
} else {
for (var x = 0; x < wordList.length; x++) {
settings.wordRegExps.push(new RegExp('\\b(' + wordList[x][0] + ')' + wordList[x].substring(1) + '\\b', 'gi' ));
}
}
}
function handleText(textNode) {
textNode.nodeValue = replaceText(textNode.nodeValue);
}
// Returns true if a node should *not* be altered in any way
// Credit: https://github.com/ericwbailey/millennials-to-snake-people/blob/master/Source/content_script.js
function isForbiddenNode(node) {
return node.isContentEditable || // DraftJS and many others
(node.parentNode && node.parentNode.isContentEditable) || // Special case for Gmail
(node.tagName && (node.tagName.toLowerCase() == "textarea" || // Some catch-alls
node.tagName.toLowerCase() == "input" ||
node.tagName.toLowerCase() == "script" ||
node.tagName.toLowerCase() == "style")
);
}
// The callback used for the document body and title observers
function observerCallback(mutations) {
var i, node;
mutations.forEach(function(mutation) {
for (i = 0; i < mutation.addedNodes.length; i++) {
node = mutation.addedNodes[i];
if (isForbiddenNode(node)) {
// Should never operate on user-editable content
continue;
} else if (node.nodeType === 3) {
// Replace the text for text nodes
handleText(node);
} else {
// Otherwise, find text nodes within the given node and replace text
walk(node);
}
}
});
updateCounterBadge();
}
function replaceText(str) {
for (var z = 0; z < settings.wordList.length; z++) {
str = str.replace(settings.wordRegExps[z], starReplace);
}
return str;
}
// Replace the profanity with a string of asterisks
// Only gets run when there is a match in replaceText()
function starReplace(strMatchingString, strFirstLetter) {
var starString = '';
if (!settings.preserveFirst) {
for (var i = 0; i < strMatchingString.length; i++) {
starString = starString + '*';
}
} else {
starString = strFirstLetter;
for (var i = 1; i < strMatchingString.length; i++) {
starString = starString + '*';
}
}
settings.counter++;
return starString;
}
// Updates the counter and displays it if enabled
function updateCounterBadge() {
if (settings.showCounter && settings.counter > 0) {
chrome.runtime.sendMessage({counter: settings.counter.toString()});
}
}
function walk(rootNode) {
// Find all the text nodes in rootNode
var walker = document.createTreeWalker(
rootNode,
NodeFilter.SHOW_TEXT,
null,
false
),
node;
// Modify each text node's value
while (node = walker.nextNode()) {
handleText(node);
}
}
// Walk the doc (document) body, replace the title, and observe the body and title
function walkAndObserve(doc) {
var docTitle = doc.getElementsByTagName('title')[0],
observerConfig = {
characterData: true,
childList: true,
subtree: true
},
bodyObserver, titleObserver;
// Do the initial text replacements in the document body and title
walk(doc.body);
doc.title = replaceText(doc.title);
updateCounterBadge();
// Observe the body so that we replace text in any added/modified nodes
bodyObserver = new MutationObserver(observerCallback);
bodyObserver.observe(doc.body, observerConfig);
// Observe the title so we can handle any modifications there
if (docTitle) {
titleObserver = new MutationObserver(observerCallback);
titleObserver.observe(docTitle, observerConfig);
}
}
cleanPage();