FANDOM


Greetings,

I have created an automated Pokémon wiki text generator. By visiting a Pokémon's page on the Official Pokédex, a console log will spit out the text of a basic wiki page. The GAME_MASTER data is provided by Celandro's PokéBattler GitHub project. I will be using this as I start to add the remaining Gen II Pokemon.

Words of caution

Be advised that moves are not sorted by damage, and not all moves will translate over correctly. This will require some minimal effort.

Some examples of such moves are:

  • V0100_MOVE_X_SCISSOR is converted to X Scisor, but should be X-Scisor.
  • V0275_MOVE_FUTURESIGHT is converted to Futuresight, but should be Future Sight.

Setup

Prerequisites

I have not tested this on Firefox yet, so Chrome is a sure bet.

Installation

Once you have installed the usersript plugin for your browser, simply open up the plugin's dashboard and add a new script. Copy and paste the script at the end of this page and hit save. The next time you go to a Pokédex entry, you will see the wiki text logged in the console.

Userscript

Copy the script and paste it into the plugin's editor. If you edit the script, please increment the version number accordingly.

// ==UserScript==
// @name         Pokedex Grabber
// @namespace    pokemon
// @version      1.0.4
// @description  Grab all the data!
// @author       rmkane
// @match        http://www.pokemon.com/us/pokedex/*
// @require      https://code.jquery.com/jquery-latest.min.js
// @grant        GM_log
// ==/UserScript==

