蚊章汇 - Sanel Z
type: channel
link: https://linuxcn.bwsl.wang/author/1365/
description: Sanel Z
items: 通过 Org 模式管理 Chromium 和 Firefox 会话

通过 Org 模式管理 Chromium 和 Firefox 会话
type: item
date: 2020-02-24T19:30:57+08:00
description: 我是会话管理器的铁粉，它是 Chrome 和 Chromium 的小插件，可以保存所有打开的选项卡，为会话命名，并在需要时恢复会话。它非常有用，特别是如果你像我一样，白天的时候需要在多个“思维活动”之间切换——研究、开发或者阅读新闻。或者你只是单纯地希望记住几天前的工作流（和选项卡）。在我决定放弃 chromium 上除了 uBlock Origin 之外的所有扩展后，就必须寻找一些替代品了。我的主要目标是使之与浏览器无关，同时会话链接必须保存在文本文件中，这样我就可以享受所有纯文本的好处了。还有什么比 org 模式更好呢 ;)很久以前我就发现了这个小诀窍：通过命令行获取当前在谷歌 Chrome 中打开的标签 再加上些 elisp 代码：(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)))))那么，它的工作原理是什么呢？运行上述代码，打开一个新 org 模式文件并调用 M-x save-chromium-session。它会创建类似这样的东西：* [2019-12-04 12:14:02] - https://www.reddit.com/r/emacs/comments/... - https://www.reddit.com/r/Clojure - https://news.ycombinator.com也就是任何在 chromium 实例中运行着的 URL。要还原的话，则将光标置于所需日期上然后运行 M-x restore-chromium-session。所有标签都应该恢复了。以下是我的使用案例，其中的数据是随机生成的：#+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://n…