在Tampermonkey中用户输入后消失的菜单命令

Tampermonkey是谷歌浏览器的扩展,试图模仿Greasemonkey的function。 为了清楚起见,我让我的脚本在Chrome中运行,并显示默认的JavaScript更改。 但是,我想测试菜单命令,并在单击Tampermonkey菜单中的命令后输入一个6位hex颜色代码。 我重新加载页面,命令从菜单中消失了! 我的脚本仍在那里(勾选了复选框)。

无论我做了什么或改变了什么代码,在设置了用户定义的输入之后,我永远无法模仿这个初始function。 这让我相信有一些我无法删除的持久性数据导致我的脚本过早失败。 注意:这个确切的脚本在Firefox中完美无误。

这显然不是Tampermonkey论坛,但这里的人似乎对跨平台的比较非常了解。 在下面的所有更改之后,我没有听到Chrome控制台的一次窥视,此时我真的只是出于想法。 这是我尝试过的一些事情(没有成功)。 列出了任何控制台错误:

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

这是我使用的代码:

// ==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。“

您正在测试此URL的URL是什么? 我在http://veekun.com/dex/gadgets/stat_calculator测试过。

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

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

我从来没有成为那个更新检查器的粉丝,所以我不打算深入了解为什么它似乎打破了这个脚本的Tampermonkey实例。 (那将是另一个问题 – 而且可能最好针对2位负责任的开发人员。)

现在,建议你删除它。 您的用户将根据需要检查更新:)。