How To Check For Banned Word On Page With Javascript ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bits2017
    New Member
    • Jun 2017
    • 39

    How To Check For Banned Word On Page With Javascript ?

    Folks,

    I'm trying to add a content filter (banned words filter) onto a web proxy. When the Javascript detects any of the banned words on the page, it should:

    * As soon as a banned word is detected, it should not check for any more but immediately echo/print on screen/display a warning to the user with the first banned word spotted:
    "Banned word "blah" found on page. You will be redirected to Google".
    * Then, redirect to Google.
    * Dump to database the banned word found, and the url of the page the banned word was found on. Eg.

    Banned Word Found|Url|Date & Time of the Server
    --------------------------------------------------------
    ass|donkey-ass.com|25-01-2007, 03:00:00



    The only requirement is that, I should be able to feed a list of banned words it should check for.

    What would the code be to do all that ?
    I'd like to code see samples.
    I'm trying to build one in php but the php gurus say it is best the banned words checking is done on client-side. Else, the page would take too long to load (high cpu usage) if the checking is done on server side. I have no experience in writing Javascript code.


    Thank You!
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    For live checking of words as they're being typed yes javascript might be the best approach.

    You should use a library like jquery to consume the input and run it through a filter and set the alert when it's found.

    Take a look at the following github rep
    filter out profane words on the client. Contribute to ChaseFlorell/jQuery.ProfanityFilter development by creating an account on GitHub.
    niheel @ bytes

    Comment

    • bits2017
      New Member
      • Jun 2017
      • 39

      #3
      Folks!

      Why do you reckon this code does not work ? I only see a blank page.
      The code will need to run everytime cURL loads a page so that the JS can check the page for banned words.

      Code:
      <?php
      /*
      ERROR HANDLING
      */
      // 1). Set banned words.
      $banned_words = array("Prick","Dick", "blow", "cock");
      // 2). $curl is going to be data type curl resource.
      $curl = curl_init();
      // 3). Set cURL options.
      curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
      // 4). Run cURL (execute http request).
      $result = curl_exec($curl);
      if (curl_errno($curl)) {
          echo 'Error:' . curl_error($curl);
      }
      $response = curl_getinfo( $curl );
      if($response['http_code'] == '200' )
      {
      ?>
      <script>
      $(function(){
      		var pageData = "hello <a>example can have tags! swear word is </a><div> one..</div>";
         if(checkSwear(pageData)== false){
         //redirect to google
         }
      });
      
      function checkSwear(sentance) {
      	
      	var swear_words_arr=new Array("blow", "nut", "asshole");
        var regex = new RegExp('\\b(' + swear_words_arr.join('|') + ')\\b', 'i' );
        if(regex.test(sentance)) {
          alert("Please refrain from using offensive words"); /* + alert_text */
          return false;
        } else {
        //alert(1)
        	return true;
        }
      }
      </script>
      <?php
      }
      curl_close($curl);
      ?>

      Comment

      Working...