学习香草JS,通过Tampermonkey和我自己的本地html / js在服务器页面上进行练习。 现在,我正在玩cookieclicker。 有时,页面上会随机出现一个金色的Cookie。如果单击该按钮,则有时会增加buff,这会增加从大Cookie点击次数中获得的Cookie数量。
目标:如果GoldenCookie出现(与频率无关),请单击它。 如果Buff处于活动状态:尽可能多地单击bigCookie,直到buff处于不活动状态。
我已经创建了此代码,它可以正常工作。 但是,使用setInterval时,它当然会重叠并降低性能。
// ==UserScript==
// @name GoldenCookie
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Intervall check and click golden cookie, if buff quickly "loop"click bigCookie
// @author ProfDrNoiR
// @match http://orteil.dashnet.org/cookieclicker/
// @grant none
// ==/UserScript==
function checkGoldenCookie (){
if (document.getElementsByClassName("crate enabled buff").length > 0) {
var Buff = true;
} else {
var Buff = false;
}
if (Buff) {
var IntervalBigCookie = setInterval(function(){
document.getElementById("bigCookie").click();
}, 5)
}
setTimeout(function(){
clearInterval(IntervalBigCookie);
}
, 1000);
if (document.getElementsByClassName("shimmer").length > 0) {
document.getElementsByClassName("shimmer")[0].click();
}
}
setInterval(checkGoldenCookie, 20);
随机buff出现在div类“ buffs”下,并获得“ buff”类。 随机的金色Cookie出现在div类“ shimmers”下,并获得类“ shimmer”。所以要远离
setInterval(checkGoldenCookie, 20);
并使它更好,我想到了EventListeners。我的研究在数组EventListeners上失败了,所以我考虑了childElementCount的解决方法,并开始创建它:
// ==UserScript==
// @name GoldenCookie
// @namespace http://tampermonkey.net/
// @version 2.0
// @description Auto golden Cookie Click and massive Big Cookie Click on Buff
// @author ProfDrNoiR
// @match http://orteil.dashnet.org/cookieclicker/
// @grant none
// ==/UserScript==
document.getElementById("shimmers").addEventListener(
"onchange", shimmerChange);
console.log("addEventListener shimmers Done");
document.getElementById("buffs").addEventListener(
"onchange", buffChange);
console.log("addEventListener buffs Done");
function shimmerChange()
{
var amtGoldenCookies = document.getElementById("shimmers").childElementCount;
while (amtGoldenCookies > 0)
{
document.getElementsByClassName("shimmer")[0].click();
amtGoldenCookies = amtGoldenCookies - 1;
console.log("addEventListener buffs Done")+amtGoldenCookies;
}
};
function buffChange()
{
var amtBuffs = document.getElementById("buffs").childElementCount;
if (amtBuffs > 0)
{
var buffActive = true;
console.log("buffActive true");
} else
{
var buffActive = false;
console.log("buffActive false");
}
if (buffActive)
{
var IntervalBigCookie = setInterval(function()
{
document.getElementById ("bigCookie").click();
console.log("bigCookie clicked");
}, 5);
} else
{
clearInterval(IntervalBigCookie);
console.log("clearInterval bigCookie click");
}
};
由于这很难测试,因此我在这里为您提供了我的保存游戏,其中goldenCookies相对经常出现:
Mi4wMTZ8fDE1NDc4NTQyMzQ2MzE7MTU0Njg1NTkzNjkwMjsxNTQ3OTAwMDI0OTQ4O1RyaXBsZSBNdWZmaW47YWF3b2R8MDExMTAwMTExMDEwMTAxMDAxMDB8MzAzMDU3OTQ5Njg3NzIuMzk7NTMyMDIxMDU0Njg2NzUuNjY0OzQyNjI0Ozc0MDsxOTU3NDMxNDg3MDIzMS45MDI7MzE3OzA7MDs2OTg4NDkyMTM3Njk3NDQ4MDAwOzA7MDswOzA7MDszOzUwOzA7MDswOzA7MDswOzswOzA7MTkxOzEzOzE3ODswOzA7NDM7LTE7LTE7LTE7LTE7ODs0OzA7MDswOzA7MDswOzEyOzE1NDc4ODQ5MTc3OTI7MDswOzt8MTYwLDI2MCw2NzM2NDg3MjM0MDU3LDAsLDA7MTQwLDM0MCw1MTU1NjQ4NTgyOCwwLCwxOzEyMCw2MTAsODUyNjM1MDMzNyw0LDE1NDc5MDAzODYwNTE6MjoxNTQ3ODYwNzQzOTIzOjA6MDo0MToxOjA6MTU0Nzg1NDIzNDYzNTogMTExMDAwMDAwMDAwMTEwMDAwMDAwMDAxMDAwMDAwMDAwMCAwOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MDowOjA6MjoxNToyOjM2OjI6MzU6MjozNDowOjA6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wMDAxMDAwMDAwMDAwMDAxMTAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDExMTAwMTExMDAxMTAwMDAwMDAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDExMDAwMDAwMDAwMDAxMDAwMDAwMDAwMDAwMDAwMDAwMTEwMTAwMTAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAxMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMTAwMDAwMDAwMDAwMDAwMDAwMHwwLDIzMTAsMzc5LDc7%21END%21
如您所见,我添加了一些console.logs,以了解我走了多远。添加了eventListeners。 ChromeDevTools还会在元素上显示eventListeners。
希望您能为我提供帮助,并将mit推向正确的方向。