1   Update on commandline.open() (2011-07-21T22:24:21Z)

Some months after I posted this, this didn’t work anymore. Yesterday, I finally decided to fix this. The JavaScript code is actually being evaluated, but the prompt with :bmark doesn’t show up. If I run manually, it still doesn’t sure, but the command has been entered into the history.

I read some plugins’ source code, but nothing really gave me any hint. They just work with virtually same commandline.open() code. But then I realized, they actually is executed after XMLHttpRequest().

So, the fix is

# map a :js my_bookmark_adder()<CR>
map a :js setTimeout(my_bookmark_adder, 0)<CR>

Or you can do it in the function.

I don’t know when and what actually cause this and I don’t really care. If you find out which commit causes this, feel free to tell me.

2   Original post

If you use bookmarks to organize your to-read list, or you bookmark on certain website a lot. You might want to tag with readlater or remove some words from bookmark titles, such as website’s name.

For instance, you bookmark questions you want to read or to answer later on Stack Overflow. Normally, the title would be Question blah blah blah - Stack Overflow. If you bookmark very often, you might want to get rid of " - Stack Overflow" and probably tag with "Stack Overflow", so it will be easy to find since :bmark doesn’t allow you to put a bookmark in a specific folder.

Here is the final example code, you can put it into your ~/.vimperatorrc:

:js << EOF
function my_bookmark_adder() {
  // stolen from Vimperator source code:
  // http://code.google.com/p/vimperator-labs/source/browse/common/content/bookmarks.js
  let options = {};

  let bmarks = bookmarks.get(buffer.URL).filter(function (bmark) bmark.url == buffer.URL);

  if (bmarks.length == 1)   {
    let bmark = bmarks[0];

    options["-title"] = bmark.title;
    if (bmark.keyword)
      options["-keyword"] = bmark.keyword;
    if (bmark.tags.length > 0)
      options["-tags"] = bmark.tags.join(", ");
    }
  else {
    if (buffer.title != buffer.URL)
      options["-title"] = buffer.title;
    }

  // Doing some stuff you need
  if (buffer.URL.indexOf('http://stackoverflow.com') >= 0) {
    let d = new Date();
    let timetag = '[' + d.getFullYear() + '-' + d.getMonth() + '-' + (d.getDate() < 10 ? '0' : '') + d.getDate() + ']'
    options["-title"] = timetag + ' ' + options["-title"].replace(' - Stack Overflow', '');

    let mytags = ['StackOverflow']
    if (getBrowser().contentDocument.body.innerHTML.indexOf('vote-accepted-on') >= 0)
      mytags.push('Answered');

    if (options["-tags"])
      options["-tags"] = options["-tags"] + ", " + mytags.join(", ");
    else
      options["-tags"] = mytags.join(", ");
    }

  commandline.open(":",
    commands.commandToString({ command: "bmark", options: options, arguments: [buffer.URL], bang: bmarks.length == 1 }),
    modes.EX);
  }
EOF

# map a :js my_bookmark_adder()<CR>
map a :js setTimeout(my_bookmark_adder, 0)<CR>

The most of the code was copied from Vimperator source, the only code I added is Doing some stuff you need part and the last map command. (You don’t need to replace the a key, you can bind it with a new key.)

This example shows how to remove " - Stack Overflow" from title and prefix a "[YYYY-MM-DD]" timestamp, then tag with "Stack Overflow". It also checks if this question is answered, if so, then tag with "Answered".

This is just a simple example to let you know you can have more fun when bookmarking and it’s not something Firefox can give you, only Vimperator can.

I believe some people could write a flexible and smart code to remove arbitrary website’s name from title, feel free to show off if you just happened to write one.