def setupFormObserver():
    """
    Function generating a script to monitor a web form for user entries.
    
    @return script to monitor a web page
    @rtype str
    """
    source = """
(function() {
    function findUsername(inputs) {
        var usernameNames = ['user', 'name', 'login'];
        for (var i = 0; i < usernameNames.length; ++i) {
            for (var j = 0; j < inputs.length; ++j)
                if (inputs[j].type == 'text' &&
                    inputs[j].value.length &&
                    inputs[j].name.indexOf(usernameNames[i]) != -1)
                    return inputs[j].value;
        }
        for (var i = 0; i < inputs.length; ++i)
            if (inputs[i].type == 'text' && inputs[i].value.length)
                return inputs[i].value;
        for (var i = 0; i < inputs.length; ++i)
            if (inputs[i].type == 'email' && inputs[i].value.length)
                return inputs[i].value;
        return '';
    }
    
    function registerForm(form) {
        form.addEventListener('submit', function() {
            var form = this;
            var data = '';
            var password = '';
            var inputs = form.getElementsByTagName('input');
            for (var i = 0; i < inputs.length; ++i) {
                var input = inputs[i];
                var type = input.type.toLowerCase();
                if (type != 'text' && type != 'password' &&
                    type != 'email')
                    continue;
                if (!password && type == 'password')
                    password = input.value;
                data += encodeURIComponent(input.name);
                data += '=';
                data += encodeURIComponent(input.value);
                data += '&';
            }
            if (!password)
                return;
            data = data.substring(0, data.length - 1);
            var url = window.location.href;
            var username = findUsername(inputs);
            external.passwordManager.formSubmitted(
                url, username, password, data);
        }, true);
    }
    
    for (var i = 0; i < document.forms.length; ++i)
        registerForm(document.forms[i]);
    
    var observer = new MutationObserver(function(mutations) {
        for (var mutation of mutations)
            for (var node of mutation.addedNodes)
                if (node.tagName && node.tagName.toLowerCase() == 'form')
                    registerForm(node);
    });
    observer.observe(document.documentElement, {
        childList: true, subtree: true
    });
    
})()"""
    return source
