From 142d5362fc81b7c8ea036accce92c0074163776e Mon Sep 17 00:00:00 2001 From: Christopher Finke Date: Fri, 13 May 2011 02:18:30 +0000 Subject: [PATCH] Support for filtering on keywords and phrases --- chrome/_locales/en_US/messages.json | 11 +++ chrome/comment-snob.js | 4 +- chrome/content.js | 107 ++++++++++++++++++++++------ chrome/options.html | 31 +++++--- 4 files changed, 119 insertions(+), 34 deletions(-) diff --git a/chrome/_locales/en_US/messages.json b/chrome/_locales/en_US/messages.json index 912748a..8eecb18 100644 --- a/chrome/_locales/en_US/messages.json +++ b/chrome/_locales/en_US/messages.json @@ -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." @@ -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." } + } \ No newline at end of file diff --git a/chrome/comment-snob.js b/chrome/comment-snob.js index 90878be..024f1e8 100644 --- a/chrome/comment-snob.js +++ b/chrome/comment-snob.js @@ -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") }; }, diff --git a/chrome/content.js b/chrome/content.js index 1b1e177..ec7bcb5 100644 --- a/chrome/content.js +++ b/chrome/content.js @@ -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); @@ -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"); + } } } diff --git a/chrome/options.html b/chrome/options.html index dac52ea..b5ec78e 100644 --- a/chrome/options.html +++ b/chrome/options.html @@ -17,7 +17,7 @@ ruleList.find("li").click(); var rules = COMMENT_SNOB.prefs.getJSONPref("rules", {}); - + for (var i in rules) { var option = $("
  • "); option.addClass("navbar-item"); @@ -48,7 +48,7 @@ else { var rulePrefs = COMMENT_SNOB.defaultPrefs; } - + $(".preference-bool").each(function () { this.checked = rulePrefs[$(this).attr("preference")]; }); @@ -56,6 +56,10 @@ $(".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(); @@ -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 = {}; @@ -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; @@ -248,13 +260,14 @@

    Filtering Preferences

    -

    -

    -

    -

    -

    -

    -

    +

    +

    +

    +

    +

    +

    +

    +