Posts tagged ‘Javascript’

Vimperator: Plugin to set proxies from the command line

There are firefox addons to set proxies. These can be useful if you are dealing with a small and fixed set of proxies. Since they generally form part of the menuing system, they can be got at using the emenu command in vimperator. One such example is foxyproxy.

However, if you use a *lot* of proxies this can all become somewhat unwieldy. An alternative is to add the following code to your vimperatorrc file. (Or bundle it up as a plugin, if you prefer).

js >>EOF
var pref = services.services.pref;

function set_proxy(host, port){
    liberator.echo(host + ':' + port);
    pref.setCharPref('network.proxy.http', host);
    pref.setIntPref('network.proxy.http_port', port);
    pref.setBoolPref('network.proxy.share_proxy_settings', true);
    pref.setIntPref('network.proxy.type', 1);
}

function get_proxy_string(){
    proxyType = pref.getIntPref('network.proxy.type');
    if (proxyType == 0){
        return 'no_proxy';
    } else if (proxyType == 1) {
        host = pref.getCharPref('network.proxy.http');
        port = pref.getIntPref('network.proxy.http_port');
        return host + ':' + port;
    } else {
        return 'non_http_proxy';
    }
}

commands.addUserCommand(['noproxy'],
    'Switch off proxy',
    function (args) {
        pref.setIntPref('network.proxy.type', 0);
    },
    {},
    true
);
commands.addUserCommand(['proxy'],
    'Set the proxy',
    function (args){
        switch (args.length) {
            case 0:
                liberator.echo(get_proxy_string());
                break;
            case 1:
                [host] = args;
                port = 3128;
                set_proxy(host, port);
                break;
            case 2:
                [host, port] = args;
                set_proxy(host, port);
                break;
            default:
                liberator.echo('proxy host port')
        }
    },
    {}, true);
EOF

This gives you the commands proxy [host [port]] to get and set the current proxy. And noproxy to clear proxies.

December 30, 2010 at 1:18 pm Leave a comment

Transparent javascript rpc

I’ve been writing a little ajaxish code using django as the server backend recently and have been finding it kind of irritating. The levels of indirection necessary for a simple function call seem excessive.

If you were on one machine and you wanted to call a function in response to a button click your code would look like this

function handleClick()
{
    // Perform actual operations that do things
}
button.click += handleClick

Whilst to do this using a client and a server you have to do the following in addition.

* Decide how you are going to serialize the arguments
* Serialize the arguments
* Decide on a url scheme for your function
* Be tormented by how to make an overall consistent scheme
* Be tormented by the principles of rest
* Define some urls
* Define a de-serialize
* Debug copiously

For most purposes this just seems silly. I’ve been feeling like what I want is something far simpler.

* I don’t want to have to worry about structuring urls
* I don’t want to worry about principles of url schemes
* I don’t want to worry about serialization
* I don’t want to have to debug things
* I don’t want to have to write boiler plate code

Also I don’t want to write java or compile any javascript (gwt or pyjamas). These approaches may indeed have large payoffs if you can live within the framework and once you have learnt it but this seems like a very large overhead.

I just want to be able to have a javascript function that I can on the client which corresponds to a function (rather than some crazy object/ class / data structure) which when called will deal with serialization and deciding on a url and calling this function.

In practice of course you mightn’t have a synchronous function in javascript, but something that takes a callback or similar but the principle remains the same.

The payoff to this approach seems large, everything suddenly becomes terribly simple – vast swathes of things that you would have had to thought of before disappear. But there are of course some potential costs:

* Defining a url scheme forces you to think about state and the structure of your data.
* Defining a url scheme forces you to separate out your client and server code somewhat – i.e define a generalish interface to your server. This allows other people to interact with your server and creates some structure, which no such separation you are more likely to generate a big ball of state
* The transparency can lead you to forget the difference between calling function. (I’m rather doubtful about this argument – I seem to be fairly aware of the implications of what I’m doing when I’m writing code. I might however agree that it can be an impediment when learning new code / sharing code between a large number of people). This argument reminds be of the ‘nice’ arguments one has bleated at you whilst in the education system.
* Url schemes make you think about state
* Debugging becomes more magical
* You don’t necessarily have the means to create more structure
* Eventually you will probably end up wanting to do some advanced serialization, there are going to be questions about this. Avoiding this for too long may lead to add hock structures permeating your code. There could be a mismatch between what is easy and what suddenly becomes terribly difficult.

March 29, 2010 at 11:20 pm Leave a comment

Javascript: keyCode versus charCode

Javascript key press events have both a keyCode and a charCode property. These are mutually exclusive – whenever one is non-zero the other is zero.

Which one is set depends on the type of event: keydown and keyup events give one events with keyCode set (they produce corresponding character) whilst keypress events give one events with charCode set.

July 26, 2009 at 11:35 pm Leave a comment


May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031