From e7d07c825a29621cd1be7d787d5e1565bc3d3513 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 16 Jan 2025 09:50:31 +1100 Subject: [emacs] web related - fix browse-url-handlers - generalise fetch-and-browse flows --- emacs/.emacs.d/lisp/my/my-buffer.el | 2 -- emacs/.emacs.d/lisp/my/my-emms.el | 4 +++- emacs/.emacs.d/lisp/my/my-net.el | 21 ++++++++++++++++----- emacs/.emacs.d/lisp/my/my-web.el | 35 ++++++++++++++++++++++------------- 4 files changed, 41 insertions(+), 21 deletions(-) (limited to 'emacs/.emacs.d/lisp/my') diff --git a/emacs/.emacs.d/lisp/my/my-buffer.el b/emacs/.emacs.d/lisp/my/my-buffer.el index f2da7f5..a8683de 100644 --- a/emacs/.emacs.d/lisp/my/my-buffer.el +++ b/emacs/.emacs.d/lisp/my/my-buffer.el @@ -264,8 +264,6 @@ Focus write: make the current window the only one centered with width 80. If in org-mode, also narrow to current subtree. Make buffers on both sides empty read-only buffers." (interactive) - ;; Only one window in the current frame indicates we are in focus - ;; write mode. (if (and (equal (buffer-name (window-buffer (window-left (get-buffer-window)))) diff --git a/emacs/.emacs.d/lisp/my/my-emms.el b/emacs/.emacs.d/lisp/my/my-emms.el index fd3c73d..6e8cf2a 100644 --- a/emacs/.emacs.d/lisp/my/my-emms.el +++ b/emacs/.emacs.d/lisp/my/my-emms.el @@ -509,7 +509,9 @@ character." If the last command is `emms-playlist-mode-play-smart', then set `my-emms-score-delta' to 2." (when (eq last-command 'emms-playlist-mode-play-smart) - (setq my-emms-score-delta 2))) + (setq my-emms-score-delta 2) + (setq last-command nil)) + ) (defun my-emms-wrapped () "Print top 5 scored tracks." diff --git a/emacs/.emacs.d/lisp/my/my-net.el b/emacs/.emacs.d/lisp/my/my-net.el index 2574789..6212b50 100644 --- a/emacs/.emacs.d/lisp/my/my-net.el +++ b/emacs/.emacs.d/lisp/my/my-net.el @@ -30,11 +30,22 @@ ;;; net utilities (defvar my-download-dir "~/Downloads") -(defun my-make-file-name-from-url (url) - (file-name-nondirectory - (directory-file-name - (car (url-path-and-query (url-generic-parse-url - (url-unhex-string url))))))) +(defmacro my-url-as-googlebot (&rest body) + "Run BODY while spoofing as googlebot" + `(let ((url-request-extra-headers '(("X-Forwarded-For" . "66.249.66.1"))) + (url-user-agent + "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")) + ,@body)) + +(def-edebug-spec my-url-as-googlebot t) + +(defun my-make-file-name-from-url (url &optional extension) + (format "%s%s" + (file-name-nondirectory + (directory-file-name + (car (url-path-and-query (url-generic-parse-url + (url-unhex-string url)))))) + (if extension (concat "." extension) ""))) ;; stolen from `eww-make-unique-file-name' (defun my-make-unique-file-name (file directory) diff --git a/emacs/.emacs.d/lisp/my/my-web.el b/emacs/.emacs.d/lisp/my/my-web.el index f2e48ba..472cab6 100644 --- a/emacs/.emacs.d/lisp/my/my-web.el +++ b/emacs/.emacs.d/lisp/my/my-web.el @@ -137,20 +137,29 @@ ;;; webgetter (require 'my-net) -(defun my-open-spectator-au (url &optional no-overwrite) - (interactive "sspectator.com.au link: ") - (let ((url-request-extra-headers '(("X-Forwarded-For" . "66.249.66.1"))) - (url-user-agent "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")) - (let ((file-name - (if no-overwrite - (my-make-unique-file-name - (my-make-file-name-from-url url) - my-download-dir) - (expand-file-name +(defun my-fetch-browse (url &optional no-overwrite) + "Fetch URL to a local file then browse it with firefox. + +Useful for bypassing \"Enable JavaScript and cookies to continue\"." + (interactive "sUrl to fetch and browse: ") + (let ((file-name + (if no-overwrite + (my-make-unique-file-name (my-make-file-name-from-url url) - my-download-dir)))) - (url-copy-file url file-name (not no-overwrite)) - (browse-url-firefox (format "file://%s" file-name))))) + my-download-dir) + (expand-file-name + (my-make-file-name-from-url url "html") + my-download-dir)))) + (url-copy-file url file-name (not no-overwrite)) + (browse-url-firefox (format "file://%s" file-name)))) + +(defun my-fetch-browse-as-googlebot (url &optional no-overwrite) + "Same as `my-fetch-browse', but spoofing googlebot. + +Useful for bypassing some paywalls." + (interactive "sUrl to fetch and browse as googlebot: ") + (my-url-as-googlebot + (my-fetch-browse url no-overwrite))) (defun my-mastodon-url-p (url) "Guess if a url is a mastodon post. -- cgit v1.2.3