Activate the frontmost Safari and run Javascript
Your script is trying to tell s.th. to a Safari application allegedly already "frontmost". Is it?!?
You can only identify or activate an unknown element by property ("whose ... is ...")
This will do: the first application whose ...

If you WANT "Safari" to become "frontmost" you'd  better write:

tell application "Safari"
    activate
    do JavaScript theScript in current tab of first window
end tell