问题描述
Tampermonkey是Google Chrome的一个扩展,它试图模拟Greasemonkey的功能。为了清楚起见,我让我的脚本在Chrome中工作,并且默认的JavaScript更改显示。然而,我想测试菜单命令,并在点击Tampermonkey菜单中的命令后输入了一个6位十六进制颜色代码。我重新加载了页面,命令从菜单中消失了!我的脚本仍然在那里(并勾选了复选框)。无论我做了什么或者更改了哪些代码,在设置用户定义的输入后,我都无法模拟此初始功能。这让我相信有一些持久性数据我无法删除,导致我的脚本过早失败。注意:这个确切的脚本完美无缺地在Firefox中运行。这显然不是Tampermonkey论坛,但这里的人们对跨平台的兼容性非常了解。在经历了下面的所有变更之后,我没有听到Chrome控制台的一个窥探,而我现在真的只是没有想法。以下是我尝试过的一些事情(没有成功)。列出了任何控制台错误:
- 将jQuery版本从1.5.1更改为1.3.2
- 将客户端存储从localStorage改为get / setValue
- 在页面加载后从控制台调用localStorage.getItem('prevoColor' li>从控制台调用GM_getValue =未定义GM_getValue
- 在Chrome选项中删除veekun.com的localStorage条目
- 刷新,安装脚本并重新启动浏览器的次数超过我可以计数的时间
- 使用Firebug Lite(bookmarklet)重复上述所有命令
下面是我使用的代码:
// == UserScript ==
// @name Veekun比较荧光笔
// @namespace标记:// veekun
// @description亮点专门针对veekun.com家族比较页面上的预演变(用户定义的颜色可用)
// @include http://veekun.com/dex/gadgets/*
// @作者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 ==
/ *
目标:更改复选标记颜色&如果
[DONE]将姓名移动到用户指定的颜色,那么宝宝可以通过任何演变获得LEVEL-UP动作
[DONE] a)确保动作不是TM或导师动作
[完成]任何其他中期进化都有一个最后的evo(Caterpie,Weedle家族)
[DONE]无法让人接受的举动a)确保移动不是TM或教师移动
[ DONE]任何pre-evo都有一个TUTOR移动未被任何evo(Murkrow in HG / SS)
[]上传到userscripts.org后执行自动更新
学分:Brock Adams,用于帮助Chrome兼容性
Metalkid,用于jQuery咨询
* /
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();
//本节讨论用户定义的颜色
GM_registerMenuCommand(Color for pre-evolutionary-only moves,prevoColorPrompt)
GM_registerMenuCommand(Color对于第一次进化移动,evoColorPrompt)
var prevoColor = GM_getValue('prevoColor','#FF0000');
var evoColor = GM_getValue('evoColor','#339900');
函数prevoColorPrompt()
{
var input = prompt(请为进化前口袋妖怪输入所需的6位十六进制颜色代码:)
GM_setValue('prevoColor','#'+ input);
}
函数evoColorPrompt()
{
var input = prompt(请输入所需的六位十六进制颜色代码,用于first-evolution pokemon: )
GM_setValue('evoColor','#'+ input);
}
//此循环测试样本标题行中的每个'th'元素,确定图表中当前有多少个Evos。
$('。header-row')。eq(1).find('th')。each(function(index)
{
if($(this ).find('a')。length!= 0)
{
switch(index)
{
case 2:
hasSecondEvo = true;
break;
案例3:
hasFinalEvo1 = true;
break;
案例4:
hasFinalEvo2 = true;
break;
}
}
});
//所有的'tr'兄弟都是TM动作,因为它是页面上的最后一部分
//这个数组只将可用的TM的名字放入TMmoves数组
)TMhead.nextAll()。each(function(index)
{
TMmoves.push($(this).children(:first)。find('a')。eq(0) .html());
});
$ b $('tr')。each(function(index)
{
var moveName = $(this).children(:first)。find('a ').eq(0).html();
moveName = $ .trim(moveName);
switch($(this).attr('id'))
{
'moves:level-up':
isLevelupMove = true;
break;
'moves:egg':
isLevelupMove = false;
break;
case'moves:tutor':
isTutorMove = true;
'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(fi nalEvo1Cell.html());
finalEvo2Cell = finalEvo1Cell.next();
finalEvo2Text = $ .trim(finalEvo2Cell.html());
//这会检查演变是否有复选标记
if(babyMoveText.length> 0)
{
if(hasSecondEvo&& secondEvoText .length == 0 || hasFinalEvo1&&&finalEvo1Text.length == 0 ||
hasFinalEvo2&&&finalEvo2Text.length == 0)
{
//看看在继续
var tm = tmCheck(moveName)之前移动是TM。
if(!tm)
{
if(secondEvoText.length> 0)
{
babyMoveCell.css(color ,evoColor);
secondEvoCell.css(color,evoColor);
babyMoveCell.prev()。find('a')。eq(0).css(color,evoColor); //突出显示移动名称
}
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);
}
}
}
}
});
函数tmCheck(输入)
{
var isTM = false;
//遍历TMmoves数组以查看输入是否与任何条目匹配
for(var i = 0; i< TMmoves.length; i ++)
{
if(input == TMmoves [i])
{
isTM = true;
休息;
}
}
if(isTM == true)
return true;
else
返回false;
}
// alert(evoColor:+ localStorage.getItem('evoColor')+。prevoColor:+ localStorage.getItem('prevoColor'));
任何有关这种情况发生的想法?
编辑:我对这个问题发出了警告,这是他的回复:Tampermonkey的@require实现不正确,它太频繁地下载我的更新程序,所以我禁止它通过更新程序来使用浏览器嗅探,我的服务器无法处理它带来的流量,它从我的服务器上下载的脚本不应该有任何实际的代码,因为它在脚本中导致错误,我猜Tampermonkey没有通过用户代理标题,当它执行这些请求时,我从来没有在Chrome中测试过我的更新程序,所以我不知道它为什么会中断,也许你可以尝试安装NinjaKit。
您测试了哪个网址?我测试了 http://veekun.com/dex/gadgets/stat_calculator
。
无论如何,脚本行为,与菜单命令相比,Tampermonkey看起来并不稳定。我真的不知道/没有真正检查脚本的其余部分是否正常工作。
罪魁祸首似乎是 sizzlemctwizzle.com
更新检查。删除它的 // @require
使菜单稳定。把这个指令放回去,再次破坏了脚本。
我从来不是那个更新检查器的粉丝,所以我不打算探讨它为什么会出现打破这个脚本的Tampermonkey实例。 (这将是另一个问题 - 也许最好针对2个负责任的开发人员。)
现在,建议您删除它。您的用户将根据需要检查更新:)。
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:
- Changing jQuery version from 1.5.1 to 1.3.2
- Calling localStorage.getItem('prevoColor') from console after page load (both values null)
- Changing client-side storage from localStorage to get/setValue
- Calling GM_getValue from the console = ReferenceError: GM_getValue is not defined
- Deleting localStorage entries for veekun.com in Chrome options
- Refreshing, Re-installing the script, and restarting the browser more times than I can count
- 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."
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 :) .
这篇关于用户在Tampermonkey中输入后消失的菜单命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!