Skip to content

Commit

Permalink
Support for filtering on keywords and phrases
Browse files Browse the repository at this point in the history
  • Loading branch information
cfinke committed May 13, 2011
1 parent 3739c5d commit 142d536
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 34 deletions.
11 changes: 11 additions & 0 deletions chrome/_locales/en_US/messages.json
Expand Up @@ -76,6 +76,10 @@
"message": "Profanity",
"description": "A reason shown to the user when hiding a comment."
},
"reason_keywords": {
"message" : "Contains a banned phrase or keyword",
"description": "A reason shown to the user when hiding a comment."
},
"reason_spelling": {
"message" : "Too many spelling mistakes",
"description": "A reason shown to the user when hiding a comment."
Expand All @@ -91,5 +95,12 @@
},
"install_rule": {
"message": "Install a Comment Snob Rule from this page"
},
"label_hide_keywords": {
"message": "Contains one of these keywords or phrases:"
},
"text_keywords_instructions": {
"message": "Separate keywords with spaces. To include a phrase, surround it with double quotes. Regular expressions are allowed."
}

}
4 changes: 2 additions & 2 deletions chrome/comment-snob.js
Expand Up @@ -25,8 +25,8 @@ var COMMENT_SNOB = {
"excessiveCapitals" : COMMENT_SNOB.prefs.getBoolPref("excessiveCapitals"),
"profanity" : COMMENT_SNOB.prefs.getBoolPref("profanity"),
"extreme" : COMMENT_SNOB.prefs.getBoolPref("extreme"),
"mistakes" : COMMENT_SNOB.prefs.getIntPref("mistakes")

"mistakes" : COMMENT_SNOB.prefs.getIntPref("mistakes"),
"keywords" : COMMENT_SNOB.prefs.getCharPref("keywords")
};
},

