blob: 7d990f3ae258f461fbad23f58edb6d03d72b89f1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
;;; eww-config --- EWW Text Browser Settings -*- lexical-binding: t; coding: utf-8; -*-
;; author Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;;
;; This module provides a minimal, privacy-focused browsing experience with:
;; - Simplified navigation keybindings (< and > for back/forward)
;; - Quick URL copying to clipboard
;; - Image toggle functionality
;; - Privacy-conscious defaults (no tracking info sent)
;; - Alternative search engine (Frog Find for simplified web pages)
;;
;; Key features:
;; - `M-E' to launch EWW
;; - `u' to copy current URL
;; - `i' to toggle images
;; - `o' to open link in new buffer
;;
;; The configuration tries to prioritize text-based browsing and minimal distractions.
;;
;;; Code:
;; ----------------------- EWW-Only User-Agent Injection -----------------------
(require 'cl-lib)
(defgroup my-eww-user-agent nil
"EWW-only User-Agent management."
:group 'eww)
(defcustom my-eww-user-agent
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0"
"User-Agent string to send for EWW requests only."
:type 'string
:group 'my-eww-user-agent)
(defun my-eww--inject-user-agent (orig-fun &rest args)
"Set a User-Agent only when making requests from an EWW buffer."
(if (derived-mode-p 'eww-mode)
(let* ((ua my-eww-user-agent)
;; Remove any existing UA header, then add ours.
(url-request-extra-headers
(cons (cons "User-Agent" ua)
(cl-remove-if (lambda (h)
(and (consp h)
(stringp (car h))
(string-equal (car h) "User-Agent")))
url-request-extra-headers))))
(apply orig-fun args))
(apply orig-fun args)))
(with-eval-after-load 'url
;; Cover both async and sync fetches used by eww/shr (pages, images, etc.).
(advice-add 'url-retrieve :around #'my-eww--inject-user-agent)
(advice-add 'url-retrieve-synchronously :around #'my-eww--inject-user-agent))
;; --------------------------------- EWW Config --------------------------------
(use-package eww
:ensure nil ;; built-in
:bind
(("M-E" . eww)
:map eww-mode-map
("<" . eww-back-url)
(">" . eww-forward-url)
("i" . eww-toggle-images)
("u" . cj/eww-copy-url)
("b" . cj/eww-bookmark-quick-add)
("B" . eww-list-bookmarks)
("/" . cj/eww-switch-search-engine)
("&" . cj/eww-open-in-external)
("o" . eww-open-in-new-buffer)
("r" . eww-readable))
:config
;; Define search engines
(defvar cj/eww-search-engines
'(("frog" . "http://frogfind.com/?q=")
("ddg" . "https://duckduckgo.com/html?q=")
("searx" . "https://searx.be/search?q="))
"List of search engines for EWW.")
(defvar cj/eww-current-search-engine "frog"
"Currently selected search engine.")
;; Function definitions
(defun cj/eww-switch-search-engine ()
"Switch between different search engines."
(interactive)
(let* ((engine (completing-read "Search engine: "
(mapcar #'car cj/eww-search-engines)
nil t nil nil cj/eww-current-search-engine))
(url (cdr (assoc engine cj/eww-search-engines))))
(when url
(setq eww-search-prefix url)
(setq cj/eww-current-search-engine engine)
(message "Search engine set to: %s" engine))))
(defun cj/eww-open-in-external ()
"Open current URL in external browser."
(interactive)
(unless (derived-mode-p 'eww-mode)
(user-error "Not in EWW buffer"))
(if-let ((url (plist-get eww-data :url)))
(browse-url-xdg-open url)
(user-error "No URL to open")))
(defun cj/eww-bookmark-quick-add ()
"Quickly bookmark current page with minimal prompting."
(interactive)
(unless (derived-mode-p 'eww-mode)
(user-error "Not in EWW buffer"))
(when-let ((title (plist-get eww-data :title)))
(let ((eww-bookmarks-directory (expand-file-name "eww-bookmarks" user-emacs-directory)))
(unless (file-exists-p eww-bookmarks-directory)
(make-directory eww-bookmarks-directory t))
(eww-add-bookmark)
(message "Bookmarked: %s" title))))
(defun cj/eww-copy-url ()
"Copy the current EWW URL to clipboard."
(interactive)
(unless (derived-mode-p 'eww-mode)
(user-error "Not in EWW buffer"))
(if-let ((current-url (plist-get eww-data :url)))
(progn
(kill-new current-url)
(message "URL copied: %s" current-url))
(message "No URL to copy")))
(defun cj/eww-clear-cookies ()
"Clear all cookies."
(interactive)
(setq url-cookie-storage nil)
(when (and url-cookie-file (file-exists-p url-cookie-file))
(delete-file url-cookie-file))
(message "Cookies cleared"))
;; Configuration settings
(setq shr-use-colors nil)
(setq shr-bullet "• ")
(setq eww-search-prefix (cdr (assoc cj/eww-current-search-engine cj/eww-search-engines)))
(setq url-cookie-file (expand-file-name "~/.local/share/cookies.txt"))
;; sets the user-agent for everything (e.g., package.el)
;;(setq url-user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0")
(setq url-privacy-level '(email lastloc))
(setq shr-inhibit-images t)
(setq shr-use-fonts nil)
(setq shr-max-image-proportion 0.2)
(setq eww-retrieve-command nil))
(provide 'eww-config)
;;; eww-config.el ends here
|