Crashtestdummy
Crashtestdummy

Reputation: 9

How can I edit an autoclicker Javascript bookmarklet to allow two autoclickers at the same time?

I am trying to edit a Javascript bookmarklet that creates an autoclicker(using the click event) to allow 2 clickers on the page at the same time. I am using the web version of Visual Studio Code and testing it on the idle game Cookie Clicker.

Sorry for the mess of comments and if it looks bad. This is my first time trying to use JS but I'm able to figure it out because I'm in a programming course using Java.

Here is the entire code:

//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 the code I am specifically focusing on:

//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

I tried changing it to this:

//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

I big screwed up somewhere because now it won't even show the crosshair. Please help me fix this.

Upvotes: 0

Views: 1583

Answers (1)

Glitch
Glitch

Reputation: 33

This should work

javascript:(function()%7Bfunction%20callback()%7B(function(%24)%7Bvar%20jQuery%3D%24%3B%2F***%20Modified%20by%20Jason%20O'Neill%202017*%20Andrew%20Childs%20%3Cac%40glomerate.com%3E*%2Fvar%20DomOutline%20%3D%20function%20(options)%20%7B'use%20strict'%3Boptions%20%3D%20options%20%7C%7C%20%7B%7D%3Bvar%20pub%20%3D%20%7B%7D%2Cself%20%3D%20%7Bopts%3A%20%7Bnamespace%3A%20options.namespace%20%7C%7C%20'DomOutline'%2CborderWidth%3A%20options.borderWidth%20%7C%7C%202%2ConClick%3A%20options.onClick%20%7C%7C%20false%2Cborder%3A%20options.border%20%7C%7C%20false%2Crealtime%3A%20options.realtime%20%7C%7C%20false%2Clabel%3A%20options.label%20%7C%7C%20false%2CclassExclude%3A%20options.exclude%20%7C%7C%20null%2CzIndex%3A%20options.zIndex%20%7C%7C%201000000%2C%7D%2CkeyCodes%3A%20%7BBACKSPACE%3A%208%2CESC%3A%2027%2CDELETE%3A%2046%7D%2Cactive%3A%20false%2CsingleHighlightActive%3A%20false%2CsingleHighlightLoop%3A%20null%2Cinitialized%3A%20false%2Celements%3A%20%7B%7D%2C%7D%3Bfunction%20writeStylesheet(css)%20%7Bvar%20element%20%3D%20document.createElement('style')%3Belement.type%20%3D%20'text%2Fcss'%3Bdocument.getElementsByTagName('head')%5B0%5D.appendChild(element)%3Bif%20(element.styleSheet)%20%7Belement.styleSheet.cssText%20%3D%20css%3B%7D%20else%20%7Belement.innerHTML%20%3D%20css%3B%7D%7Dfunction%20initStylesheet()%20%7Bvar%20css%20%3D%20''%3Bif%20(self.initialized%20!%3D%3D%20true)%20%7Bcss%20%2B%3D'.'%20%2B%20self.opts.namespace%20%2B%20'%20%7B'%20%2B'%20%20%20%20background%3A%20rgba(0%2C%20153%2C%20204%2C%200.5)%3B'%20%2B'%20%20%20%20position%3A%20absolute%3B'%20%2B'%20%20%20%20z-index%3A%20'%2Bself.opts.zIndex%2B'%3B'%20%2B'%20%20%20%20pointer-events%3A%20none%3B'%20%2B'%7D'%20%2B'.'%20%2B%20self.opts.namespace%20%2B%20'_label%20%7B'%20%2B'%20%20%20%20background%3A%20%2309c%3B'%20%2B'%20%20%20%20border-radius%3A%202px%3B'%20%2B'%20%20%20%20color%3A%20%23fff%3B'%20%2B'%20%20%20%20font%3A%20bold%2012px%2F12px%20Helvetica%2C%20sans-serif%3B'%20%2B'%20%20%20%20padding%3A%204px%206px%3B'%20%2B'%20%20%20%20position%3A%20absolute%3B'%20%2B'%20%20%20%20text-shadow%3A%200%201px%201px%20rgba(0%2C%200%2C%200%2C%200.25)%3B'%20%2B'%20%20%20%20z-index%3A%20'%2B(self.opts.zIndex%2B1)%2B'%3B'%20%2B'%20%20%20%20pointer-events%3A%20none%3B'%20%2B'%7D'%20%2B'.'%20%2B%20self.opts.namespace%20%2B%20'_box%20%7B'%20%2B'%20%20%20%20background%3A%20rgba(0%2C%20153%2C%20204%2C%200.5)%3B'%20%2B'%20%20%20%20position%3A%20absolute%3B'%20%2B'%20%20%20%20z-index%3A%20'%2Bself.opts.zIndex%2B'%3B'%20%2B'%20%20%20%20pointer-events%3A%20none%3B'%20%2B'%7D'%3BwriteStylesheet(css)%3Bself.initialized%20%3D%20true%3B%7D%7Dfunction%20createOutlineElements()%20%7Bself.elements.label%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace%20%2B%20'_label').appendTo('body')%3Bself.elements.top%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace).appendTo('body')%3Bself.elements.bottom%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace).appendTo('body')%3Bself.elements.left%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace).appendTo('body')%3Bself.elements.right%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace).appendTo('body')%3Bself.elements.box%20%3D%20jQuery('%3Cdiv%3E').addClass(self.opts.namespace%20%2B%20'_box').appendTo('body')%3B%7Dfunction%20removeOutlineElements()%20%7BjQuery.each(self.elements%2C%20function%20(name%2C%20element)%20%7Belement.remove()%3B%7D)%3B%7Dfunction%20compileLabelText(element%2C%20width%2C%20height)%20%7Bvar%20label%20%3D%20element.tagName.toLowerCase()%3Bif%20(element.id)%20%7Blabel%20%2B%3D%20'%23'%20%2B%20element.id%3B%7Dif%20(element.className)%20%7Blabel%20%2B%3D%20('.'%20%2B%20jQuery.trim(element.className).replace(%2F%20%2Fg%2C%20'.')).replace(%2F%5C.%5C.%2B%2Fg%2C%20'.')%3B%7Dreturn%20label%20%2B%20'%20('%20%2B%20Math.round(width)%20%2B%20'x'%20%2B%20Math.round(height)%20%2B%20')'%3B%7Dfunction%20getScrollTop()%20%7Bif%20(!self.elements.window)%20%7Bself.elements.window%20%3D%20jQuery(window)%3B%7Dreturn%20self.elements.window.scrollTop()%3B%7Dfunction%20stopOnEscape(e)%20%7Bif%20(e.keyCode%20%3D%3D%3D%20self.keyCodes.ESC%20%7C%7C%20e.keyCode%20%3D%3D%3D%20self.keyCodes.BACKSPACE%20%7C%7C%20e.keyCode%20%3D%3D%3D%20self.keyCodes.DELETE)%20%7Bpub.stop()%3B%7Dreturn%20false%3B%7Dfunction%20draw(e)%20%7Bif%20(e.target.className.indexOf(self.opts.namespace)%20!%3D%3D%20-1%20%7C%7C%20e.target.className.indexOf(self.opts.classExclude)%20!%3D%3D%20-1)%20%7Breturn%3B%7Dpub.element%20%3D%20e.target%3Bvar%20b%20%3D%20self.opts.borderWidth%2Cscroll_top%20%3D%20getScrollTop()%2Cpos%20%3D%20pub.element.getBoundingClientRect()%2Ctop%20%3D%20pos.top%20%2B%20scroll_top%2Clabel_text%20%3D%20''%2Clabel_top%20%3D%200%2Clabel_left%20%3D%200%3Bif%20(self.opts.label)%20%7Blabel_text%20%3D%20compileLabelText(pub.element%2C%20pos.width%2C%20pos.height)%3Blabel_top%20%3D%20Math.max(0%2C%20top%20-%2020%20-%20b%2C%20scroll_top)%3Blabel_left%20%3D%20Math.max(0%2C%20pos.left%20-%20b)%3Bself.elements.label.css(%7B%20top%3A%20label_top%2C%20left%3A%20label_left%20%7D).text(label_text)%3B%7Dif%20(self.opts.border)%20%7Bself.elements.top.css(%7B%20top%3A%20Math.max(0%2C%20top%20-%20b)%2C%20left%3A%20pos.left%20-%20b%2C%20width%3A%20pos.width%20%2B%20b%2C%20height%3A%20b%20%7D)%3Bself.elements.bottom.css(%7B%20top%3A%20top%20%2B%20pos.height%2C%20left%3A%20pos.left%20-%20b%2C%20width%3A%20pos.width%20%2B%20b%2C%20height%3A%20b%20%7D)%3Bself.elements.left.css(%7B%20top%3A%20top%20-%20b%2C%20left%3A%20Math.max(0%2C%20pos.left%20-%20b)%2C%20width%3A%20b%2C%20height%3A%20pos.height%20%2B%20b%20%7D)%3Bself.elements.right.css(%7B%20top%3A%20top%20-%20b%2C%20left%3A%20pos.left%20%2B%20pos.width%2C%20width%3A%20b%2C%20height%3A%20pos.height%20%2B%20(b%20*%202)%20%7D)%3B%7D%20else%20%7Bself.elements.box.css(%7Btop%3A%20top%2Cleft%3A%20pos.left%2Cwidth%3A%20pos.width%2Cheight%3A%20pos.height%7D)%3B%7D%7Dfunction%20clickHandler(e)%20%7Bif%20(e.target.className.indexOf(self.opts.namespace)%20!%3D%3D%20-1%20%7C%7C%20e.target.className.indexOf(self.opts.classExclude)%20!%3D%3D%20-1)%20%7Breturn%3B%7Dif%20(!self.opts.realtime)%20%7Bdraw(e)%3B%7De.preventDefault()%3Bself.opts.onClick(pub.element)%3Breturn%20false%3B%7Dpub.start%20%3D%20function%20()%20%7BinitStylesheet()%3Bpub.stop()%3Bif%20(self.active%20!%3D%3D%20true)%20%7Bself.active%20%3D%20true%3BcreateOutlineElements()%3BjQuery('body').bind('keyup.'%20%2B%20self.opts.namespace%2C%20stopOnEscape)%3Bif%20(self.opts.onClick)%20%7BsetTimeout(function%20()%20%7BjQuery('body').bind('click.'%20%2B%20self.opts.namespace%2C%20clickHandler)%3B%7D%2C%2050)%3B%7Dif%20(self.opts.realtime)%20%7BjQuery('body').bind('mousemove.'%20%2B%20self.opts.namespace%2C%20draw)%3B%7D%7D%7D%3Bpub.stop%20%3D%20function%20()%20%7Bif%20(self.singleHighlightLoop%20!%3D%20null)%7BclearInterval(self.singleHighlightLoop)%3Bself.singleHighlightLoop%20%3D%20null%3B%7Dself.singleHighlightActive%20%3D%20false%3Bself.active%20%3D%20false%3BremoveOutlineElements()%3Bselecting%20%3D%20false%3BjQuery('body').unbind('mousemove.'%20%2B%20self.opts.namespace).unbind('keyup.'%20%2B%20self.opts.namespace).unbind('click.'%20%2B%20self.opts.namespace)%3B%7D%3Bpub.highlight%20%3D%20function(element)%7BinitStylesheet()%3Bpub.stop()%3Bself.singleHighlightActive%20%3D%20true%3BcreateOutlineElements()%3Bvar%20pseudoElement%20%3D%20%7Btarget%3A%20element%7D%3Bself.singleHighlightLoop%20%3D%20setInterval(function()%7Bdraw(pseudoElement)%3B%7D%2C%2050)%3B%7D%3Breturn%20pub%3B%7D%3Bif%20(document.getElementById(%22ctg_multiple_autoclick_initialized%22)%20%3D%3D%20null)%7Balert(%22Click%20on%20an%20element%20to%20set%20auto%20click%20location.%20When%20you%20are%20done%20selecting%2C%20press%20ESC%20to%20start%20auto%20click.%20More%20Info%3A%20creativetechguy.com%2Futilities%2Fbookmarklets%22)%3Bvar%20div%20%3D%20document.createElement(%22div%22)%3Bdiv.style.display%20%3D%20%22none%22%3Bdiv.id%20%3D%20%22ctg_multiple_autoclick_initialized%22%3Bdocument.body.appendChild(div)%3Bvar%20domSelector%20%3D%20DomOutline(%7BonClick%3A%20elementClicked%2C%20realtime%3A%20true%7D)%3BdomSelector.start()%3Bvar%20selecting%20%3D%20true%3Bfunction%20elementClicked(e)%7Bvar%20cps%20%3D%20prompt(%22Clicks%20per%20second%3A%22)%3Bif%20(cps%20%3D%3D%20null)return%3Bif%20(isNaN(cps))return%3Bif%20(cps%20%3E%201000)cps%20%3D%201000%3Bvar%20delay%20%3D%201000%2Fcps%3BsetInterval(function()%7Bif%20(selecting)return%3Bif%20(e)e.click()%3B%7D%2C%20delay)%3B%7D%7D%7D)(jQuery.noConflict(true))%7Dvar%20s%3Ddocument.createElement(%22script%22)%3Bs.src%3D%22https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fjquery%2F1.7.1%2Fjquery.min.js%22%3Bif(s.addEventListener)%7Bs.addEventListener(%22load%22%2Ccallback%2Cfalse)%7Delse%20if(s.readyState)%7Bs.onreadystatechange%3Dcallback%7Ddocument.body.appendChild(s)%3B%7D)()

Upvotes: 0

Related Questions