OrgMode and Firefox conversations

I like to use OrgMode because I feel organized and in control. That is definitely an illusion, but it is a nice one.

I am still using firefox to browse the web. Mainly because w3m doesn’t work well at all outside of plain text pages and ezbl is not yet ready. This means I am generating a lot of information; history, tabs, bookmarks that are outside of my massive org collection of stuff.

When I find something I would like to read, but don’t have time for at that moment, I would like to store it in orgmode. However, copy/paste the url+title got really tedious. As with anything tedious I tend not to do it. So my firefox ‘to read later’ bookmark folder got full and without a way to prioritize them it became a morass of unused links.

Wouldn’t it be nice if emacs could pull the current url and title from firefox and put that into org for me?

Queue MozRepl stage right

MozRepl gives you a simple line oriented repl into the entire browser context. You could even use telnet if you really wanted to.

So I hacked up some quick primitive functions to ask MozRepl for the current url and title.

The function that will send a string to Firefox. The repl MUST be started already which you can do with M-x run-mozilla

(require 'moz)

(defun jk/moz-get (attr)
  (comint-send-string (inferior-moz-process) attr)
  ;; try to give the repl a chance to respond
  (sleep-for 0 100))

The sleep-for function is there because there is no flow control on the mozrepl. If you call jk/moz-get in succession too quickly you will get whatever the last result was multiple times.

Get the current url:

(defun jk/moz-get-current-url ()
  (jk/moz-get "repl._workContext.content.location.href"))

Get the current title:

(defun jk/moz-get-current-title ()
  (jk/moz-get "repl._workContext.content.document.title"))

The repl buffer will now have the results printed as a string. Not useful. So we work a little buffer walking magic that makes too many assumptions.

(defun jk/moz-get-current (moz-fun)
  (funcall moz-fun)
  ;; doesn't work if repl takes too long to output string
                  (set-buffer (process-buffer (inferior-moz-process)))
                  (goto-char (point-max))
                  (setq jk/moz-current (buffer-substring-no-properties
                                        (+ (point-at-bol) (length moz-repl-name) 3)
                                        (- (point-at-eol) 1))))
  (message "%s" jk/moz-current)

This simply calls whatever func is passed in as moz-fun and grabs the last output line of the moz repl.

The last bit for getting what we want is to put all the above together into a couple convenience functions:

(defun jk/moz-url ()
  (jk/moz-get-current 'jk/moz-get-current-url)

(defun jk/moz-title ()
  (jk/moz-get-current 'jk/moz-get-current-title)

Now we can run (jk/moz-url) and it returns the url in whatever window/tab is currently active, same for (jk/moz-title)

Last step is to get this into org via org-capture using org-capture-templates.

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/" "Tasks")
         "* TODO %?\n %i\n %a")
        ("n" "Notes" entry (file+datetree "~/org/")
         "* %?\nEntered on %U\n %i\n %a")
        ("b" "Bookmark" entry (file+datetree "~/org/")
         "* %(concat \"[[\" (jk/moz-url) \"][\" (jk/moz-title) \"]]\")\n Entered on %U\n")

The orgmode capture template has %(sexp) so you can run arbitrary elisp code to generate the content of the template.

Now all I have to do is C-c c b C-c C-c and I have a ‘bookmark’ in org. I am finding this to be very useful.

Comments (4)

  1. Howdy,

    Recently I’ve found that if I want to read it later, I put it in my todo list in Org Mode, and if I don’t, I don’t even bookmark it. This happened “organically,” it wasn’t conscious. I just noticed one day “I’m not bookmarking anything in Firefox anymore!”

    Tuesday, January 18, 2011 at 9:43 pm #
  2. user wrote::

    Do you know about org-protocol? It implements the same thing, allows you to quickly create todo items out of webpages in the browser:

    Tuesday, February 15, 2011 at 1:12 pm #
  3. user wrote::

    There is also a screencast at the bottom of the linked page which demonstrates how it works.

    Tuesday, February 15, 2011 at 1:14 pm #
  4. Justin Kirby wrote::

    @user thanks, that looks like a much more reliable solution :)

    Wednesday, February 16, 2011 at 9:08 pm #

Trackbacks/Pingbacks (3)

  1. Firefox ? Emacs ??? | Amrta on Monday, June 27, 2011 at 5:00 am

    [...] OrgMode and Firefox conversations ????????????????????? (Org 7.4, Emacs 22.3)? [...]

  2. ?? Org Capture ?? | Amrta on Saturday, July 16, 2011 at 6:06 am

    [...] OrgMode and Firefox conversations ????????????? (?????? ???)? [...]

  3. ?? Org Capture ?? | Amrta on Wednesday, August 31, 2011 at 3:23 pm

    [...] OrgMode and Firefox conversations ????????????? (?????? ???)? [...]