Feb 4, 2011

Obfuscated Hello World CodeGolf

 

Finally the codegolf Stackexchange site is in beta. So I decided to give it a try, as I found this nice codegolf:

golf

So…. Behold my solution with JavaScript:

javascript:(_=(_=[][(f=!!(_='')+_)[3]+(b=({}+_)[1])+($=(c=(d=!_+_)[1])+d[0])])())[f[1]+'l'+(a=d[3])+$]("H"+a+'ll'+b+' W'+b+c+'ld');

Feel free to try it out. Just paste it in your browser's address bar and let it do the magic.


Well if you would like to know how it works I suggest you read this blog post first (I've actually shared it in my Google Reader feed a month back, so you might have already seen it). The script uses the same principle to acquire the sort() function (and consequently the window object and alert() function) as described there. I also found this nice video that was very helpful.


Good fight, good night :)

Feb 3, 2011

jQuery.validation breaks jQuery 1.5 ajax API

Today I found out (the hard way) that the use of the jQuery.validation plugin breaks the jQuery.ajax API. Since the Asp.net MVC 3 uses jQuery.validation for its unobtrusive validation it is included in the default project template. You can imagine my surprise when I updated jQuery to v1.5, and my heavy ajaxified MVC page stopped working correctly. Every ajax request contained an parseerror with an error message stating that the callback jQuery[some random string] was not called. I tried to fiddle with the ajax settings for JSONP (namely jsonp and jsonpCallback), but no matter how I set them, the error was still present.

error_example
Error message

So I started digging around, trying to replicate the problem. When I ran simple html pages $.ajax worked as expected. Then I started to gradually test all the jQuery plugins and found out that the plugin causing the error was jQuery.validation. So here is my reproduction and a simple workaround (the whole project is available here):

The controller:

using System.Linq;
using System.Web.Mvc;

public partial class DefaultController : Controller
{
  private static readonly int[] numbers = Enumerable.Range(1, 10).ToArray();

  public virtual ActionResult Index()
  {
    return View();
  }

  [HttpPost]
  [ValidateAntiForgeryToken]
  public virtual JsonResult SimpleArray(int id)
  {
    return Json(numbers.Concat(new [] { id }));
  }
}

The view:

@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
  <script src="/Scripts/jquery-1.5.js" type="text/javascript"></script>
  <script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
</head>
  <body>
  <script type="text/javascript">
    $(function () {
      $('a').click(function (e) {
        var req = $.ajax({
            url: '@Url.Action(MVC.Default.SimpleArray())',
            type: 'POST',
            data: $('form').serializeArray(),
            dataType: 'json'
        });
        req.success(function (response, status, xhr) { alert('Success: ' + response); });
        req.error(function (xhr, error, msg) { alert('Error "' + error + '": ' + msg); });
      });
    });
  </script>
  <a href="javascript:void(0);">Do request</a>
  <form action="#">
    <input type="hidden" value="-1" name="id" />
    @Html.AntiForgeryToken()
  </form>
</body>
</html>

The workaround:

$(function () {
$.ajaxSettings.cache = false;
$.ajaxSettings.jsonp = undefined;
$.ajaxSettings.jsonpCallback = undefined;
})


The cause of the problem is this line of JavaScript in jQuery.validate.js, that overrides the settings you pass into the $.ajax call with all the default ones (and jQuery.ajaxSettings defaults to { jsonp: "callback", jsonpCallback: function() {...}}):


// create settings for compatibility with ajaxSetup 
settings = $.extend(settings, $.extend({}, $.ajaxSettings, settings));

Cheers!

Dec 14, 2010

inJsect – a JavaScript injection utility

Today I was fooling around on how to launch external JavaScript files in web pages. As a result a small utility called inJsect was created. Feel free to fork the code on jsfiddle (BTW great platform).

The final peace of the puzzle is a link that you can bookmark so you can run the tool from the toolbar of your browser (bookmarklet).

inJsect (drag&drop to your toolbar)

I host the code on m0sa.net so everyone can access it. You can find the expanded version of the bookmarklet code here.

inJsect can:

  • inject external JavaScript files into the remote page DOM
  • remove a script tag prior to the injection
  • execute addition JavaScript code after the external code is successfully injected

A pictures is worth a thousand words:

inJsect
Before

inJsect_after

After