After I made an automatic downloading for new subscription videos, I recently had this idea: Why I dont take an advantage of it? I could put a video ID into queue directory, then that script will download for me.

Its quite simple to write a file.

:js io.system('echo "Hello World from Vimperator!" \> ~/hi');

Thats it. There is no trick or secret, its as simple as you read. The only thing is you need to escape the redirection >.

I map <S-q> to write a file:

map <S-q> :js (function(){var href=getBrowser().contentDocument.location.href; if(href.indexOf("youtube.com")>=0) {var m=/v=([-_a-zA-Z0-9]+)/.exec(href);if(m){io.system('echo "'+href+'" \> ~/Videos/livibetter/queue/'+m[1]); commandline.echo(m[1]+" queued.");}};})()

Breaking down the function content:

var href = getBrowser().contentDocument.location.href;
if (href.indexOf("youtube.com") >= 0) {
  var m = /v=([-_a-zA-Z0-9]+)/.exec(href);
  if (m) {
    io.system('echo "' + href + '" \\> ~/Videos/livibetter/queue/' + m[1]);
    commandline.echo(m[1] + " queued.")};
    }
  }

Here is the actually code I put into my ~/.vimperatorrc.

The code I use probably only works for UNIX-like system. In fact, io.File should be the more proper way:

var f = new io.File('~/testfile');
f.write("Hey!"); // Works here, but might not what you want! Read next section for correct usage
delete f;

I am not sure if its okay to close the file using delete, but it works anyway and Vimperator told me there is no close() method. I think this io.File is not exactly the File object proposal by Mozilla, but :echo io.File === File returns true.

1   Update on File.write()

I was trying to is File.write() to write some data, but the written file only get the last content by write(). I read the source, here is the definition:

/**
 * Writes the string <b>buf</b> to this file.
 *
 * param {string} buf The file content.
 * param {string|number} mode The file access mode, a bitwise OR of
 *     the following flags:
 *       {link #MODE_RDONLY}:   0x01
 *       {link #MODE_WRONLY}:   0x02
 *       {link #MODE_RDWR}:     0x04
 *       {link #MODE_CREATE}:   0x08
 *       {link #MODE_APPEND}:   0x10
 *       {link #MODE_TRUNCATE}: 0x20
 *       {link #MODE_SYNC}:     0x40
 *     Alternatively, the following abbreviations may be used:
 *       ">"  is equivalent to {link #MODE_WRONLY} | {link #MODE_CREATE} | {link #MODE_TRUNCATE}
 *       ">>" is equivalent to {link #MODE_WRONLY} | {link #MODE_CREATE} | {link #MODE_APPEND}
 * default ">"
 * param {number} perms The file mode bits of the created file. This
 *     is only used when creating a new file and does not change
 *     permissions if the file exists.
 * default 0644
 * param {string} encoding The encoding to used to write the file.
 * default options["fileencoding"]
 */
write: function (buf, mode, perms, encoding) {

The code from previous section should be:

f.write("Hey!", ">>");
// or `File.MODE_WRONLY | File.MODE_CREATE | File.MODE_APPEND`,
// or just `">"` if it's one-time write on the file

2   Updates

  • 2010-11-26T04:28:30+0800: Add the correct usage of write()