Disappearing Menu Commands After User Input in Tampermonkey

混江龙づ霸主 提交于 2019-12-24 09:27:53

问题


Tampermonkey is an extension for Google Chrome that attempts to emulate the functionality of Greasemonkey. To be clear, I got my script to work in Chrome and the default JavaScript changes to show up. I wanted to test the menu commands, however, and entered a 6-digit hex color code after clicking on the command in the Tampermonkey menu. I reloaded the page, and the commands disappeared from the menu! My script was still there (and the checkbox was ticked).

No matter what I did or what code I changed, I could never emulate this initial functionality after that user-defined input was set. This leads me to believe that there's some persistent data that I can't delete that's causing my script to fail prematurely. NOTE: This exact script works perfectly and without errors in Firefox.

This is obviously not a Tampermonkey forum, but people here seem very knowledgeable about cross-platform compatility. I didn't hear a single peep from the Chrome console after all of the changes below, and I'm really just out of ideas at this point. Here are some things I've tried (with no success). Any console errors are listed:

  1. Changing jQuery version from 1.5.1 to 1.3.2
  2. Calling localStorage.getItem('prevoColor') from console after page load (both values null)
  3. Changing client-side storage from localStorage to get/setValue
  4. Calling GM_getValue from the console = ReferenceError: GM_getValue is not defined
  5. Deleting localStorage entries for veekun.com in Chrome options
  6. Refreshing, Re-installing the script, and restarting the browser more times than I can count
  7. Repeating all of the above commands using Firebug Lite (bookmarklet)

Here's the code I was using:

// ==UserScript==
// @name           Veekun Comparison Highlighter
// @namespace      tag://veekun
// @description    Highlights moves exclusive to pre-evolutions on veekun.com's family comparison pages (user-defined colors available)
// @include        http://veekun.com/dex/gadgets/*
// @author         Matthew Ammann
// @version        1.0.3
// @date           3/11/11
// @require        http://sizzlemctwizzle.com/updater.php?id=98824
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

/*
Goal: Change checkmark color & move name to user-specified color on family comparison pages if 
[DONE] Baby poke has a LEVEL-UP move unlearned by any evolutions
    [DONE] a) Make sure move is not a TM or tutor move
[DONE] Any other mid-evolution has a move unlearnable by a final evo (Caterpie, Weedle families)
    [DONE] a) Make sure move is not a TM or tutor move
[DONE] Any pre-evo has a TUTOR move unlearned by any evo (Murkrow in HG/SS)
[] Implement auto-update after uploading to userscripts.org

Credits: Brock Adams, for helping with Chrome compatibility
         Metalkid, for the jQuery consult
*/

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)


var prevoColor = GM_getValue('prevoColor', '#FF0000');
var evoColor = GM_getValue('evoColor', '#339900');

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

function evoColorPrompt()
{
    var input = prompt("Please enter the desired 6-digit hex color-code for first-evolution pokemon:") 
    GM_setValue('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's 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'));

Any ideas as to why this is happening?

EDIT: I messaged sizzlemctwizzle about this problem, and this was his reply: "Tampermonkey’s @require implementation is incorrect. It downloads my updater far too often so I have banned it from using my updater via browser sniffing. My server just can’t handle the traffic it brings. The script it is downloading from my server shouldn’t have any actual code in it. Since it is causing errors with in your script I would guess Tampermonkey isn’t passing the User Agent header when it does these requests. I’m never tested my updater in Chrome so I have no idea why it breaks. Perhaps you could try and install NinjaKit instead."


回答1:


What URL are you testing this on? I tested on http://veekun.com/dex/gadgets/stat_calculator.

Anyway, the script behavior, vis à vis the menu commands did seem erratic with Tampermonkey. I couldn't really tell / didn't really check if the rest of the script was working as it should.

The culprit seems to be the sizzlemctwizzle.com update check. Removing its // @require made the menu stable. Putting that directive back, broke the script again.

I've never been a fan of that update checker, so I'm not going to dive into why it appears to be breaking the Tampermonkey instance of this script. (That would be another question -- and one probably best directed at the 2 responsible developers.)

For now, suggest you just delete it. Your users will check for updates as needed :) .



来源:https://stackoverflow.com/questions/5286597/disappearing-menu-commands-after-user-input-in-tampermonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!