forked from FrostCo/AdvancedProfanityFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
126 lines (109 loc) · 3.77 KB
/
filter.js
File metadata and controls
126 lines (109 loc) · 3.77 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
var wordList, preserveFirst, filterSubstring, showCounter;
var wordRegExps = [];
var defaults = {'wordList': 'asshole,bastard,bitch,cunt,damn,fuck,piss,slut,shit,tits,whore', 'preserveFirst': false, 'filterSubstring': true, 'showCounter': true};
var counter = 0;
var xpathDocText = '//*[not(self::script or self::style)]/text()[normalize-space(.) != ""]';
var xpathNodeText = './/*[not(self::script or self::style)]/text()[normalize-space(.) != ""]';
function checkNodeForProfanity(mutation) {
mutation.addedNodes.forEach(function(node) {
if (!isForbiddenNode(node)) {
removeProfanity(xpathNodeText, node);
}
});
}
function cleanPage() {
chrome.storage.sync.get(defaults, function(storage) {
// Load settings and setup environment
wordList = storage.wordList.split(',');
filterSubstring = storage.filterSubstring;
preserveFirst = storage.preserveFirst;
showCounter = storage.showCounter;
generateRegexpList();
// Remove profanity from the main document and watch for new nodes
removeProfanity(xpathDocText);
updateCounterBadge();
});
observeNewNodes();
}
// Parse the profanity list
function generateRegexpList() {
if (filterSubstring) {
for (var x = 0; x < wordList.length; x++) {
wordRegExps.push(new RegExp('(' + wordList[x][0] + ')' + wordList[x].substring(1), 'gi' ));
}
} else {
for (var x = 0; x < wordList.length; x++) {
wordRegExps.push(new RegExp('\\b(' + wordList[x][0] + ')' + wordList[x].substring(1) + '\\b', 'gi' ));
}
}
}
// 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")
);
}
// Watch for new text nodes and clean them as they are added
function observeNewNodes() {
var observerConfig = {
childList: true,
subtree: true
};
// When DOM is modified, remove profanity from inserted node
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
checkNodeForProfanity(mutation);
});
updateCounterBadge();
});
// Remove profanity from new objects
observer.observe(document, observerConfig);
}
function removeProfanity(xpathExpression, node) {
node = (typeof node !== 'undefined') ? node : document;
var evalResult = document.evaluate(
xpathExpression,
node,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (var i = 0; i < evalResult.snapshotLength; i++) {
var textNode = evalResult.snapshotItem(i);
textNode.data = replaceText(textNode.data);
}
}
function replaceText(str) {
for (var z = 0; z < wordList.length; z++) {
str = str.replace(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 (!preserveFirst) {
for (var i = 0; i < strMatchingString.length; i++) {
starString = starString + '*';
}
} else {
starString = strFirstLetter;
for (var i = 1; i < strMatchingString.length; i++) {
starString = starString + '*';
}
}
counter++;
return starString;
}
function updateCounterBadge() {
if (showCounter && counter > 0) {
chrome.runtime.sendMessage({counter: counter.toString()});
}
}
cleanPage();