Add a way to use an external command to download HTML in eww

* doc/misc/eww.texi (Advanced): Document it.

* lisp/net/eww.el (eww-retrieve): New function.
(eww-reload): Use it.
(eww): Ditto.
(eww-retrieve-command): New variable.
This commit is contained in:
Lars Ingebrigtsen 2020-09-13 00:12:33 +02:00
parent 3e073520b3
commit 31be4d7ca4
3 changed files with 52 additions and 2 deletions

View file

@ -212,6 +212,23 @@ in an external browser by customizing
@node Advanced
@chapter Advanced
@findex eww-retrieve-command
EWW normally uses @code{url-retrieve} to fetch the @acronym{HTML}
before rendering it. It can sometimes be convenient to use an external
program to do this, and @code{eww-retrieve-command} should then be a
list that specifies a command and the parameters. For instance, to
use the Chromium browser, you could say something like this:
@lisp
(setq eww-retrieve-command
'("chromium" "--headless"
"--virtual-time-budget=3000"
"--dump-dom"))
@end lisp
The command should return the @acronym{HTML} on standard output, and
the data should use @acronym{UTF-8} as the charset.
@findex eww-view-source
@kindex v
@cindex Viewing Source

View file

@ -808,6 +808,11 @@ background colors or transparency, such as xbm, pbm, svg, png and gif.
** EWW
+++
*** New variable 'eww-retrieve-command'.
This can be used to download data via an external command. If nil
(the default), then 'url-retrieve' is used.
+++
*** New Emacs command line convenience function.
The 'eww-browse' command has been added, which allows you to register

View file

@ -134,6 +134,15 @@ The string will be passed through `substitute-command-keys'."
:type '(choice (const :tag "Unlimited" nil)
integer))
(defcustom eww-retrieve-command nil
"Command to retrieve an URL via an external program.
If nil, `url-retrieve' is used to download the data. If non-nil,
this should be a list where the first item is the program, and
the rest are the arguments."
:version "28.1"
:type '(choice (const :tag "Use `url-retrieve'" nil)
(list string)))
(defcustom eww-use-external-browser-for-content-type
"\\`\\(video/\\|audio/\\|application/ogg\\)"
"Always use external browser for specified content-type."
@ -346,9 +355,28 @@ killed after rendering."
(let ((eww-buffer (current-buffer)))
(with-current-buffer buffer
(eww-render nil url nil eww-buffer)))
(url-retrieve url #'eww-render
(eww-retrieve url #'eww-render
(list url nil (current-buffer))))))
(defun eww-retrieve (url callback cbargs)
(if (null eww-retrieve-command)
(url-retrieve url #'eww-render
(list url nil (current-buffer)))
(let ((buffer (generate-new-buffer " *eww retrieve*")))
(with-current-buffer buffer
(set-buffer-multibyte nil)
(make-process
:name "*eww fetch*"
:buffer (current-buffer)
:stderr (get-buffer-create " *eww error*")
:command (append eww-retrieve-command (list url))
:sentinel (lambda (process _)
(unless (process-live-p process)
(with-current-buffer buffer
(goto-char (point-min))
(insert "Content-type: text/html; charset=utf-8\n\n")
(apply #'funcall callback nil cbargs)))))))))
(function-put 'eww 'browse-url-browser-kind 'internal)
(defun eww--dwim-expand-url (url)
@ -1117,7 +1145,7 @@ just re-display the HTML already fetched."
(eww-display-html 'utf-8 url (plist-get eww-data :dom)
(point) (current-buffer)))
(let ((url-mime-accept-string eww-accept-content-types))
(url-retrieve url #'eww-render
(eww-retrieve url #'eww-render
(list url (point) (current-buffer) encode))))))
;; Form support.