User:Polygnotus/Scripts/FavouriteTemplates.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 15:21, 9 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Add Favorite Templates to WikiEditor 2010 toolbar
// Place this code in your common.js file

// Check if we're editing a page
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
    // Add a hook handler
    mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
        
        // Function to get favorite templates from user preferences
        async function getFavoriteTemplates() {
            try {
                const apiUrl = mw.config.get('wgScriptPath') + '/api.php';
                
                const params = {
                    action: 'query',
                    meta: 'userinfo',
                    uiprop: 'options',
                    format: 'json'
                };
                
                const response = await $.get(apiUrl, params);
                
                if (response.query && response.query.userinfo && response.query.userinfo.options) {
                    const options = response.query.userinfo.options;
                    
                    // Get the favorite templates
                    const favoriteTemplates = options['templatedata-favorite-templates'];
                    
                    if (favoriteTemplates) {
                        console.log('Favorite Templates (Page IDs):', favoriteTemplates);
                        
                        // Parse as JSON array
                        let templateIds = [];
                        if (typeof favoriteTemplates === 'string') {
                            try {
                                templateIds = JSON.parse(favoriteTemplates);
                            } catch (e) {
                                console.log('Could not parse favorites as JSON');
                                return [];
                            }
                        } else {
                            templateIds = favoriteTemplates;
                        }
                        
                        console.log('Template IDs:', templateIds);
                        
                        // Convert IDs to template names
                        if (templateIds.length > 0) {
                            const templateNames = await getTemplateNames(templateIds);
                            console.log('Template Names:', templateNames);
                            return templateNames;
                        }
                        
                        return [];
                    } else {
                        console.log('No favorite templates found.');
                        return [];
                    }
                }
                
                return [];
                
            } catch (error) {
                console.error('Error retrieving favorite templates:', error);
                return [];
            }
        }

        // Function to convert Page IDs to template names
        async function getTemplateNames(pageIds) {
            try {
                const apiUrl = mw.config.get('wgScriptPath') + '/api.php';
                
                // Convert array to pipe-separated string for the API
                const pageIdsString = pageIds.join('|');
                
                const params = {
                    action: 'query',
                    pageids: pageIdsString,
                    format: 'json'
                };
                
                const response = await $.get(apiUrl, params);
                
                if (response.query && response.query.pages) {
                    const pages = response.query.pages;
                    const templateNames = [];
                    
                    console.log('\n=== Template Details ===');
                    
                    for (const pageId of pageIds) {
                        const page = pages[pageId];
                        if (page && page.title) {
                            console.log(`ID ${pageId}: ${page.title}`);
                            templateNames.push({
                                id: pageId,
                                title: page.title,
                                namespace: page.ns
                            });
                        } else {
                            console.log(`ID ${pageId}: Template not found`);
                            templateNames.push({
                                id: pageId,
                                title: 'Unknown',
                                namespace: null
                            });
                        }
                    }
                    
                    return templateNames;
                }
                
                return [];
                
            } catch (error) {
                console.error('Error retrieving template names:', error);
                return [];
            }
        }

        // Function to create a safe button ID from template title
        function createButtonId(title) {
            // Remove Template: prefix if present
            const cleanTitle = title.replace(/^Template:/, '');
            // Replace spaces and special characters with hyphens
            return cleanTitle.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
        }

        // Function to create template syntax
        function createTemplateSyntax(title) {
            // Remove Template: prefix if present
            const cleanTitle = title.replace(/^Template:/, '');
            return `{{${cleanTitle}}}`;
        }

        // Function to add favorite template buttons
        async function addFavoriteTemplateButtons() {
            const favoriteTemplates = await getFavoriteTemplates();
            
            if (favoriteTemplates && favoriteTemplates.length > 0) {
                // Create the tools object for the buttons
                const templateTools = {};
                
                // Add each favorite template as a tool
                for (const template of favoriteTemplates) {
                    const buttonId = createButtonId(template.title);
                    const templateSyntax = createTemplateSyntax(template.title);
                    const displayTitle = template.title.replace(/^Template:/, '');
                    
                    templateTools[buttonId] = {
                        label: displayTitle,
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: templateSyntax
                            }
                        }
                    };
                }
                
                // Add the Templates section with all tools at once
                $textarea.wikiEditor( 'addToToolbar', {
                    section: 'main',
                    groups: {
                        'favorite-templates': {
                            label: 'Favorite Templates',
                            tools: templateTools
                        }
                    }
                } );
                
                console.log(`Added ${favoriteTemplates.length} favorite template buttons to toolbar`);
            } else {
                console.log('No favorite templates to add to toolbar');
            }
        }

        // Execute the function to add buttons
        addFavoriteTemplateButtons();
        
    } );
}