This doesn't work
The reason calling eww-readable
right after eww
does not work is because eww
is asynchronous; when eww-readable
is called the eww
buffer is not yet rendered, so there are no "unreadable" parts to omit.
How to do that?
There may be a more elegant way, but if you have lexical binding (see the last section) enabled in your init file, you can write something like the following:
(defun today-ca ()
"Open Google homepage in `eww' with `eww-readable' enabled."
(interactive)
(letrec ((nonce (lambda ()
(unwind-protect
(eww-readable)
(remove-hook 'eww-after-render-hook nonce)))))
(add-hook 'eww-after-render-hook nonce))
(eww "https://google.com"))
The basic idea is adding the function eww-readable
to eww-after-render-hook
, so that it is run in the finalised buffer. The only issue lies in cleaning up the hook so that eww-readable
does not apply all the time.
We can't call remove-hook
right after eww
because eww-after-render-hook
will not have been run by that point. So the function we add to eww-after-render-hook
must remove itself. With lexical-binding
we can just create a closure as per my previous example. Alternatively, you could write an additional global helper function:
(defun my-eww-readable-nonce ()
"Once-off call to `eww-readable' after EWW is done rendering."
(unwind-protect
(eww-readable)
(remove-hook 'eww-after-render-hook #'my-eww-readable-nonce)))
(defun today-ca ()
"Open Google homepage in `eww' with `eww-readable' enabled."
(interactive)
(add-hook 'eww-after-render-hook #'my-eww-readable-nonce)
(eww "https://google.com"))
You could remove the call to unwind-protect
, but it ensures the hook is cleaned up even if something in eww-readable
fails.