Ruminations on Code

Saturday, May 1, 2010

Self replicating JavaScript

Running the following programm with node.js will produce the exact output that makes up the program.

#!/usr/bin/env node

var sys = require("sys");

var repl = function () {

    sys.puts("#!/usr/bin/env node\n\nvar sys = require(\"sys\");\n\nvar repl = " + repl + "\n\nrepl();");
}

repl();


Assuming you put the code in the file replicate.js you should be able to do the following (on unix):
node replicate.js > r2.js
diff replicate.js r2.js && echo same
node r2.js > r3.js
diff r2.js r3.js && echo sameThis code was inspired by r.zip

Tuesday, March 23, 2010

Blinder - A Google Chrome extension to ignore certain content on web pages

I started my second Google Chrome extension today. It's called blinder and will allow to hide certain html-tags on a per domain basis.

The default config will include ad-removal for facebook.

The initial idea was to remove only ads on facebook but the prototype (called lessAds) had a to long dev cycle. Save - Reload extension - reload facebook - see the result. I need this all inside Chrome so I will configure the tags to hide in the options page.

Ah. You wonder what the first extension is? Its something I put together with some people I met for the first time for a GTUG Chrome Battle. We did not win because there was an extension that did the same as ours only better: Search in the content of an open tab.

This is also my first project managed with Mercurial, so expect some tests and problems while I get my head around a distributed VCS.

Monday, March 22, 2010

Reading files with node.js

I tried to find an example on using node.js to read a file line by line. I found none so I had to write one myself.


// Module: FileLineReader
// Constructor: FileLineReader(filename, bufferSize = 8192)
// Methods: hasNextLine() -> boolean
//          nextLine() -> String
//
//
var fs = require("fs");
var sys = require("sys");

exports.FileLineReader = function(filename, bufferSize) {

    if(!bufferSize) {
        bufferSize = 8192;
    }

    //private:
    var currentPositionInFile = 0;
    var buffer = "";
    var fd = fs.openSync(filename, "r");


    // return -1
    // when EOF reached
    // fills buffer with next 8192 or less bytes
    var fillBuffer = function(position) {

        var res = fs.readSync(fd, bufferSize, position, "ascii");

        buffer += res[0];
        if (res[1] == 0) {
            return -1;
        }
        return position + res[1];

    };

    currentPositionInFile = fillBuffer(0);

    //public:
    this.hasNextLine = function() {
        while (buffer.indexOf("\n") == -1) {
            currentPositionInFile = fillBuffer(currentPositionInFile);
            if (currentPositionInFile == -1) {
                return false;
            }
        }

        if (buffer.indexOf("\n") > -1) {

            return true;
        }
        return false;
    };

    //public:
    this.nextLine = function() {
        var lineEnd = buffer.indexOf("\n");
        var result = buffer.substring(0, lineEnd);

        buffer = buffer.substring(result.length + 1, buffer.length);
        return result;
    };

    return this;
};




And some TestCode:

var assert = require("assert"),
        sys = require("sys"),
        flr = require("./FileLineReader");

try {
    var reader = new flr.FileLineReader("top-5.txt", 10);

    sys.debug(__filename + " First line...");
    assert.equal(reader.hasNextLine(), true);
    assert.equal(reader.nextLine(), "yahoo.com");

    sys.debug(__filename + " Next line...");

    assert.equal(reader.hasNextLine(), true);
    assert.equal(reader.nextLine(), "youtube.com");
    sys.debug(__filename + " Next line...");

    assert.equal(reader.hasNextLine(), true);
    assert.equal(reader.nextLine(), "facebook.com");
    sys.debug(__filename + " Next line...");

    assert.equal(reader.hasNextLine(), true);
    assert.equal(reader.nextLine(), "live.com");

    assert.equal(reader.hasNextLine(), true);
    assert.equal(reader.nextLine(), "msn.com");


    assert.equal(reader.hasNextLine(), false);

} catch (err) {
    sys.debug(__filename + ": ERROR: " + err);
}



The testdata file top-5.txt looked like this:

yahoo.com
youtube.com
facebook.com
live.com
msn.com