GM_registerMenuCommand not defined

all. I am puzzled by why I keep getting the "GM_registerMenuCommand not defined" error when I try to run the usercript you created. I tried this in Firefox using Scriptish 1.0b9 and the latest version of Greasemonkey. I even turned off all the add-ons except the violin in order to understand if this was a conflict, but without joy.

I am including jQuery in my usercript using this Eric Wold template . Before trying this template, I put the same block of code in the template suggested by Joan Piedra, and everything worked fine. Unfortunately, the Piedra template did not work in Chrome, which, in my opinion, is necessary, given the growing user base of Chrome. The fragment that produces the error is below:

    // a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {


var isLevelupMove = false;
var isTutorMove = false;
var isTM = false;
var TMhead = $('#moves\\:machine');
var hasSecondEvo = false;
var hasFinalEvo1 = false;
var hasFinalEvo2 = false;
var header = $('.header-row').eq(1);
var TMmoves = new Array();

//This section deals with the user-defined colors 

GM_registerMenuCommand("Color for pre-evolutionary-only moves", prevoColorPrompt);
GM_registerMenuCommand("Color for first evolution-only moves", evoColorPrompt);

if(localStorage.getItem('prevoColor') == null || localStorage.getItem('evoColor') == null)
{
    localStorage.setItem('prevoColor', 'red');
    localStorage.setItem('evoColor', 'orange');
}
var prevoColor = localStorage.getItem('prevoColor');
var evoColor = localStorage.getItem('evoColor');

function prevoColorPrompt()
{
    var input = prompt("Please enter a desired 6-digit hex color-code for pre-evolutionary pokemon:") 
    localStorage.setItem('prevoColor', '#'+input);
}

function evoColorPrompt()
{
    var input = prompt("Please enter the desired 6-digit hex color-code for first-evolution pokemon:") 
    localStorage.setItem('evoColor', '#'+input);
}

//This loop tests each 'th' element in a sample header row, determining how many Evos are currently present in the chart.

$('.header-row').eq(1).find('th').each(function(index) 
{
    if($(this).find('a').length != 0)
    {
        switch(index)
        {
            case 2:
            hasSecondEvo = true;
            break;

            case 3:
            hasFinalEvo1 = true;
            break;

            case 4:
            hasFinalEvo2 = true;
            break;
        }
    }
});

//All 'tr' siblings are TM moves, since it the last section on the page
//This array puts only the names of the available TMs into the TMmoves array
TMhead.nextAll().each(function(index)
{
    TMmoves.push($(this).children(":first").find('a').eq(0).html());
});

$('tr').each(function(index) 
{
    var moveName = $(this).children(":first").find('a').eq(0).html();   
    moveName = $.trim(moveName);

    switch($(this).attr('id'))
    {
        case 'moves:level-up':
            isLevelupMove = true;   
            break;

        case 'moves:egg':
            isLevelupMove = false;
            break;  

        case 'moves:tutor':
            isTutorMove = true;

        case 'moves:machine':
            isTM = true;    
    }

    if(isLevelupMove || isTutorMove)
    {
        var babyMoveCell = $(this).find('td').eq(0);
        babyMoveText = $.trim(babyMoveCell.html());

        secondEvoCell = babyMoveCell.next();
        secondEvoText = $.trim(secondEvoCell.html());

        finalEvo1Cell = secondEvoCell.next();
        finalEvo1Text = $.trim(finalEvo1Cell.html());

        finalEvo2Cell = finalEvo1Cell.next();
        finalEvo2Text = $.trim(finalEvo2Cell.html());

        //This checks if evolutions have checkmarks

        if(babyMoveText.length > 0)
        {
            if(hasSecondEvo && secondEvoText.length == 0 || hasFinalEvo1 && finalEvo1Text.length == 0 || 
                hasFinalEvo2 && finalEvo2Text.length == 0)
            {
                //See if the move is a TM before proceeding 
                var tm = tmCheck(moveName);

                if(!tm)
                {

                    if(secondEvoText.length > 0)
                    {       
                        babyMoveCell.css("color", evoColor);
                        secondEvoCell.css("color", evoColor);
                        babyMoveCell.prev().find('a').eq(0).css("color", evoColor); //highlights move name
                    }
                    else
                    {
                        babyMoveCell.css("color", prevoColor);
                        babyMoveCell.prev().find('a').eq(0).css("color", prevoColor);
                    }
                }
            }
        }
        else if(secondEvoText.length > 0)
        {
            if(hasFinalEvo1 && finalEvo1Text.length == 0 || hasFinalEvo2 && finalEvo2Text.length == 0)
            {
                var tm = tmCheck(moveName); 

                if(!tm)
                {
                    secondEvoCell.css("color", evoColor);
                    babyMoveCell.prev().find('a').eq(0).css("color", evoColor);
                }
            }
        }
    }

});

function tmCheck(input)
{
    var isTM = false;

    //Iterate through TMmoves array to see if the input matches any entries
    for(var i = 0; i < TMmoves.length; i++)
    {   
        if(input == TMmoves[i])
        {
            isTM = true;
            break;
        }
    }

    if(isTM == true)
        return true;
    else
        return false;       
}

//alert("evoColor: " + localStorage.getItem('evoColor') + ". prevoColor: " + localStorage.getItem('prevoColor'))

}//end main()

// load jQuery and execute the main function
addJQuery(main);

usercript, . - , , !

+3
2

, addJQuery() , .

, GM_ , , addJQuery(), .

script GM_, GM // @require , jQuery. Chrome - Tampermonkey.

addJQuery() - .

+1

, , , addJQuery, , , , script, body.

, , -, GM_* . , , main, , Greasemonkey , , main, . , , DOM unsafeWindow, , , separateable.

+2

All Articles