BitBucket have released their Cursores.js library that does this, it's small and focused which is nice - http://cursores.bitbucket.org/
The only issue I have is that it doesn't pick up the token if there is no text to the left of the caret, for example "t|est" would work but "|test" wouldn't.