any way to run js in Chrome?

115 views
Skip to first unread message

angu...@gmail.com

unread,
Apr 27, 2018, 9:48:58 AM4/27/18
to Hammerspoon
I noticed that Keyboard Maestro is able to run js code in Chrome and put the result back to clipboard. How does it manage to do that? Is Hammerspoon able to do that?

Oliver Schrenk

unread,
Apr 29, 2018, 7:28:45 AM4/29/18
to Hammerspoon
I didn't realize I only send this to OP. So here was my take on it

If you are on macOS you can use AppleScript bridge to Chrome to execute some Javascript. 

Take a look at my ever favourite Vim Plugin and how they solved it:

https://github.com/thalesmello/webcomplete.vim/blob/master/sh/get-words

I haven’t tried it but you should be able to go from Hammerspoon via AppleScript to do some stuff (not sure what you are trying to achieve) with Chrome.

Haider Rizvi

unread,
May 4, 2018, 3:08:04 PM5/4/18
to Oliver Schrenk, Hammerspoon
Oliver Schrenk <oliver....@gmail.com> writes:

> I didn't realize I only send this to OP. So here was my take on it
>
> If you are on macOS you can use AppleScript bridge to Chrome to execute some Javascript.
>
> Take a look at my ever favourite Vim Plugin and how they solved it:
>
> https://github.com/thalesmello/webcomplete.vim/blob/master/sh/get-words

You can also look at chrome-cli as an option:
https://github.com/prasmussen/chrome-cli

Regards,
--
Haider

angu...@gmail.com

unread,
May 5, 2018, 11:29:43 PM5/5/18
to Hammerspoon
Thanks, that's a friendly interface for people like me not familiar with Applescript. Since what I want to achieve is just get something from a web, I've take Oliver's path. 
           _,result = hs.osascript.applescript([[
             tell application
"Google Chrome"
               
set theResult to execute front window's active tab javascript "document.getElementById('current-learning-word'). innerHTML"
               if theResult is not missing value then
                 theResult
               end if
             end tell
             ]])

Chris Jones

unread,
Jul 12, 2018, 4:16:22 AM7/12/18
to angu...@gmail.com, Hammerspoon
Hey

It might be fun exercise for someone to make a Spoon that takes care of the AppleScript part of that for you, and just accepts the javascript you want to be executed :)

Cheers,
Chris

--
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon+unsubscribe@googlegroups.com.
To post to this group, send email to hamme...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/hammerspoon/96239022-d02f-4e0d-a6c1-0b7c5bb597e0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Chris
Reply all
Reply to author
Forward
0 new messages