You're more or less on the right track. The main problem I see here is that the 'search' is not actually a string. It implements toString() so it looks like a string, and it's certainly easy to think of it as a string. And certainly all of the other context key arguments are strings, so i can see how you'd think that.
However it's an object. More specifically it's an instance of Splunk.Search.
So when you do context.set("search", search) , and you actually set it to a string value, things downstream will derail badly. Probably there are a bunch of javascript errors in the console.
There is actually a little documentation around this stuff now. http://dev.splunk.com/view/SP-CAAADQV . What you want to look at specifically is the setBaseSearch() method.
And if you're going down this road of using NullModule to inject custom javascript, you probably want to at least take a look at Sideview Utils. that app among other things gives you a CustomBehavior module, and all of the Sideview modules themselves take customBehavior params, and this makes it a little more flexible and a little less brittle to wire up your app with custom behaviors.
http://splunk-base.splunk.com/apps/22279/sideview-utils
UPDATE:
1) You should delete your entire 'getUserName' function and replace it with this:
Splunk.util.getConfigValue("USERNAME")
2) Since this answer Sideview Utils 2.0 is out, and although it's no longer free, it has a lot of new features and modules that the old version does not. Email me with any question or for pricing details. http://sideviewapps.com/apps/sideview-utils/
... View more