在我決定放棄 chromium 上除了 uBlock Origin 之外的所有擴展後,就必須尋找一些替代品了。-- Sanel Z(作者)
我是 會話管理器 的鐵粉,它是 Chrome 和 Chromium 的小插件,可以保存所有打開的選項卡,為會話命名,並在需要時恢復會話。
它非常有用,特別是如果你像我一樣,白天的時候需要在多個“思維活動”之間切換——研究、開發或者閱讀新聞。或者你只是單純地希望記住幾天前的工作流(和選項卡)。
在我決定放棄 chromium 上除了 uBlock Origin 之外的所有擴展後,就必須尋找一些替代品了。我的主要目標是使之與瀏覽器無關,同時會話鏈接必須保存在文本文件中,這樣我就可以享受所有純文本的好處了。還有什麼比 org 模式 更好呢 ;)
很久以前我就發現了這個小訣竅: 通過命令行獲取當前在谷歌 Chrome 中打開的標籤 再加上些 elisp 代碼:
<code>(require 'cl-lib)(defun save-chromium-session () "Reads chromium current session and generate org-mode heading with items." (interactive) (save-excursion (let* ((cmd "strings ~/'.config/chromium/Default/Current Session' | 'grep' -E '^https?://' | sort | uniq") (ret (shell-command-to-string cmd))) (insert (concat "* " (format-time-string "[%Y-%m-%d %H:%M:%S]") "\\n" (mapconcat 'identity (cl-reduce (lambda (lst x) (if (and x (not (string= "" x))) (cons (concat " - " x) lst) lst)) (split-string ret "\\n") :initial-value (list)) "\\n"))))))(defun restore-chromium-session () "Restore session, by openning each link in list with (browse-url).Make sure to put cursor on date heading that contains list of urls." (interactive) (save-excursion (beginning-of-line) (when (looking-at "^\\\\*") (forward-line 1) (while (looking-at "^[ ]+-[ ]+\\\\(http.?+\\\\)$") (let* ((ln (thing-at-point 'line t)) (ln (replace-regexp-in-string "^[ ]+-[ ]+" "" ln)) (ln (replace-regexp-in-string "\\n" "" ln))) (browse-url ln)) (forward-line 1)))))/<code>
那麼,它的工作原理是什麼呢?
運行上述代碼,打開一個新 org 模式文件並調用 M-x save-chromium-session。它會創建類似這樣的東西:
<code>* [2019-12-04 12:14:02] - https://www.reddit.com/r/emacs/comments/... - https://www.reddit.com/r/Clojure - https://news.ycombinator.com/<code>
也就是任何在 chromium 實例中運行著的 URL。要還原的話,則將光標置於所需日期上然後運行 M-x restore-chromium-session。所有標籤都應該恢復了。
以下是我的使用案例,其中的數據是隨機生成的:
<code>#+TITLE: Browser sessions* [2019-12-01 23:15:00]...* [2019-12-02 18:10:20]...* [2019-12-03 19:00:12] - https://www.reddit.com/r/emacs/comments/... - https://www.reddit.com/r/Clojure - https://news.ycombinator.com* [2019-12-04 12:14:02] - https://www.reddit.com/r/emacs/comments/... - https://www.reddit.com/r/Clojure - https://news.ycombinator.com/<code>
請注意,用於讀取 Chromium 會話的方法並不完美:strings 將從二進制數據庫中讀取任何類似 URL 字符串的內容,有時這將產生不完整的 URL。不過,你可以很方便地地編輯它們,從而保持會話文件簡潔。
為了真正打開標籤,elisp 代碼中使用到了 browse-url ,它可以通過 browse-url-browser-function 變量進一步定製成運行 Chromium、Firefox 或任何其他瀏覽器。請務必閱讀該變量的相關文檔。
別忘了把會話文件放在 git、mercurial 或 svn 中,這樣你就再也不會丟失會話歷史記錄了 :)
那麼 Firefox 呢?
如果你正在使用 Firefox(最近的版本),並且想要獲取會話 URL,下面是操作方法。
首先,下載並編譯 lz4json ,這是一個可以解壓縮 Mozilla lz4json 格式的小工具,Firefox 以這種格式來存儲會話數據。會話數據(在撰寫本文時)存儲在 $HOME/.mozilla/firefox/<unique-name>/sessionstore-backup /recovery.jsonlz4 中。/<unique-name>
如果 Firefox 沒有運行,則沒有 recovery.jsonlz4,這種情況下用 previous.jsonlz4 代替。
要提取網址,嘗試在終端運行:
<code>$ lz4jsoncat recovery.jsonlz4 | grep -oP '"(http.+?)"' | sed 's/"//g' | sort | uniq/<code>
然後更新 save-chromium-session 為:
<code>(defun save-chromium-session () "Reads chromium current session and converts it to org-mode chunk." (interactive) (save-excursion (let* ((path "~/.mozilla/firefox/<unique-name>/sessionstore-backups/recovery.jsonlz4") (cmd (concat "lz4jsoncat " path " | grep -oP '\"(http.+?)\"' | sed 's/\"//g' | sort | uniq")) (ret (shell-command-to-string cmd)))...;; rest of the code is unchanged/<unique-name>/<code>
更新本函數的文檔字符串、函數名以及進一步的重構都留作練習。
via: https://acidwords.com/posts/2019-12-04-handle-chromium-and-firefox-sessions-with-org-mode.html
作者: Sanel Z 選題: lujun9972 譯者: lujun9972 校對: wxy
本文由 LCTT 原創編譯, Linux中國 榮譽推出
閱讀更多 Linux中國 的文章