Day 35 – Find And Replace Words In Twitter Tweet Stream

I recently built a Kynetx app to remove foul language from my Twitter tweet stream and thought it was interesting enough to share. The code is mostly JavaScript but there are a few concepts that are really good to know when building a Kynetx app that uses a lot of JavaScript. You can find the original app on my blog over at

ruleset a60x551 {
  meta {
    name "Foul Fowl Control"
    description <<
      hides foul language in twitter stream
    author "Mike Grace"
    logging on

  dispatch {
    domain ""

  rule find_dem_foul_fowls {
    select when pageview ".*"
      emit <|
        // add repeat function to String object
        String.prototype.repeat = function(times) {
           return (new Array(times + 1)).join(this);

        // list of words to find and replace
        // var foulWords = ["it","is","the","on","a","i","of","my","me","for","in","up","and","to","be","that"];

        // build regex to find and replace foul words
        KOBJ.a60x550.theReplacer = new RegExp('\\b('+foulWords.join('|')+')\\b','gi');

        // attatch function to global KOBJ object so it's callable outside of closure
        KOBJ.a60x550.findFoulFowls = function() {

          // process tweets not marked as processed
          $(".stream-item:not(.defouled)").each(function(index, element) {

            // mark as processed and cache object
            var $current = $K(element).addClass("defouled");

            // get text of tweet
            var $tweet = $current.find(".tweet-text");
            var tweet = $tweet.text();

            // clean it up!
            var needsCleaning = false;
            var cleanTweet = tweet.replace(KOBJ.a60x550.theReplacer, function(match) {
              needsCleaning = true;
              return "*".repeat(match.length);

            // only write back to the DOM if the tweet was changed because DOM access is expen$ive!
            if (needsCleaning) {

          }); // end of each

          setTimeout(function() {
          }, 1000);


The code is fairly well commented so I’ll just point out one thing. Because I am using a setTimout function to recursively call the function that looks for new tweets, I needed to attach the function to the global KOBJ object. This is because the emitted JavaScript gets executed inside a closure and won’t be accessible from outside the closure.

This entry was posted in Kynetx. Bookmark the permalink.

One Response to Day 35 – Find And Replace Words In Twitter Tweet Stream

  1. Pingback: Foul Fowl Control Kynetx App For – Michael Grace

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s