Hello fellow Nyxt hackers. Here’s a small code snippet we’ve collectively came up with on IRC that you can paste to your config and enjoy the source-viewing via edit-source-in-external-editor:
(defun %edit-source-with-external-editor ()
"Edit page source using `external-editor-program'.
Create a temporary file. The editor runs synchronously so invoke on a
separate thread when possible."
(let ((page-source (if (current-mode 'web)
(plump:serialize (nyxt/web-mode:document-model (current-mode 'web)) nil)
(ffi-buffer-get-document (current-buffer)))))
(uiop:with-temporary-file (:directory (uiop:xdg-data-home nyxt::+data-root+)
:pathname p)
(when (> (length page-source) 0)
(alexandria:write-string-into-file page-source p :if-exists :supersede))
(log:debug "External editor ~s opens ~s"
(external-editor-program *browser*) p)
(with-protect ("Failed editing: ~a" :condition)
(uiop:run-program (append (external-editor-program *browser*)
(list (uiop:native-namestring p)))
:ignore-error-status t)))))
(define-command-global edit-source-with-external-editor ()
"Edit the current page source using `external-editor-program'.
Has no effect on the page, use only to look at sources!"
(if (external-editor-program *browser*)
(run-thread
(%edit-source-with-external-editor))
(echo-warning "Please set `external-editor-program' browser slot.")))
@aartaka By the way: any chance that we can provide a “dynamic” version of this function? Some websites use JavaScript exceedingly and so the HTML source is pretty dry because the contents is created dynamically.
How difficult would be to use some Parenscript to take a snapshot of the “real” source of the page? I mean the HTML that represents what you are seeing at a certain moment from the browser.
What do you think?
@Andrea, it is already there. nyxt/web-mode:document-model parses the page and gets you a fresh and full version of it in an easy to process Lispy data structure (namely plump:node). This command is already dynamic then, see the line with (plump:serialize (nyxt/web-mode:document-model ...) nil).
One thing to keep in mind when using a terminal $EDITOR is even though Nyxt defaults to it, it can’t open the terminal window in the first place unless specifically told to, so that’s what I had to use:
"--name" "floating" is not mandatory of course, it just matches with an i3wm rule in my configuration. I suppose Nyxt could use $TERMINAL and $EDITOR in that command too instead of hardcoding the programs?
Thanks a lot for creating this command @aartaka. Glad the command to view source is useful to several users, at least I didn’t bother you for nothing!