Jump to content

User:Arved/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[User:Caorongjin/wordcount]]
importScript('User:Caorongjin/wordcount.js');
 
/**
 * Anzahl der Beobachter für Artikel in einem Tab
 * oben rechts anzeigen lassen mit Verweis auf
 * Artikel-Informationsseite
 *
 * Nutzbar für den Vector- und Monobook-Skin
 *
 * Author: Benutzer:Nightfly85
 */
(function($, mw) {

    function placeTab(text, url, title, isSelected) {
        var liClass = isSelected ? 'selected' : '',
            $liElement = $('<li id="ca-n-watchers" style="display:none" class="' + liClass +'" ><span><a title="' + title + '" href="' + url + '">' + text + '</a></span></li>');
        $('#ca-history').after($liElement);
        $liElement.slideDown();
    }

    var isInfoPage = (mw.config.get('wgAction') == 'info'),
        articleName = mw.util.wikiUrlencode(mw.config.get('wgPageName')),
        wikiBaseUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath'),
        infoUrl = wikiBaseUrl + '/index.php?action=info&title=' + articleName,
        apiUrl  = wikiBaseUrl + '/api.php?action=query&prop=info&inprop=watchers&format=xml&titles=' + articleName;

    if(isInfoPage) {
        // The info tab is selected, so highlight the tab and forget the ajax stuff
        placeTab('Information', infoUrl, 'Page Information', true);
    } else {
        if(false === mw.config.get('wgIsArticle') ) {
            return; // Generated page 
        }
        $.ajax({ url: apiUrl }).done(function(xmlData) {
            var numViewer = $(xmlData).find('page').attr('watchers'),
                numViewerText  = (numViewer > 0) ? '<strong>' + numViewer + '</strong>' : 'Nearly no';
            placeTab(numViewerText + ' Watcher', infoUrl, 'Show Page Information', false);

        });
    }
})(jQuery, mediaWiki);