Created
July 16, 2025 11:27
-
-
Save wsgac/2f3a4cd768017125d0ba7db54e4459b2 to your computer and use it in GitHub Desktop.
Make Eww recognize additional types of URLs as next/previous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
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
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
, andnext-link-labels-regex
.