Safari Javascript works in Safari console, doesn't work when run from KM?

The problem is in the way in which you are trying to fetch a value from a KM variable.

See, for example: Execute Javascript in Safari using KM variable (SOLVED)