Javascript masterpiece



  • Here is the full source code of a portlet I found on a customer's intranet. It's not a very efficient way to block porn (or youtube), but I am moved by the "who needs if" design. 

    <script language="javascript">
    function search() { eval(new Array("document.forms[0].submit();","alert('Keyword is invalid');")[Math.abs(filter(document.forms[0].q.value)=="")]); }

    function filter(query){
    banned=new Array("porn","gambling","dating","youtube"); //add banned keywords here
    for(i=0;i<banned.length;i++){query=query.replace(new RegExp(banned[i], "i"),"");};
    //could send email to HR here if query != q.value
    return query;}
    </script>

    <b>Google search</b>:
    <form action="http://www.google.ca/search" target="_blank">
    <input type="text" name="q">
    <input type="button" value="go" onclick="search()">
    </form>



  • This is... so ugly it's beautiful, and beautiful it hurts. This shouldn't be a sidebar, this should be on the main page. We need a way to "digg" things up there.



  • @president_ch0ice said:

    Here is the full source code of a portlet I found on a customer's intranet. It's not a very efficient way to block porn (or youtube), but I am moved by the "who needs if" design.


    Obviously, they DO need 'if' -- they [code]//could send email to HR here if query != q.value[/code]





  • cool! so that's how the ternary operator is implemented... always wanted to know...



  • @PSWorx said:

    cool! so that's how the ternary operator is implemented... always wanted to know...

    I have to admit I once actually DID this in Python...

    [function1, function2][condition]()

    as a shorthand. But it's far more common in Perl to emulate the missing "switch" statement:

    my $func = {
      key1 => sub { CASE1... },
      key2 => sub { CASE2... },
      key3 => sub { CASE3... },
    }->{$key};
    return DEFAULTCASE... unless $func;
    return $func->();


  • Erhm.. There's no ternary operator in that script ? The ternary operator is specifically "?" (e.g. expression?trueAction():falseAction()). Maybe I just missed your point..

    Besides, the script doesn't work at all unless the banned word was the only input; if a user searches for "porn dating" the filter method would return " " which would be compared to "" (false) and then the original contents of the form "porn dating" would be submitted. So all in all this is utter crap.



  • @nixen said:

    Erhm.. There's no ternary operator in that script ? The ternary operator is specifically "?".

    Actually there is specifically a colon involved.

    @nixen said:
    Maybe I just missed your point..

    Maybe...

    @nixen said:
    So all in all this is utter crap.

    While some may come here to find the WorldToughestFuturistic code samples for their projects, i hope most are here for exactly that "utter crap".



  • @Radiation Dude said:

    @nixen said:
    Erhm.. There's no ternary operator in that script ? The ternary operator is specifically "?".

    Actually there is specifically a colon involved.

    Well, yes there are two colons: One in the url and one in html. None in javascript though.

    Still, the ternary operator is not a colon, it is generally refered to mean a question mark, or rather (if you want to be really precise) a question mark and a colon. None of that here. A colon in javascript exists either as part of an object declaration or as part of a ternary operator.


    @nixen said:

    Maybe I just missed your point..

    Maybe...

    And maybe that's what I'm doing again....


    @nixen said:

    So all in all this is utter crap.

    While some may come here to find the WorldToughestFuturistic code samples for their projects, i hope most are here for exactly that "utter crap".

    Yes, of course, you're right. However, this piece of code is both coded with poor style AND doesn't work as intended.



  • @nixen said:

    Erhm.. There's no ternary operator in that script ? The ternary operator is specifically "?" (e.g. expression?trueAction():falseAction()). Maybe I just missed your point..


    He was obviously referring to (expanded for readability)

    eval(
        new Array(
            "document.forms[0].submit();",
            "alert('Keyword is invalid');"
          )[Math.abs(filter(document.forms[0].q.value)=="")]
    )
    

    -- a stupidly fun implementation of

    (filter(document.forms[0].q.value)=="") ? alert('Keyword is invalid') : document.forms[0].submit();


  • "There once was a master programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the master to evaluate his progress, the master criticized him for writing unstructured programs, saying, ``What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure.''

    -The tao of programming

     



     


Log in to reply