Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
1.5 kB
1
Indexable
Never
var pageUrl = window.location.href;
var usernameStartsAt = pageUrl.lastIndexOf('Utilisateur:');
// Check if there is a valid string inside URL after "Utilisateur:".
if (usernameStartsAt > 0 && pageUrl.length > usernameStartsAt + 12) {
    // Get username from URL.
    var username = pageUrl.substring(usernameStartsAt + 12);
    // Check if username string is valid.
    if (!(username === undefined && username === null) && (typeof username === "string") && (username.length > 0)) {
        // Split any additional query parameters and arguments.
        var splitAt = username.indexOf('?');
        var extraUrlArgs = username.indexOf('#');
        if (extraUrlArgs > 0) {
            if (splitAt > 0) {
                if (extraUrlArgs < splitAt) {
                    spitAt = extraUrlArgs;
                }
            } else {
                splitAt = extraUrlArgs;
            }
        }
        if (splitAt > 0) {
            username = username.substring(0, splitAt);
        }
        // Make call to Wiki API.
        new mw.Api().get({
            action: 'query',
            format: 'json',
            uselang: 'fr',
            list: 'usercontribs',
            formatversion: 2,
            uclimit: 500,
            ucuser: username,
            ucprop: 'ids'
        }).done(function (data) {
            // Display number of contributions after bodyContent element.
            $('#bodyContent').append('<p>Nombre de contributions : ' + data.query.usercontribs.length + '</p>');
        });
    }
}
Leave a Comment