Skip to content

Commit

Permalink
Merge pull request #59 from richardfrost/preserve_last
Browse files Browse the repository at this point in the history
Censor: Preserve last character
  • Loading branch information
richardfrost authored Feb 8, 2018
2 parents 68194f0 + 08d3aba commit e398cff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
28 changes: 19 additions & 9 deletions filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var defaults = {
"filterMethod": 0, // ["Censor", "Substitute", "Remove"];
"globalMatchMethod": 3, // ["Exact", "Partial", "Whole", "Per-Word"]
"preserveFirst": false,
"preserveLast": false,
"showCounter": true,
"substitutionMark": true,
"words": {}
Expand All @@ -26,7 +27,7 @@ var defaultWords = {
"tits": {"matchMethod": 1, "words": ["explative"] },
"whore": {"matchMethod": 1, "words": ["harlot", "tramp"] }
};
var censorCharacter, censorFixedLength, defaultSubstitutions, disabledDomains, filterMethod, globalMatchMethod, matchMethod, preserveFirst, showCounter, substitutionMark, words, wordList;
var censorCharacter, censorFixedLength, defaultSubstitutions, disabledDomains, filterMethod, globalMatchMethod, matchMethod, preserveFirst, preserveLast, showCounter, substitutionMark, words, wordList;
var wordRegExps = [];
var whitespaceRegExp = new RegExp('\\s');
var xpathDocText = '//*[not(self::script or self::style)]/text()[normalize-space(.) != ""]';
Expand All @@ -35,25 +36,25 @@ var xpathNodeText = './/*[not(self::script or self::style)]/text()[normalize-spa
// Word must match exactly (not sub-string)
// /\b(w)ord\b/gi
function buildExactRegexp(word) {
wordRegExps.push(new RegExp('\\b(' + word[0] + ')' + word.substring(1) + '\\b', 'gi' ));
wordRegExps.push(new RegExp('\\b(' + word[0] + ')' + word.slice(1)+ '\\b', 'gi' ));
}

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

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

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

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

if (censorFixedLength > 0) {
if (preserveFirst) {
censoredString = strFirstLetter[0] + censorCharacter.repeat((censorFixedLength - 1));
if (preserveFirst && preserveLast) {
censoredString = strFirstLetter + censorCharacter.repeat((censorFixedLength - 2)) + strMatchingString.slice(-1);
} else if (preserveFirst) {
censoredString = strFirstLetter + censorCharacter.repeat((censorFixedLength - 1));
} else if (preserveLast) {
censoredString = censorCharacter.repeat((censorFixedLength - 1)) + strMatchingString.slice(-1);
} else {
censoredString = censorCharacter.repeat(censorFixedLength);
}
} else {
if (preserveFirst) {
censoredString = strFirstLetter[0] + censorCharacter.repeat((strMatchingString.length - 1));
if (preserveFirst && preserveLast) {
censoredString = strFirstLetter + censorCharacter.repeat((strMatchingString.length - 2)) + strMatchingString.slice(-1);
} else if (preserveFirst) {
censoredString = strFirstLetter + censorCharacter.repeat((strMatchingString.length - 1));
} else if (preserveLast) {
censoredString = censorCharacter.repeat((strMatchingString.length - 1)) + strMatchingString.slice(-1);
} else {
censoredString = censorCharacter.repeat(strMatchingString.length);
}
Expand Down Expand Up @@ -110,6 +119,7 @@ function cleanPage() {
globalMatchMethod = storage.globalMatchMethod;
matchMethod = storage.matchMethod;
preserveFirst = storage.preserveFirst;
preserveLast = storage.preserveLast;
showCounter = storage.showCounter;
substitutionMark = storage.substitutionMark;
words = storage.words;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"short_name": "Profanity Filter",
"author": "phermium",
"manifest_version": 2,
"version": "1.0.2",
"version": "1.0.3",
"description": "Hide offensive words on the webpages you visit",
"icons": {
"16": "icons/icon16.png",
Expand Down
6 changes: 6 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ <h4>Censor Settings</h4>
<span class="notes">(Dog: dog = d**)</span>
</label>
<br />
<label>
<input type="checkbox" id="preserveLast">
Preserve the last letter
<span class="notes">(Dog: dog = **g)</span>
</label>
<br />
<label>
Censor character:
<select id="censorCharacterSelect" class="small">
Expand Down
11 changes: 7 additions & 4 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var defaults = {
"filterMethod": 0, // ["Censor", "Substitute", "Remove"];
"globalMatchMethod": 3, // ["Exact", "Partial", "Whole", "Per-Word"]
"preserveFirst": false,
"preserveLast": false,
"showCounter": true,
"substitutionMark": true,
"words": {}
Expand Down Expand Up @@ -240,6 +241,7 @@ function populateOptions() {
document.getElementById('censorFixedLengthSelect').selectedIndex = settings.censorFixedLength;
document.getElementById('censorCharacterSelect').value = settings.censorCharacter;
document.getElementById('preserveFirst').checked = settings.preserveFirst;
document.getElementById('preserveLast').checked = settings.preserveLast;
document.getElementById('substitutionMark').checked = settings.substitutionMark;
document.getElementById('showCounter').checked = settings.showCounter;
dynamicList(matchMethods, 'globalMatchMethodSelect');
Expand All @@ -262,10 +264,10 @@ function restoreDefaults() {
exportConfig();
chrome.storage.sync.clear(function(){
if (chrome.runtime.lastError) {
updateStatus('Error restoring defaults! Please try again.', true, 5000);
updateStatus('Error restoring defaults!', true, 5000);
} else {
populateOptions();
updateStatus('Settings restored!', false, 3000);
updateStatus('Default settings restored!', false, 3000);
}
});
}
Expand All @@ -276,6 +278,7 @@ function saveOptions(event, settings) {
if (settings === undefined) {
settings = {};
settings.preserveFirst = document.getElementById('preserveFirst').checked;
settings.preserveLast = document.getElementById('preserveLast').checked;
settings.showCounter = document.getElementById('showCounter').checked;
settings.substitutionMark = document.getElementById('substitutionMark').checked;
}
Expand All @@ -285,7 +288,6 @@ function saveOptions(event, settings) {
if (chrome.runtime.lastError) {
updateStatus('Settings not saved! Please try again.', true, 5000);
} else {
updateStatus('Settings saved successfully!', false, 3000);
populateOptions();
}
});
Expand Down Expand Up @@ -336,7 +338,7 @@ function wordAdd(event) {
var word = document.getElementById('wordText').value;
if (word != "") {
if (!arrayContains(Object.keys(config.words), word)) {
config.words[word] = {"matchMethod": 1, "words": []};
config.words[word] = {"matchMethod": 0, "words": []};
saveOptions(event, config);
document.getElementById('wordText').value = "";
} else {
Expand Down Expand Up @@ -379,6 +381,7 @@ window.addEventListener('load', populateOptions);
document.getElementById('filterMethodSelect').addEventListener('change', filterMethodSelect);
// Filter - Censor
document.getElementById('preserveFirst').addEventListener('click', saveOptions);
document.getElementById('preserveLast').addEventListener('click', saveOptions);
document.getElementById('censorCharacterSelect').addEventListener('change', censorCharacter);
document.getElementById('censorFixedLengthSelect').addEventListener('change', censorFixedLength);
// Filter - Substitute
Expand Down

0 comments on commit e398cff

Please sign in to comment.