Skip to content

Commit e398cff

Browse files
authored
Merge pull request #59 from richardfrost/preserve_last
Censor: Preserve last character
2 parents 68194f0 + 08d3aba commit e398cff

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

filter.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var defaults = {
77
"filterMethod": 0, // ["Censor", "Substitute", "Remove"];
88
"globalMatchMethod": 3, // ["Exact", "Partial", "Whole", "Per-Word"]
99
"preserveFirst": false,
10+
"preserveLast": false,
1011
"showCounter": true,
1112
"substitutionMark": true,
1213
"words": {}
@@ -26,7 +27,7 @@ var defaultWords = {
2627
"tits": {"matchMethod": 1, "words": ["explative"] },
2728
"whore": {"matchMethod": 1, "words": ["harlot", "tramp"] }
2829
};
29-
var censorCharacter, censorFixedLength, defaultSubstitutions, disabledDomains, filterMethod, globalMatchMethod, matchMethod, preserveFirst, showCounter, substitutionMark, words, wordList;
30+
var censorCharacter, censorFixedLength, defaultSubstitutions, disabledDomains, filterMethod, globalMatchMethod, matchMethod, preserveFirst, preserveLast, showCounter, substitutionMark, words, wordList;
3031
var wordRegExps = [];
3132
var whitespaceRegExp = new RegExp('\\s');
3233
var xpathDocText = '//*[not(self::script or self::style)]/text()[normalize-space(.) != ""]';
@@ -35,25 +36,25 @@ var xpathNodeText = './/*[not(self::script or self::style)]/text()[normalize-spa
3536
// Word must match exactly (not sub-string)
3637
// /\b(w)ord\b/gi
3738
function buildExactRegexp(word) {
38-
wordRegExps.push(new RegExp('\\b(' + word[0] + ')' + word.substring(1) + '\\b', 'gi' ));
39+
wordRegExps.push(new RegExp('\\b(' + word[0] + ')' + word.slice(1)+ '\\b', 'gi' ));
3940
}
4041

4142
// Match any part of a word (sub-string)
4243
// /(w)ord/gi
4344
function buildPartRegexp(word) {
44-
wordRegExps.push(new RegExp('(' + word[0] + ')' + word.substring(1), 'gi' ));
45+
wordRegExps.push(new RegExp('(' + word[0] + ')' + word.slice(1), 'gi' ));
4546
}
4647

4748
// Match entire word that contains sub-string
4849
// /\b[\w-]*(w)ord[\w-]*\b/gi
4950
function buildWholeRegexp(word) {
50-
wordRegExps.push(new RegExp('\\b([\\w-]*' + word[0] + ')' + word.substring(1) + '[\\w-]*\\b', 'gi' ));
51+
wordRegExps.push(new RegExp('\\b([\\w-]*' + word[0] + ')' + word.slice(1) + '[\\w-]*\\b', 'gi' ))
5152
}
5253

5354
// Match entire word that contains sub-string and surrounding whitespace
5455
// /\s?\b[\w-]*(w)ord[\w-]*\b\s?/gi
5556
function buildWholeRegexpForRemove(word) {
56-
wordRegExps.push(new RegExp('\\s?\\b([\\w-]*' + word[0] + ')' + word.substring(1) + '[\\w-]*\\b\\s?', 'gi' ));
57+
wordRegExps.push(new RegExp('\\s?\\b([\\w-]*' + word[0] + ')' + word.slice(1) + '[\\w-]*\\b\\s?', 'gi' ));
5758
}
5859

5960
function checkNodeForProfanity(mutation) {
@@ -70,14 +71,22 @@ function censorReplace(strMatchingString, strFirstLetter) {
7071
var censoredString = '';
7172

7273
if (censorFixedLength > 0) {
73-
if (preserveFirst) {
74-
censoredString = strFirstLetter[0] + censorCharacter.repeat((censorFixedLength - 1));
74+
if (preserveFirst && preserveLast) {
75+
censoredString = strFirstLetter + censorCharacter.repeat((censorFixedLength - 2)) + strMatchingString.slice(-1);
76+
} else if (preserveFirst) {
77+
censoredString = strFirstLetter + censorCharacter.repeat((censorFixedLength - 1));
78+
} else if (preserveLast) {
79+
censoredString = censorCharacter.repeat((censorFixedLength - 1)) + strMatchingString.slice(-1);
7580
} else {
7681
censoredString = censorCharacter.repeat(censorFixedLength);
7782
}
7883
} else {
79-
if (preserveFirst) {
80-
censoredString = strFirstLetter[0] + censorCharacter.repeat((strMatchingString.length - 1));
84+
if (preserveFirst && preserveLast) {
85+
censoredString = strFirstLetter + censorCharacter.repeat((strMatchingString.length - 2)) + strMatchingString.slice(-1);
86+
} else if (preserveFirst) {
87+
censoredString = strFirstLetter + censorCharacter.repeat((strMatchingString.length - 1));
88+
} else if (preserveLast) {
89+
censoredString = censorCharacter.repeat((strMatchingString.length - 1)) + strMatchingString.slice(-1);
8190
} else {
8291
censoredString = censorCharacter.repeat(strMatchingString.length);
8392
}
@@ -110,6 +119,7 @@ function cleanPage() {
110119
globalMatchMethod = storage.globalMatchMethod;
111120
matchMethod = storage.matchMethod;
112121
preserveFirst = storage.preserveFirst;
122+
preserveLast = storage.preserveLast;
113123
showCounter = storage.showCounter;
114124
substitutionMark = storage.substitutionMark;
115125
words = storage.words;

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"short_name": "Profanity Filter",
44
"author": "phermium",
55
"manifest_version": 2,
6-
"version": "1.0.2",
6+
"version": "1.0.3",
77
"description": "Hide offensive words on the webpages you visit",
88
"icons": {
99
"16": "icons/icon16.png",

options.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ <h4>Censor Settings</h4>
3131
<span class="notes">(Dog: dog = d**)</span>
3232
</label>
3333
<br />
34+
<label>
35+
<input type="checkbox" id="preserveLast">
36+
Preserve the last letter
37+
<span class="notes">(Dog: dog = **g)</span>
38+
</label>
39+
<br />
3440
<label>
3541
Censor character:
3642
<select id="censorCharacterSelect" class="small">

options.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var defaults = {
77
"filterMethod": 0, // ["Censor", "Substitute", "Remove"];
88
"globalMatchMethod": 3, // ["Exact", "Partial", "Whole", "Per-Word"]
99
"preserveFirst": false,
10+
"preserveLast": false,
1011
"showCounter": true,
1112
"substitutionMark": true,
1213
"words": {}
@@ -240,6 +241,7 @@ function populateOptions() {
240241
document.getElementById('censorFixedLengthSelect').selectedIndex = settings.censorFixedLength;
241242
document.getElementById('censorCharacterSelect').value = settings.censorCharacter;
242243
document.getElementById('preserveFirst').checked = settings.preserveFirst;
244+
document.getElementById('preserveLast').checked = settings.preserveLast;
243245
document.getElementById('substitutionMark').checked = settings.substitutionMark;
244246
document.getElementById('showCounter').checked = settings.showCounter;
245247
dynamicList(matchMethods, 'globalMatchMethodSelect');
@@ -262,10 +264,10 @@ function restoreDefaults() {
262264
exportConfig();
263265
chrome.storage.sync.clear(function(){
264266
if (chrome.runtime.lastError) {
265-
updateStatus('Error restoring defaults! Please try again.', true, 5000);
267+
updateStatus('Error restoring defaults!', true, 5000);
266268
} else {
267269
populateOptions();
268-
updateStatus('Settings restored!', false, 3000);
270+
updateStatus('Default settings restored!', false, 3000);
269271
}
270272
});
271273
}
@@ -276,6 +278,7 @@ function saveOptions(event, settings) {
276278
if (settings === undefined) {
277279
settings = {};
278280
settings.preserveFirst = document.getElementById('preserveFirst').checked;
281+
settings.preserveLast = document.getElementById('preserveLast').checked;
279282
settings.showCounter = document.getElementById('showCounter').checked;
280283
settings.substitutionMark = document.getElementById('substitutionMark').checked;
281284
}
@@ -285,7 +288,6 @@ function saveOptions(event, settings) {
285288
if (chrome.runtime.lastError) {
286289
updateStatus('Settings not saved! Please try again.', true, 5000);
287290
} else {
288-
updateStatus('Settings saved successfully!', false, 3000);
289291
populateOptions();
290292
}
291293
});
@@ -336,7 +338,7 @@ function wordAdd(event) {
336338
var word = document.getElementById('wordText').value;
337339
if (word != "") {
338340
if (!arrayContains(Object.keys(config.words), word)) {
339-
config.words[word] = {"matchMethod": 1, "words": []};
341+
config.words[word] = {"matchMethod": 0, "words": []};
340342
saveOptions(event, config);
341343
document.getElementById('wordText').value = "";
342344
} else {
@@ -379,6 +381,7 @@ window.addEventListener('load', populateOptions);
379381
document.getElementById('filterMethodSelect').addEventListener('change', filterMethodSelect);
380382
// Filter - Censor
381383
document.getElementById('preserveFirst').addEventListener('click', saveOptions);
384+
document.getElementById('preserveLast').addEventListener('click', saveOptions);
382385
document.getElementById('censorCharacterSelect').addEventListener('change', censorCharacter);
383386
document.getElementById('censorFixedLengthSelect').addEventListener('change', censorFixedLength);
384387
// Filter - Substitute

0 commit comments

Comments
 (0)