(function() {
    'use strict';
    String.prototype.toTitleCase = function () {
        return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };

    const jsonUrl = 'https://rawgit.com/celandro/pokebattler-fight/master/src/main/resources/com/pokebattler/fight/data/pokemongo.json';

    var title = $('.pokedex-pokemon-pagination-title').children().get(0).childNodes;
    var data = {
        index : title[1].innerHTML.trim().substr(1),
        name : title[0].textContent.trim(),
        description: $('p.version-y').text().trim().split(/\s+/g).join(' ')
    };

    data['cursor'] = [];
    $('.pokedex-pokemon-pagination .pokedex-pokemon-pagination-wrapper').each((i, wrapper) => {
        data['cursor'][i] = {
            index : $(wrapper).find('.pokemon-number').text().trim().substr(1),
            name : $(wrapper).find('.pokemon-name').text().trim()
        };
    });

    data['evo'] = [];
    $('.pokedex-pokemon-evolution h3.match').map((i, evo) => {
        data['evo'].push(evo.childNodes[0].textContent.trim());
    });

    $('span.attribute-title').each((i, attr) => {
        var attrValue = $(attr).parent().children().get(1);
        var key = $(attr).text().toLowerCase();
        var value = null;

        if (data[key] != null) {
            return;
        }

        switch (key) {
            case 'gender':
                value = $(attrValue).children().map((j, child) => {
                    return child.className.match(/((?:fe)?male)/g)[0];
                }).toArray();
                break;
            default:
                value = $(attrValue).text().trim();
        }
        switch (key) {
            case 'height':
                value = (value.match(/^(\d+)'\s*(\d+)\"$/).slice(1).map((x, i) => parseInt(x, 10) * Math.pow(12, 1 - i)).reduce((r, x) => r + x) * 0.0254).toFixed(1) + ' m';
                break;
            case 'weight':
                value = (parseFloat(value.match(/^(\d+\.\d+)\s*lbs$/)[1]) * 0.453592).toFixed(1) + ' kg';
                break;
            default:
                value = $(attrValue).text().trim();
        }
        data[key] = value;
    });

    $('.dtm-type ul li a').each((i, type) => {
        if (i < 2) data['type' + (i + 1)] = $(type).text().trim();
    });

    data['weaknesses'] = $('.dtm-weaknesses ul li a').map((i, type) => {
        return $(type).text().trim();
    }).toArray();

    //GM_log(JSON.stringify(data, null, 2));

    $.getJSON(jsonUrl, function(jsonData) {
        var gameData = jsonData['itemTemplates'].filter(tpl => {
            return tpl['templateId'].match(new RegExp('^V0' + data['index'] + '_POKEMON_' + data['name'].replace(/\W+/, '').toUpperCase() + '$'));
        });

        if (gameData.length !== 1) {
            alert('Error: Cannot retrieve data for: ' + data['name']);
        }

        printWikiText(data, gameData[0]);
    });

    function ifUndef(value, replacement) {
        return (value == null || value == 'undefined') ? replacement : value;
    }

    function printWikiText(dexData, gameData) {
        GM_log([
            '{{Pokédex nav|' + [ dexData['cursor'][0]['index'], dexData['cursor'][0]['name'], dexData['cursor'][1]['index'], dexData['cursor'][1]['name']].join('|') + '}}',
            '{{Infobox Pokémon',
            ' | image        = ' + dexData['name'] + '.png',
            ' | pokedex      = ' + dexData['index'],
            ' | type1        = ' + dexData['type1'],
            ' | type2        = ' + ifUndef(dexData['type2'], ''),
            ' | weak         = ' + dexData['weaknesses'].map(w => '[[' + w + ']]').join(' / '),
            ' | height       = ' + dexData['height'],
            ' | weight       = ' + dexData['weight'],
            ' | xsheight     = ',
            ' | nrmheight    = ',
            ' | xlheight     = ',
            ' | xsweight     = ',
            ' | nrmweight    = ',
            ' | xlweight     = ',
            ' | cp           = ',
            ' | hp           = ',
            ' | stamina      = ' + gameData['pokemonSettings']['stats']['baseStamina'],
            ' | attack       = ' + gameData['pokemonSettings']['stats']['baseAttack'],
            ' | defense      = ' + gameData['pokemonSettings']['stats']['baseDefense'],
            ' | capture      = ' + Math.round(ifUndef(gameData['pokemonSettings']['encounter']['baseCaptureRate'], 0) * 100) + '%',
            ' | flee         = ' + Math.round(gameData['pokemonSettings']['encounter']['baseFleeRate'] * 100) + '%',
            ' | candytype    = ' + gameData['pokemonSettings']['familyId'].replace('FAMILY_', '').toTitleCase(),
            ' | needevolve   = ' + ifUndef(gameData['pokemonSettings']['candyToEvolve'], 'N/A'),
            ' | stage1       = ' + ifUndef(dexData['evo'][0], dexData['name']),
            ' | stage2       = ' + ifUndef(dexData['evo'][1], ''),
            ' | stage3       = ' + ifUndef(dexData['evo'][2], ''),
            '}}',
            '',
            '==Description==',
            '\'\'\'' + dexData['category'] + ' Pokémon\'\'\'',
            '',
            dexData['description'],
            '',
            '==Possible Attacks==',
            '\'\'\'Fast Attacks\'\'\'',
            gameData['pokemonSettings']['quickMoves'].map(m => '* {{Move fast|' + m.replace('_FAST', '').replace('_', ' ').toTitleCase() + '}}').join('\n'),
            '\'\'\'Special Attacks\'\'\'',
            gameData['pokemonSettings']['cinematicMoves'].map(m => '* {{Move special|' + m.replace('_', ' ').toTitleCase() + '}}').join('\n'),
            '',
            '==Extra Info==',
            '',
            '==Links==',
            '* {{Pokedex link}}',
            '* {{Pokemon Wiki link}}',
            '',
            '==History==',
            '',
            '==Gallery==',
            '<gallery hideaddbutton="true">',
            '</gallery>',
            '',
            '[[Category:Pokémon]]',
            '[[Category:Generation ' + (dexData['index'] - 151 <= 0 ? 'I' : 'II') + ']]',
            '[[Category:' + dexData['type1'] + ']]' + (dexData['type2'] != null ? '\n[[Category:' + dexData['type2'] + ']]' : ''),
            dexData['weaknesses'].map(w => '[[Category:Weak to ' + w + ']]').join('\n'),
        ].join('\n'));
    }
})();

Feedback

If you receive any errors or see a problem with the script, leave a reply.

— Thanks Rmkane (talk) 03:31, February 25, 2017 (UTC)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.