[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/firefox-javascript-repl b0695f591d 02/24: Complete acto
From: |
Thomas Fitzsimmons |
Subject: |
[elpa] externals/firefox-javascript-repl b0695f591d 02/24: Complete actor retrieval |
Date: |
Thu, 1 Jun 2023 00:41:51 -0400 (EDT) |
branch: externals/firefox-javascript-repl
commit b0695f591d142d5779588c6588bfd1c5340e1713
Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
Commit: Thomas Fitzsimmons <fitzsim@fitzsim.org>
Complete actor retrieval
* firefox-javascript-repl.el (firefox-javascript-repl--message)
(firefox-javascript-repl--error)
(firefox-javascript-repl--accept-input)
(firefox-javascript-repl--send-string)
(firefox-javascript-repl--get-first-tab-actor): New functions.
(firefox-javascript-repl--create-profile-directory): Kill buffer
after writing out its contents.
(firefox-javascript-repl): Distinguish standard output and network
process names and buffer names. Remove a debug message. Kill
network and process buffers when Firefox terminates. Check for a
network connection to Firefox. Find the actor in the first tab.
---
firefox-javascript-repl.el | 78 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 72 insertions(+), 6 deletions(-)
diff --git a/firefox-javascript-repl.el b/firefox-javascript-repl.el
index 2e1311bcf4..bab00de84d 100644
--- a/firefox-javascript-repl.el
+++ b/firefox-javascript-repl.el
@@ -36,6 +36,15 @@
:group 'external
:type 'string)
+(defun firefox-javascript-repl--message (format &rest arguments)
+ "Print to *Messages* a package herald and FORMAT.
+ARGUMENTS will be used for FORMAT, like `messages'."
+ (apply #'message (concat "firefox-javascript-repl: " format) arguments))
+
+(defun firefox-javascript-repl--error (message)
+ "Throw an error with a package herald and MESSAGE."
+ (error "firefox-javascript-repl: %s" message))
+
(defun firefox-javascript-repl--create-profile-directory ()
"Create a profile directory."
(let ((profile-directory (make-temp-file "firefox-javascript-repl-" t)))
@@ -44,9 +53,42 @@
(insert "user_pref(\"devtools.debugger.remote-enabled\", true);\n")
(insert "user_pref(\"devtools.chrome.enabled\", true);\n")
(insert "user_pref(\"devtools.debugger.prompt-connection\", false);\n")
- (save-buffer 0))
+ (save-buffer 0)
+ (kill-buffer))
profile-directory))
+(defun firefox-javascript-repl--get-first-tab-actor (network-buffer)
+ "Get the actor of the first browser tab.
+NETWORK-BUFFER is where Firefox responses go."
+ (gethash
+ "actor"
+ (elt
+ (gethash
+ "tabs"
+ (with-current-buffer network-buffer
+ (firefox-javascript-repl--message "BUF: %S" (buffer-string))
+ (goto-char (point-max))
+ (unless (bolp)
+ (insert "\n"))
+ (search-backward "{\"tabs\":")
+ (let ((start (point)))
+ (forward-sexp)
+ (prog1
+ (json-parse-string (buffer-substring start (point)))
+ (goto-char (point-max))))))
+ 0)))
+
+(defun firefox-javascript-repl--accept-input (network)
+ "Wait up to ten sections to see if packets are received from NETWORK."
+ (unless (accept-process-output network 10 0 t)
+ (firefox-javascript-repl--error
+ "Failed to receive network packets from Firefox")))
+
+(defun firefox-javascript-repl--send-string (network message)
+ "Send to NETWORK a MESSAGE then wait for the response."
+ (process-send-string network message)
+ (firefox-javascript-repl--accept-input network))
+
(defun firefox-javascript-repl ()
"Run a new instance of Firefox in a new profile.
Set about:config values `devtools.debugger.remote-enabled' to
@@ -58,23 +100,47 @@ localhost (127.0.0.1) TCP port 6000."
(when (not (process-status "firefox-javascript-repl"))
(let* ((profile-directory
(firefox-javascript-repl--create-profile-directory))
- (process-name (file-name-nondirectory profile-directory))
+ (temporary-name (file-name-nondirectory profile-directory))
+ (process-name (concat "out-" temporary-name))
+ (process-buffer-name (concat "*" process-name "*"))
+ (network-name (concat "net-" temporary-name))
+ (network-buffer-name (concat "*" network-name "*"))
(firefox-process
(start-process "firefox-javascript-repl"
- process-name
+ process-buffer-name
firefox-javascript-repl-binary
"about:blank"
"-profile" profile-directory
"-start-debugger-server")))
(set-process-sentinel firefox-process
(lambda (process event)
- (message "%S %S" process event)
(when (or (string= event "killed\n")
(string= event "finished\n"))
- (message
- "firefox-javascript-repl: %s %s; deleting %s"
+ (firefox-javascript-repl--message
+ "%s %s; deleting %s"
process (string-trim event) profile-directory)
+ (ignore-errors (kill-buffer
network-buffer-name))
+ (kill-buffer process-buffer-name)
(delete-directory profile-directory t))))
+ (let (network)
+ (catch 'connected
+ (dotimes (_count 50)
+ (when
+ (setq network
+ (ignore-errors
+ (open-network-stream process-name network-buffer-name
+ "127.0.0.1" 6000)))
+ (throw 'connected network))
+ (sit-for 2))
+ (firefox-javascript-repl--error
+ "Failed to connect to Firefox network port"))
+ (firefox-javascript-repl--accept-input network)
+ (firefox-javascript-repl--message "Established Firefox connection")
+ (firefox-javascript-repl--send-string
+ network "31:{\"to\":\"root\",\"type\":\"listTabs\"}")
+ (let ((actor (firefox-javascript-repl--get-first-tab-actor
+ network-buffer-name)))
+ (message "Actor: %s" actor)))
nil)))
(defun firefox-javascript-repl-stop ()
- [elpa] externals/firefox-javascript-repl 7c8f747bba 10/24: Add a JavaScript example with which to test, (continued)
- [elpa] externals/firefox-javascript-repl 7c8f747bba 10/24: Add a JavaScript example with which to test, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 76fe78566b 11/24: Polish process management, debugging and naming, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 075bd87281 15/24: Adjust commentary wording, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 2016b0eafa 22/24: Make firefox-javascript-repl--mode private, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl c2a0f7f44a 04/24: Get basic REPL working, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl f0ed5fbf81 03/24: Get ready for asynchronous JavaScript evaluation, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 77c9907e3e 18/24: Move usage to start of commentary, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 1e0638d411 17/24: Mention throwaway profile in commentary, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 7b288e2dba 24/24: Bump version to 0.9.0, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 7bed24ae8e 23/24: Ignore errors when showing quirk, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl b0695f591d 02/24: Complete actor retrieval,
Thomas Fitzsimmons <=
- [elpa] externals/firefox-javascript-repl 3d5c11b468 05/24: Add .gitignore file, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 563ae0984c 01/24: firefox-javascript-repl: New package, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 9644f919dd 06/24: Fix string quoting, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 5c47a29fd8 09/24: Remove newlines before sending JavaScript, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl b4186a5762 12/24: Backport to Emacs 26.1, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 876dcb4615 13/24: Add Package-Requires, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl aaefd1f8f4 14/24: Add jss URL to commentary, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl c35e324539 16/24: Refer to Firefox Debugging Remote Protocol, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 76ba08cb86 19/24: Trim some messages, Thomas Fitzsimmons, 2023/06/01
- [elpa] externals/firefox-javascript-repl 9208c234cb 20/24: Fix kill-buffer-hook for Emacs 26.1, Thomas Fitzsimmons, 2023/06/01