Skip to content

Instantly share code, notes, and snippets.

@wsgac
Created July 16, 2025 11:27
Show Gist options
  • Save wsgac/2f3a4cd768017125d0ba7db54e4459b2 to your computer and use it in GitHub Desktop.
Save wsgac/2f3a4cd768017125d0ba7db54e4459b2 to your computer and use it in GitHub Desktop.
Make Eww recognize additional types of URLs as next/previous
(defvar navigation-group-regex "postnav\\|navigation"
"Regular expression to match the class of the navigation container")
(defvar prev-link-labels-regex "earlier\\|previous"
"Regular expression matching the `previous` link")
(defvar next-link-labels-regex "later\\|next"
"Regular expression matching the `next` link")
(defun my-eww-add-rel-prev-next ()
"Add rel=\"prev\" and rel=\"next\" attributes to postnav links in EWW."
(when (eq major-mode 'eww-mode)
(let ((dom (plist-get eww-data :dom)))
(when dom
(let* ((nav (dom-by-class dom navigation-group-regex))
(links (and nav (dom-by-tag nav 'a))))
(when links
(dolist (link links)
(let ((text (string-trim (dom-text link))))
(cond
((string-match-p prev-link-labels-regex (downcase text))
(plist-put eww-data :previous (dom-attr link 'href)))
((string-match-p next-link-labels-regex (downcase text))
(plist-put eww-data :next (dom-attr link 'href)))))))
;; No re-render needed — DOM is updated in place
(plist-put eww-data :dom dom))))))
(add-hook 'eww-after-render-hook #'my-eww-add-rel-prev-next)
@wsgac
Copy link
Author

wsgac commented Jul 16, 2025

To make this work with a particular website, make sure its specific structure can be properly matched by extending navigation-group-regex, prev-link-labels-regex, and next-link-labels-regex.

@wsgac
Copy link
Author

wsgac commented Jul 23, 2025

This is the result of an exploration I did, inspired by how Greasemonkey can run arbitrary scripts upon page load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment