User:Mainulraju/common.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* 
 
    Tutorial script: QuickRC ("Quick Recent Changes")
 
    A tutorial user script which adds a link to the toolbox to show a pop-up dialog with
    up to 25 recent edits.
 
    Demonstrates: 
        - Use of the API
        - Use of jQuery
        - Use of ResourceLoader default modules
 
    (Be bold and improve it!)
 
    -- 2011-11-03 Erik Moeller, public domain
 
*/
 
// Import the jQuery dialog plugin
mw.loader.using(['jquery.ui'], function() {
 
    $(document).ready(function() {
        // Add a link to the toolbox
        mw.util.addPortletLink(
          'p-tb', 
          'javascript:quickRC()', 
          'Quick changelog', 
          't-prettylinkwidget', 
          'Show a quick overview of changes', 
          '',
          '#t-print'
        );
    });
 
    // This function will not be in global scope by default because it is loaded through
    // a ResourceLoader callback, so we attach it to the window object.
    window.quickRC = function quickRC() {
        myPages = new Array();
        myTitleExists = new Array();
 
        // Fetch recent changes from the API
        jQuery.getJSON(
            mw.util.wikiScript( 'api' ), {
                'format': 'json',
                'action': 'query',
                'list': 'recentchanges',
                'rclimit' : 25
            }, function( data ) {
                $.each ( data.query.recentchanges , function ( k , v ) {
                                // Build a unique array of links, use mw.util helper to format them
                                if(!myTitleExists[v.title]) { 
                                    myPages.push( 
                                      mw.html.element( 
                                        'a', { href: mw.util.getUrl( v.title ), }, v.title) 
                                    );
                                }
                                myTitleExists[v.title]=1;
                } ) ;
                renderQuickRCDialog();
    		} 
        );
    }
 
    function renderQuickRCDialog() {
 
      var $dialog = $('<div></div>')
        .html('<b>Welcome, '+mw.user.getName()+'!</b> The following pages have been recently modified:<br/><ul><li>'+myPages.join("<br/><li>")+"</ul>")
        .dialog({
           autoOpen: true,
           title: 'Hello there!',
           width: '70%'
        });
 
    }
});