[EWW] 从其它浏览器导入书签

EWW (Emacs Web Wowser) 也支持书签,我想把 Chrome 的书签导入。首先,从 Chrome 导出 书签文件 (HTML 格式) (其它浏览器应该一样)。然后,执行类似下面的命令,导入 EWW

(defun chunyang-eww-import-bookmarks (bookmarks-html-file)
  "Import bookmarks from BOOKMARKS-HTML-FILE."
  (interactive "fBookmarks HTML File: ")

  ;; Check if some libraries exist
  (require 'eww)
  (unless (require 'dom nil 'no-error)
    (user-error "dom.el not available, it was added in Emacs 25.1"))
  (unless (fboundp 'libxml-parse-html-region)
    (user-error "`libxml-parse-html-region' not available, \
your Emacs doesn't have libxml2 support"))

  (with-temp-buffer
    (insert-file-contents bookmarks-html-file)
    (setq eww-bookmarks
          (loop with dom = (libxml-parse-html-region (point-min) (point-max))
                for a in (dom-by-tag dom 'a)
                for url = (dom-attr a 'href)
                for title = (dom-text a)
                for time = (current-time-string
                            (seconds-to-time
                             (string-to-number
                              (dom-attr a 'add_date))))
                collect (list :url url
                              :title title
                              :time time)))
    (eww-write-bookmarks))

注意上面的代码:

  • 需要 Emacs 25.1+,因为用到了 25.1 新加入的 dom.el搜索查找 DOM
  • 不会处理 Chrome 书签中的文件夹,因为 EWW 的书签不支持用文件夹管理书签;
  • 会覆盖原来的 EWW 书签。
3 个赞

刚试了一下EWW,比w3m快了不止一个数量级啊(在我的电脑上是这样)

但是网上它的教程好少啊

楼主有没有好的配置提供一份啊?

EWW 开箱即用吧,用不着任何配置。

我的配置里面有关于书签、搜索引擎、代码高亮、字体、亮度这几个方面,都是蛮私人的设定,对别人有没有帮助就不得而知了。

(use-package eww
  :defer t
  :preface
  (defun helm-eww-bookmarks ()
    "Alternative to `eww-list-bookmarks'."
    (interactive)
    (require 'helm)
    (require 'eww)
    (helm :sources
          (helm-build-sync-source "EWW Bookmarks"
            :candidates
            (lambda ()
              (cl-loop for elt in (eww-read-bookmarks)
                       collect (cons (plist-get elt :title)
                                     (plist-get elt :url))))
            :action #'eww)
          :buffer "*Helm EWW Bookmarks*"))

  (defun chunyang-eww-import-bookmarks (bookmarks-html-file)
    "Import bookmarks from BOOKMARKS-HTML-FILE."
    (interactive "fBookmarks HTML File: ")

    ;; Check if some libraries exist
    (require 'eww)
    (unless (require 'dom nil 'no-error)
      (user-error "dom.el not available, it was added in Emacs 25.1"))
    (unless (fboundp 'libxml-parse-html-region)
      (user-error "`libxml-parse-html-region' not available, \
your Emacs doesn't have libxml2 support"))

    (with-temp-buffer
      (insert-file-contents bookmarks-html-file)
      (setq eww-bookmarks
            (loop with dom = (libxml-parse-html-region (point-min) (point-max))
                  for a in (dom-by-tag dom 'a)
                  for url = (dom-attr a 'href)
                  for title = (dom-text a)
                  for time = (current-time-string
                              (seconds-to-time
                               (string-to-number
                                (dom-attr a 'add_date))))
                  collect (list :url url
                                :title title
                                :time time)))
      (eww-write-bookmarks)))
  :config
  ;; XXX Both Google & DuckDuckGo are currently bocked in China
  (setq eww-search-prefix "https://duckduckgo.com/html/?q=")
  ;; Eww doesn't support Javascript, but HTTPS version of Google requires it (?)
  ;; (setq eww-search-prefix "https://www.google.com.hk/search?q=")
  (setq eww-search-prefix "http://www.google.com.hk/search?q=")
  (setq eww-search-prefix "https://www.bing.com/search?q="))

(use-package shr-tag-pre-highlight
  :ensure t
  :after shr
  :config
  (add-to-list 'shr-external-rendering-functions
               '(pre . shr-tag-pre-highlight))

  (when (version< emacs-version "26")
    (with-eval-after-load 'eww
      (advice-add 'eww-display-html :around
                  'eww-display-html--override-shr-external-rendering-functions))))

(use-package shr
  :defer t
  :config
  ;; Don't use proportional fonts for text
  (setq shr-use-fonts nil))

(use-package shr-color
  :defer t
  :preface
  (defun chunyang-theme-dark-p ()
    "Return t if using Dark theme."
    ;; FIXME: Use a proper way for this
    (let ((theme (car custom-enabled-themes)))
      (and theme
           (or (string-match-p (rx (or "night" "eighties" "dark" "deep"))
                               (symbol-name theme))
               (string= (symbol-name theme) "wombat"))
           t)))
  :config
  (when (chunyang-theme-dark-p)
    ;; (info "(Mu4e) Displaying rich-text messages")
    (setq shr-color-visible-luminance-min 75)))
1 个赞

eww能登录论坛吗? 它与w3m比,有什么限制啊? 感觉它没有多少快捷键呢

不能(你自己试下就知道了)。EWW 不支持 JavaScript,登录的话需要用到 JavaScript。

没用过 w3m,不了解它

嗯,我觉得EWW挺好用的。

知道怎么设置它的代理吗?

网上的方法都是全局代理啊,怎么写一个toggle啊?需要的时候再开启代理。

关于 url.el 代理的讨论:

@zilongshanren21 天学会 Emacs 之第 19 天:Elisp 技巧 的末尾给了一个 Toggle 的命令,我自己写过一个类似的,但是我发现代理并不会及时地得到修改,我不清楚什么原因,你有这个需求地话,可以自己研究看看。另外,url-gateway-local-host-regexp 可以设置白名单。

有白名单的话就不用toggle了,我试试,谢谢。