Jump to content

User:Bradv/catlist.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.
importScript('User:AzaToth/morebits.js');

function catlist() {
    if (mw.config.get('wgCanonicalNamespace') == "Category" || mw.config.get('wgPageName') == "User:Bradv15/catlist.js") {
        mw.util.addPortletLink( 'p-cactions', "javascript:catlist.callback()", "catlist", "catlist", "Generate List", "");
    }
}
 
$(catlist);
 
catlist.callback = function catlistCallback( uid ) {
    var Window = new SimpleWindow( 600, 400 );
    Window.setTitle(mw.config.get('wgTitle'));

    var form = new QuickForm( catlist.callback.evaluate );

    form.append( {type:'button', label:'Plain text',     
        name:'text',  event: function(e){catlist.callback.update(e)}} );
    form.append( {type:'button', label:'Wikilinks',      
        name:'links', event: function(e){catlist.callback.update(e)}} );
    form.append( {type:'button', label:'Extended links', 
        name:'extd',  event: function(e){catlist.callback.update(e)}} );
    form.append( {type:'button', label:'Extended links (with headers)',
        name:'extdhdr',  event: function(e){catlist.callback.update(e)}} );

    var output = form.append( {type:'div', id:'catlistOutput'} );

    var result = form.render();
    Window.setContent( result );
    Window.display();    
}

catlist.callback.update = function catlistUpdate(e) {
    var output = document.getElementById('catlistOutput');

    while (output.firstChild) 
    {
        output.removeChild(output.firstChild);
    };
    
    var pre = output.appendChild(document.createElement('pre'));

    var lstTD = document.evaluate('//div[@id="mw-pages"]//td/*', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i=0; i<lstTD.snapshotLength; i++) {
        var nod = lstTD.snapshotItem(i);
        if (nod.tagName == 'H3' && e.target.name == 'extdhdr') {
            if (nod.innerHTML.length==1) {
                pre.appendChild(document.createTextNode('==='+nod.innerHTML+'===\n\n'));
            }
        }
        else if (nod.tagName == 'UL') {
            var lstA = document.evaluate('.//a', nod, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            for (var j=0;j<lstA.snapshotLength; j++) {
                var title = lstA.snapshotItem(j).title;
                var text = '';          
                switch (e.target.name) {
                    case 'text':
                        text = title;
                        break;
                    case 'links':
                        text = '[['+title+']]';
                        break;
                    case 'extd':
                    case 'extdhdr':
                        text = '{{la|'+title+'}}';
                        break;
                }
                pre.appendChild(document.createTextNode(text+'\n\n'));
            }
        }
    }
}