MediaWiki:UnhideUserMasthead/code.js

/** * Name:       UnhideUserMasthead * Description: Unhides user information in masthead on wikis where the user's *             masthead doesn't exist, and if the masthead does exist queries *             the API for user's wikis, which means if the user has hidden *             their wikis, they will be unhidden too. * Author:     KockaAdmiralac <1405223@gmail.com> * Notes:      The original version of this script located in my userspace on *              w:c:kocka was also able to unhide the user's birthday. *             By request of Staff, this option has been commented out. */ (function {   'use strict';    if($('#UserProfileMasthead').length === 0) {        return;    }    var UnhideUserMasthead = {        properties: [            'location',            'occupation',            // 'UserProfilePagesV3_birthday',            'UserProfilePagesV3_gender',            'website',            'twitter',            'fbPage',            'name'        ],        i18nMsg: [            'aka-label',            'location',            'my-website',            'my-twitter',            'my-fb-page',            'occupation',            'fav-wikis',            'was-born-on'        ],        i18n: {},        propMessageMap: {            location: 'location',            occupation: 'occupation',            UserProfilePagesV3_gender: '',            // UserProfilePagesV3_birthday: 'was-born-on',            website: 'my-website',            fbPage: 'my-fb-page',            twitter: 'my-twitter' },       websiteInfo: { website: ['website', ''], twitter: ['twitter', 'https://twitter.com/'], fbPage: ['facebook', ''] },       init: function(ui) { this.ui = ui; new mw.Api.get({               action: 'query',                meta: 'allmessages',                ammessages: this.i18nMsg.map(function(msg) { return 'user-identity-box-' + msg; }).join('|') + '|user-identity-i-am'           }).done($.proxy(this.i18nCallback, this)); },       i18nCallback: function(d) { if(!d.error) { d.query.allmessages.forEach(function(msg) {                   this.i18n[msg.name.substring(18)] = msg['*'];                }, this); $.nirvana.getJson('UserProfilePage', 'renderUserIdentityBox', {                   title: 'User:' + $('.masthead-info hgroup h1').text                }, $.proxy(this.detailsCallback, this)); }       },        detailsCallback: function(d) { var user = d.user; if(user) { console.log(user); if(user.registration) { // The masthead maybe doesn't have wikis, render them this.renderWikis(user.topWikis); } else { // The masthead has no info, render it                   $.get(                        'https://services.wikia.com/user-attribute/user/' +                            user.id,                        $.proxy(this.callbackProp, this)                    ); }           }        },        renderWikis: function(wikis) { if(wikis.length === 0) { // Wikis don't load when the user didn't make their masthead return; }           $('.masthead-info-lower .wikis').remove; this.ui({               type: 'ul',                attr: { 'class': 'wikis' },                children: [                    {                        type: 'span',                        text: this.i18n['fav-wikis']                    },                    {                        type: 'ul',                        children: wikis.filter(function(w) { return w.wikiName; }).map(function(w) { return { type: 'li', children: [ {                                       type: 'a', attr: { href: w.wikiUrl }, text: w.wikiName + ' (' + w.edits + ')' }                               ]                            };                        })                    }                ],                parent: '.masthead-info-lower'            }); },       callbackProp: function(d) { d._embedded.properties.forEach($.proxy(this.prop, this)); },       prop: function(d) { if(d.value) { switch(d.name) { case 'name': if(!$('.masthead-info hgroup h2').exists) { $('.masthead-info hgroup h1').after(this.ui({ type: 'h2', html: this.i18n['aka-label'] .replace('$1', d.value) }));                       }                        break; case 'UserProfilePagesV3_gender': case 'location': case 'occupation': this.ui({                           type: 'li',                            html: this.i18n[this.propMessageMap[d.name]]                                .replace('$1', d.value),                            parent: '.masthead-info .details ul'                        }); break; /*                   case 'UserProfilePagesV3_birthday': // TODO: Make this smarter var birthday = new Date(d.value); this.ui({                           type: 'li',                            html: this.i18n[this.propMessageMap.UserProfilePagesV3_birthday]                                .replace('$1', birthday.getDate + '.')                                .replace('$2', birthday.getMonth + 1 + '.'),                            parent: '.masthead-info .details ul'                        }); break; */                   case 'website': case 'twitter': case 'fbPage': var info = this.websiteInfo[d.name], className = info[0]; if(!$('.masthead-info-lower .links .' + className).exists) { this.ui({                               type: 'li',                                attr: { 'class': className },                                children: [                                    {                                        type: 'a',                                        attr: {                                            href: info[1] + ( info[1] === '' ? d.value : encodeURIComponent(d.value) )                                       },                                        children: [                                            {                                                type: 'img',                                                attr: {                                                    'class': className + ' icon'                                                }                                            },                                            // Don't blame me, Wikia does it this way too                                            '             ' +                                            this.i18n[this.propMessageMap[d.name]]                                        ]                                    }                                ],                                parent: '.masthead-info-lower .links'                            }); }               }            }        }    };    // Preload resources if(!window.dev || !window.dev.ui) { importArticle({           type: 'script',            article: 'u:dev:MediaWiki:UI-js/code.js'        }); }   mw.loader.using('mediawiki.api').done(function {        mw.hook('dev.ui').add($.proxy(UnhideUserMasthead.init, UnhideUserMasthead));    }); });