我正在尝试编辑一个 Javascript 小书签,它创建一个自动点击器(使用点击事件)以允许页面上同时有 2 个点击器。 我正在使用 Visual Studio Code 的网页版,并在闲置游戏 Cookie Clicker 上进行测试。
很抱歉评论乱七八糟,如果看起来不好。这是我第一次尝试使用 JS,但我能够弄明白,因为我正在使用 Java 参加编程课程。
这里是完整的代码:
//javascript: ...[removed to look cleaner]
//^original^
//GOAL: I want two autoclickers to be able to run at the same time
//SOLUTION(maybe): change if statement checking if there is already an autoclicker
// javascript:
var DELAY = 1;
var autoClickerStyleElement = document.createElement("style");//here
autoClickerStyleElement.innerHTML="*{cursor: crosshair !important;}";//to
document.body.appendChild(autoClickerStyleElement);//here sets up style/crosshair
function addClicker(e) {
if(!e.isTrusted) {//if click event is not trusted
return;}//do nothing
//THIS IS CODE THAT WAS ALREADY HERE
// if(e.target.classList.contains("auto-clicker-target")) {//if there's already one
// e.target.classList.remove("auto-clicker-target");}//remove it
// else {
// e.target.classList.add("auto-clicker-target");}//add target class to the click event's classList
//THIS IS AN EDITED VERSION OF THE CODE
if(e.target.classList.contains("auto-clicker-target1") && !(e.target.classList.contains("auto-clicker-target2"))) {//if there's 1st & not 2nd
e.target.classList.add("auto-clicker-target2");}//add a 2nd
//if 1st and 2nd exist, remove both
else if(e.target.classList.contains("auto-clicker-target1") && e.target.classList.contains("auto-clicker-target2")){//if it has both
e.target.classList.remove("auto-clicker-target1");//remove 1st
e.target.classList.remove("auto-clicker-target2");}//remove 2nd
//my code compacted: ...[removed to look cleaner]
//rewritten full: ...[removed to look cleaner]
//WHY NOT WORK?
//MAIN EDITED CODE END
document.body.removeChild(autoClickerStyleElement);//make crosshair disappear
document.body.removeEventListener("click", addClicker);//add clicker
e.preventDefault();
autoClick(e.target);//run autoclick where crosshair is clicked
}//end of addClicker
function autoClick(element) {
if(element.classList.contains("auto-clicker-target1")) {//i just duped this part and changed the name
element.click();//simulates click
setTimeout(function()//set delay for click
{ autoClick(element); }, DELAY);
}//ADDED:
if(element.classList.contains("auto-clicker-target2")) {
element.click();
setTimeout(function()
{ autoClick(element); }, DELAY);
}
}
document.body.addEventListener("click", addClicker, 0);//waits for my click to add autoclicker
这是我特别关注的代码:
//THIS IS CODE THAT WAS ALREADY HERE
// if(e.target.classList.contains("auto-clicker-target")) {//if there's already one
// e.target.classList.remove("auto-clicker-target");}//remove it
// else {
// e.target.classList.add("auto-clicker-target");}//add target class to the click event's classList
我试过改成这样:
//THIS IS AN EDITED VERSION OF THE CODE
if(e.target.classList.contains("auto-clicker-target1") && !(e.target.classList.contains("auto-clicker-target2"))) {//if there's 1st & not 2nd
e.target.classList.add("auto-clicker-target2");}//add a 2nd
//if 1st and 2nd exist, remove both
else if(e.target.classList.contains("auto-clicker-target1") && e.target.classList.contains("auto-clicker-target2")){//if it has both
e.target.classList.remove("auto-clicker-target1");//remove 1st
e.target.classList.remove("auto-clicker-target2");}//remove 2nd
我在某个地方搞砸了,因为现在它甚至不会显示十字准线。请帮我解决这个问题。