So far I haven't got it to work. If I hard-code a literal document.queryselector(...) in the text script in the action it works.
The test herewith, although not a subroutine, illustrates what doesn't work. Note that the second action returns the string, document.querySelector(..., not the result of executing the query. This is understandable. But I wonder if there's a way to do this.
I read about Function and eval(); the latter seems simpler and in my case there are no security nor performance concerns -- I'm running this code only within KM on my local Mac, not on a server. So I used eval(document.kmvar.Local_jsPath).click(); and it works!