emacs-eaf / eaf-browser

A modern, customizable and extensible browser in Emacs
GNU General Public License v3.0
129 stars 26 forks source link

How would one execute an arbitrary JavaScript string when pressing a key? #27

Closed Dima-369 closed 2 years ago

Dima-369 commented 2 years ago

What I have in mind is to run certain JavaScript in the browser on a key press.

I see that there is (eaf-py-proxy-eval_js) but it always prompts in the minibuffer, which is no option for me on a long script.

(eaf-py-proxy-eval_js_file) would work but I don't want the overhead associated with reading from a file.


So what I have in mind is something like: (eaf-py-proxy-eval-js-string "alert('foo')").

In buffer.py I see self.buffer_widget.eval_js(...) but I don't see a way to directly access in Emacs.

Is this possible?

manateelazycat commented 2 years ago

I have push new commit https://github.com/emacs-eaf/emacs-application-framework/commit/c3aa71be04a7844f789102d9f3106edee679200e

After update to newest version of EAF.

You can try:

(eaf-call-async "eval_js_code" eaf--buffer-id "alert('hello')")

or

(eaf-call-sync "execute_js_code" eaf--buffer-id "1 + 1;")

for your purpose.

Dima-369 commented 2 years ago

Perfect, works well. Thank you!

Dima-369 commented 2 years ago

One thing I noticed here is that the function doc is incorrect because a result is returned.

image


And a follow-up question to the code above. It seems that the code would allow to pass the evaluated result back into Emacs Lisp but the code below (regardless of the string) always returns nil for me.

Does it work for you?

(eaf-call-sync "execute_js_code" eaf--buffer-id "1 + 1;")
manateelazycat commented 2 years ago

One thing I noticed here is that the function doc is incorrect because a result is returned.

image

And a follow-up question to the code above. It seems that the code would allow to pass the evaluated result back into Emacs Lisp but the code below (regardless of the string) always returns nil for me.

Does it work for you?

(eaf-call-sync "execute_js_code" eaf--buffer-id "1 + 1;")

Sorry, typo, this issue fixed by patch https://github.com/emacs-eaf/emacs-application-framework/commit/62c54778138c67bb5ef599f1bf7c094f5c0ad812

Please upgrade again.

Dima-369 commented 2 years ago

Works now!