emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 55fa6a2: Add new user option eww-url-transformers


From: Lars Ingebrigtsen
Subject: master 55fa6a2: Add new user option eww-url-transformers
Date: Sat, 6 Nov 2021 19:45:10 -0400 (EDT)

branch: master
commit 55fa6a2655972017c692b515b8c476bb9705747d
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add new user option eww-url-transformers
    
    * doc/misc/eww.texi (Advanced): Document it.
    * lisp/net/eww.el (eww-url-transformers): New user option.
    (eww-remove-tracking): New default function.
    (eww--transform-url): Helper function.
    (eww-follow-link): Use it.
    (eww): Ditto.
---
 doc/misc/eww.texi |  9 +++++++++
 etc/NEWS          |  7 +++++++
 lisp/net/eww.el   | 24 ++++++++++++++++++++++--
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi
index 7635685..5d308ef 100644
--- a/doc/misc/eww.texi
+++ b/doc/misc/eww.texi
@@ -388,6 +388,15 @@ EWW buffers will be renamed after rendering a document.  
If this is
 can also be a user-defined function, which is called with no
 parameters in the EWW buffer, and should return a string.
 
+@cindex utm
+@vindex eww-url-transformers
+  EWW runs the URLs through @code{eww-url-transformers} before using
+them.  This user option is a list of functions, where each function is
+called with the URL as the parameter, and should return the (possibly)
+transformed URL.  By default, this variable contains
+@code{eww-remove-tracking}, which removes the common @samp{utm_}
+trackers from links.
+
 @node Command Line
 @chapter Command Line Usage
 
diff --git a/etc/NEWS b/etc/NEWS
index 3dcaffe..a502299 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -222,6 +222,13 @@ If non-nil, 'C-c C-a' will put attached files at the end 
of the message.
 ---
 *** HTML Mode now supports text/html and image/* yanking.
 
+** eww
+
++++
+*** New user option 'eww-url-transformers'.
+These are used to alter an URL before using it.  By default it removes
+the common utm_ trackers from URLs.
+
 ** Gnus
 
 +++
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 94a5890..61c7801 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -231,6 +231,13 @@ See also `eww-form-checkbox-selected-symbol'."
                  (const "☐")            ; Unicode BALLOT BOX
                  string))
 
+(defcustom eww-url-transformers '(eww-remove-tracking)
+  "This is a list of transforming functions applied to an URL before usage.
+The functions will be called with the URL as the single
+parameter, and should return the (possibly) transformed URL."
+  :type '(repeat function)
+  :version "29.1")
+
 (defface eww-form-submit
   '((((type x w32 ns) (class color))   ; Like default mode line
      :box (:line-width 2 :style released-button)
@@ -385,6 +392,7 @@ killed after rendering."
       (while (string-match "\\`/[.][.]/" (url-filename parsed))
         (setf (url-filename parsed) (substring (url-filename parsed) 3))))
     (setq url (url-recreate-url parsed)))
+  (setq url (eww--transform-url url))
   (plist-put eww-data :url url)
   (plist-put eww-data :title "")
   (eww--after-page-change)
@@ -1831,6 +1839,17 @@ The browser to used is specified by the
   (funcall browse-url-secondary-browser-function
            (or url (plist-get eww-data :url))))
 
+(defun eww-remove-tracking (url)
+  "Remove the commong utm_ tracking cookies from URLs."
+  (replace-regexp-in-string ".utm_.*" "" url))
+
+(defun eww--transform-url (url)
+  "Appy `eww-url-transformers'."
+  (when url
+    (dolist (func eww-url-transformers)
+      (setq url (funcall func url)))
+    url))
+
 (defun eww-follow-link (&optional external mouse-event)
   "Browse the URL under point.
 If EXTERNAL is single prefix, browse the URL using
@@ -1841,7 +1860,8 @@ If EXTERNAL is double prefix, browse in new buffer."
    (list current-prefix-arg last-nonmenu-event)
    eww-mode)
   (mouse-set-point mouse-event)
-  (let ((url (get-text-property (point) 'shr-url)))
+  (let* ((orig-url (get-text-property (point) 'shr-url))
+         (url (eww--transform-url orig-url)))
     (cond
      ((not url)
       (message "No link under point"))
@@ -1860,7 +1880,7 @@ If EXTERNAL is double prefix, browse in new buffer."
        (plist-put eww-data :url url)
        (eww-display-html 'utf-8 url dom nil (current-buffer))))
      (t
-      (eww-browse-url url external)))))
+      (eww-browse-url orig-url external)))))
 
 (defun eww-same-page-p (url1 url2)
   "Return non-nil if URL1 and URL2 represent the same page.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]