1

Tampermonkey 是 Google Chrome 的一个扩展,它试图模拟 Greasemonkey 的功能。需要明确的是,我让我的脚本在 Chrome 中运行,并且显示了默认的 JavaScript 更改。然而,我想测试菜单命令,并在点击 Tampermonkey 菜单中的命令后输入了一个 6 位十六进制颜色代码。我重新加载了页面,命令从菜单中消失了!我的脚本仍然存在(并且勾选了复选框)。

无论我做了什么或更改了什么代码,在设置了用户定义的输入之后,我永远无法模拟这个初始功能。这使我相信有一些我无法删除的持久性数据导致我的脚本过早地失败。注意:这个确切的脚本在 Firefox 中完美运行并且没有错误。

这显然不是 Tampermonkey 论坛,但这里的人们似乎对跨平台兼容性非常了解。在进行了以下所有更改之后,我没有听到来自 Chrome 控制台的任何声音,而且我现在真的只是没有想法。这是我尝试过的一些事情(没有成功)。列出任何控制台错误:

  1. 将 jQuery 版本从 1.5.1 更改为 1.3.2
  2. 页面加载后从控制台调用 localStorage.getItem('prevoColor') (两个值都为空)
  3. 将客户端存储从 localStorage 更改为 get/setValue
  4. 从控制台调用 GM_getValue = ReferenceError: GM_getValue is not defined
  5. 在 Chrome 选项中删除 veekun.com 的 localStorage 条目
  6. 刷新、重新安装脚本和重新启动浏览器的次数超过我数不清
  7. 使用 Firebug Lite(书签)重复上述所有命令

这是我使用的代码:

// ==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'));

关于为什么会发生这种情况的任何想法?

编辑:我向 sizzlemctwizzle 发送了关于这个问题的消息,这是他的回复:“Tampermonkey 的 @require 实现不正确。它下载我的更新程序太频繁了,所以我禁止它通过浏览器嗅探使用我的更新程序。我的服务器无法处理它带来的流量。它从我的服务器下载的脚本中不应该有任何实际代码。由于它在您的脚本中导致错误,我猜 Tampermonkey 在执行这些请求时没有传递用户代理标头。我从来没有在 Chrome 中测试过我的更新程序,所以我不知道它为什么会中断。也许你可以尝试安装 NinjaKit。”

4

1 回答 1

1

你在哪个 URL 上测试这个?我在http://veekun.com/dex/gadgets/stat_calculator.

无论如何,相对于菜单命令的脚本行为在 Tampermonkey 中确实显得不稳定。我无法真正判断/没有真正检查脚本的其余部分是否正常工作。

罪魁祸首似乎是sizzlemctwizzle.com更新检查。删除它// @require使菜单稳定。将该指令放回去,再次破坏了脚本。

我从来都不是那个更新检查器的粉丝,所以我不打算深入探讨它为什么会破坏这个脚本的 Tampermonkey 实例。(这将是另一个问题 - 可能最好针对 2 个负责任的开发人员。)

目前,建议您将其删除。您的用户将根据需要检查更新:)。

于 2011-03-14T05:22:59.553 回答