Expand Down
107 changes: 84 additions & 23 deletions chrome/content.js
Expand Up @@ -62,6 +62,8 @@ function filterComments(isRefilter) {
allComments.remove();
}
else {
var parsedKeywords = null;

allComments.find(theRule.commentContainerSelector).each(function (idx) {
var reason = false;
var $this = $(this);
Expand Down Expand Up @@ -107,38 +109,97 @@ function filterComments(isRefilter) {
else if (prefs.profanity && originalText.match(/\b(ass(hole)?\b|bitch|cunt|damn|(mother)?fuc[kc]|(bull)?shits?\b|fag|nigger|nigga)/i)) {
reason = chrome.i18n.getMessage("reason_too_much_profanity");
}
else if (prefs.mistakes) {
var now = new Date();
else {
if (prefs.keywords) {
if (!parsedKeywords) {
var filter = prefs.keywords;

var filterString = filter.replace(/\s+/g, " ").replace(/^\s+|\s+$/g, "");
var filterParts = [];

// We now have a space delimited filter string, but it may included quoted phrases
var currentFilter = "";
var inQuotes = 0;

for (var i = 0; i < filterString.length; i++) {
var theChar = filterString.charAt(i);

if (theChar == "'" || theChar == '"') {
if (inQuotes == theChar) {
inQuotes = false;
}
else if (currentFilter.length == 0) {
inQuotes = theChar;
}
else {
currentFilter += theChar;
}
}
else {
if (theChar == " "){
if (!inQuotes) {
filterParts.push(currentFilter);
currentFilter = "";
continue;
}
}

currentFilter += filterString.charAt(i);
}
}

if (currentFilter != "") filterParts.push(currentFilter);

var parsedKeywords = [];

for (var i = 0; i < filterParts.length; i++) {
if (filterParts[i]) {
parsedKeywords.push(new RegExp(filterParts[i], "i"));
}
}
}

for (var i = 0, _len = parsedKeywords.length; i < _len; i++) {
if (originalText.match(parsedKeywords[i])) {
reason = chrome.i18n.getMessage("reason_keywords");
break;
}
}
}

if (!reason && prefs.mistakes) {
var now = new Date();

var mistakes = 0;
var text = originalText;
var mistakes = 0;
var text = originalText;

text = text.replace(/\s/mg, " ");
text = text.replace(/\s+|[^a-z0-9\-']/img, " "); //'
text = $.trim(text);
text = text.replace(/\s/mg, " ");
text = text.replace(/\s+|[^a-z0-9\-']/img, " "); //'
text = $.trim(text);

words = text.split(" ");
words = text.split(" ");

for (var j = 0, _jlen = words.length; j < _jlen; j++){
var word = words[j];
for (var j = 0, _jlen = words.length; j < _jlen; j++){
var word = words[j];

if (!dictionary.check(word)){
if (
(word[0] === word[0].toUpperCase()) &&
(word.substring(1) === word.substring(1).toLowerCase())
) {
// Probably a name. We'll let it slide.
}
else {
mistakes++;
if (!dictionary.check(word)){
if (
(word[0] === word[0].toUpperCase()) &&
(word.substring(1) === word.substring(1).toLowerCase())
) {
// Probably a name. We'll let it slide.
}
else {
mistakes++;

if (mistakes >= prefs.mistakes) break;
if (mistakes >= prefs.mistakes) break;
}
}
}
}

if (mistakes >= prefs.mistakes || mistakes == words.length) {
reason = chrome.i18n.getMessage("reason_spelling");
if (mistakes >= prefs.mistakes || mistakes == words.length) {
reason = chrome.i18n.getMessage("reason_spelling");
}
}
}

Expand Down
31 changes: 22 additions & 9 deletions chrome/options.html
Expand Up @@ -17,7 +17,7 @@
ruleList.find("li").click();

var rules = COMMENT_SNOB.prefs.getJSONPref("rules", {});

for (var i in rules) {
var option = $("<li/>");
option.addClass("navbar-item");
Expand Down Expand Up @@ -48,14 +48,18 @@
else {
var rulePrefs = COMMENT_SNOB.defaultPrefs;
}

$(".preference-bool").each(function () {
this.checked = rulePrefs[$(this).attr("preference")];
});

$(".preference-int").each(function () {
$(this).val(parseInt(rulePrefs[$(this).attr("preference")], 10));
});

$(".preference-text").each(function () {
$(this).val(rulePrefs[$(this).attr("preference")]);
});

$("#custom-preferences").show();

Expand Down Expand Up @@ -102,6 +106,10 @@
$(".preference-int").each(function () {
COMMENT_SNOB.prefs.setIntPref($(this).attr("preference"), $(this).val());
});

$(".preference-text").each(function () {
COMMENT_SNOB.prefs.setCharPref($(this).attr("preference"), $(this).val());
});
}
else {
var prefObject = {};
Expand All @@ -114,6 +122,10 @@
prefObject[$(this).attr("preference")] = $(this).val();
});

$(".preference-text").each(function () {
prefObject[$(this).attr("preference")] = $(this).val();
});

var prefs = COMMENT_SNOB.prefs.getJSONPref("rulePrefs", {});
prefs[ruleId] = prefObject;

Expand Down Expand Up @@ -248,13 +260,14 @@ <h3>Filtering Preferences</h3>
<div id="custom-preferences">
<p><input type="checkbox" preference="extreme" class="preference-bool preference" id="extreme" /> <label class="i18n" data-key="label_hide_all" /></p>
<div>
<p style="border-top: 1px solid #eee; padding-top: 15px;"><input type="number" size="2" min="0" preference="mistakes" class="preference-int preference" id="" /> <label class="i18n" data-key="label_hide_spelling" /></p>
<p><input type="checkbox" preference="nocaps" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_no_capital" /></p>
<p><input type="checkbox" preference="allcaps" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_only_capital" /></p>
<p><input type="checkbox" preference="startsWithCapital" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_start_lower" /></p>
<p><input type="checkbox" preference="punctuation" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_punctuation" /></p>
<p><input type="checkbox" preference="excessiveCapitals" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_capitalization" /></p>
<p><input type="checkbox" preference="profanity" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_profanity" /></p>
<p style="border-top: 1px solid #eee; padding-top: 15px;"><input type="number" size="2" min="0" preference="mistakes" class="preference-int preference" id="" /> <label class="i18n" data-key="label_hide_spelling"></label></p>
<p><input type="checkbox" preference="nocaps" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_no_capital"></label></p>
<p><input type="checkbox" preference="allcaps" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_only_capital"></label></p>
<p><input type="checkbox" preference="startsWithCapital" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_start_lower"></label></p>
<p><input type="checkbox" preference="punctuation" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_punctuation"></label></p>
<p><input type="checkbox" preference="excessiveCapitals" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_capitalization"></label></p>
<p><input type="checkbox" preference="profanity" class="preference-bool preference" /> <label class="i18n" data-key="label_hide_too_much_profanity"></label></p>
<p><label class="i18n" data-key="label_hide_keywords"></label> <input type="text" preference="keywords" class="preference-text preference" size="40"/><br /><br /><small class="i18n" data-key="text_keywords_instructions"></small></p>
</div>
</div>
</section>
Expand Down

0 comments on commit 142d536

Please sign in to comment.