Greasy Fork

MoshMages Auto Clicker

Auto click several stuff

当前为 2020-12-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         MoshMages Auto Clicker
// @version      1.3
// @description  Auto click several stuff
// @author       [email protected]
// @match        http://orteil.dashnet.org/cookieclicker/*
// @grant        none
// @namespace https://gist.github.com/moshmage/792ac013a8fb0b23d39f491261ebdb90
// ==/UserScript==
(() => {

  const options = {
    enable: true,
    autoBuyBuildings: true,
    autoBuyUpgrades: true,

    autoClickShimmers: true,
    disableBuyIfBuffs: true,

    randomUpgradeChance: .01,
    randomBuildingChance: .99,

    tickSpeedMs: 50,
    clickCookie: true,
  }

  const bigCookie = document.querySelector(`#bigCookie`);
  const centerSection = document.querySelector(`#centerArea`);
  const game = document.querySelector(`#game`);

  const tickAction = () => {
    if (!options.enable) return;

    if (options.clickCookie)
      bigCookie.click()

    const buyThing = (selector) => {
      const things = document.querySelectorAll(selector);
      if (things.length)
        things[things.length - 1].click();
    }

    if (options.autoClickShimmers) {
      const shimmers = document.querySelectorAll(`#shimmers *`);
      shimmers.forEach(el => el.click());
    }

    const buffs = document.querySelectorAll(`#buffs *`).length;

    if (buffs && options.disableBuyIfBuffs)
      return;

    if (options.autoBuyUpgrades && Math.random() >= options.randomUpgradeChance)
      buyThing(`#upgrades .enabled`);

    if (options.autoBuyBuildings && Math.random() >= options.randomBuildingChance)
      buyThing(`#products .enabled`);

    setTimeout(() => tickAction(), options.tickSpeedMs);
  }

  const onoff = (pointer) => pointer && `ON` || `OFF`;

  const makeListingOption = (name, desc, id, key, cb) => {
    const listing = document.createElement(`div`);
    listing.classList.add(`listing`);

    const option = document.createElement(`a`);
    option.classList.add(`option`);
    option.setAttribute(`id`, id);
    option.textContent = `${name} ${onoff(options[key])}`;

    option.addEventListener(`click`, (ev) => {
      options[key] = !options[key];
      option.textContent = `${name} ${onoff(options[key])}`;
      if (cb) cb(ev, key);
    });

    const label = document.createElement(`label`);
    label.textContent = desc && `(${desc})` || ``;

    listing.appendChild(option);
    if (desc) listing.appendChild(label);

    return listing;
  }

  const makeSlider = (name, min, max, increment, key) => {
    const listing = document.createElement(`div`);
    listing.classList.add(`listing`);

    const slider = document.createElement(`div`);
    slider.classList.add(`sliderBox`);

    const label = document.createElement(`div`);
    label.style.float = `left`;
    label.textContent = name;

    const value = document.createElement(`div`);
    value.style.float = `right`;
    value.textContent = options[key];

    const option = document.createElement(`input`);
    option.setAttribute(`type`, `range`);
    option.setAttribute(`min`, min);
    option.setAttribute(`max`, max);
    option.setAttribute(`step`, increment);
    option.style.clear = `both`;
    option.value = options[key];

    option.addEventListener(`change`, (ev) => {
      options[key] = +ev.target.value;
      value.textContent = options[key];
    });

    slider.appendChild(label);
    slider.appendChild(value);
    slider.appendChild(option);
    listing.appendChild(slider);

    return listing;
  }

  const makeTitle = (title) => {
    const element = document.createElement(`div`)
    element.classList.add(`title`);
    element.textContent = title;

    return element;
  }

  const buildUI = () => {

    const modMenu = document.createElement(`div`);
    modMenu.setAttribute(`id`, `mod-menu`);

    const closeModMenu = document.createElement(`div`);
    closeModMenu.classList.add(`close`, `menuClose`, `modClose`);
    closeModMenu.textContent = `x`;

    modMenu.appendChild(closeModMenu);

    const section = document.createElement(`div`);
    section.classList.add(`section`)
    section.textContent = `Mods`;

    modMenu.appendChild(section);

    const subsection = document.createElement(`div`);
    subsection.classList.add(`subsection`);

    subsection.appendChild(makeTitle(`Auto click Madness`));

    const startTickAction = () => {
      if (options.enable) tickAction();
    }

    subsection.appendChild(makeListingOption(`Auto click`, `enable/disable auto click abilities`, `enable-madness`, `enable`, function() { startTickAction() }))

    subsection.appendChild(makeTitle(`Clicks`));
    subsection.appendChild(makeListingOption(`Click cookie`, `click the big cookie`, `enable-cookie-click`, `clickCookie`));
    subsection.appendChild(makeSlider(`Tick speed (ms)`, 50, 1000, 10, `tickSpeedMs`));

    subsection.appendChild(makeTitle(`Upgrades`));
    subsection.appendChild(makeListingOption(`Buy upgrades`, `higher number = lower chances`, `auto-upgrade`, `autoBuyUpgrades`));
    subsection.appendChild(makeSlider(`Chance`, 0, .999, .001, `randomUpgradeChance`));

    subsection.appendChild(makeTitle(`Products`));
    subsection.appendChild(makeListingOption(`Buy products`, `higher number = lower chances`, `auto-build`, `autoBuyBuildings`));
    subsection.appendChild(makeSlider(`Chance`, 0, .999, .001, `randomBuildingChance`));

    subsection.appendChild(makeTitle(`Shimmers and Buffs`))
    subsection.appendChild(makeListingOption(`Click shimmers`, ``, `auto-shimmer`, `autoClickShimmers`));
    subsection.appendChild(makeListingOption(`Wait for buff end`, ``, `auto-timeout`, `disableBuyIfBuffs`));


    modMenu.appendChild(subsection);
    modMenu.style.display = `none`;
    modMenu.style.position = `absolute`;
    modMenu.style.zIndex = `1`;
    modMenu.style.left = `0`;
    modMenu.style.right = `0`;
    modMenu.style.top = `0`;
    modMenu.style.bottom = `0`;

    const modMenuOption = document.createElement(`div`);
    modMenuOption.classList.add(`roundedPanel`);
    modMenuOption.setAttribute(`id`, `ascendNumber`);
    modMenuOption.style.display = `block`;
    modMenuOption.style.left = `6rem`;
    modMenuOption.style.top = `4.5rem`;
    modMenuOption.style.right = `auto`;
    modMenuOption.style.zIndex = `1000`;
    modMenuOption.style.cursor = `pointer`;
    modMenuOption.textContent = `Mod`;

    const _openModMenu = () => {
      const close = centerSection.querySelector(`.close:not(.modClose)`);
      if (close) close.click();

      game.classList.add(`onMenu`);
      modMenu.style.display = `block`;
    }

    const _closeModMenu = () => {
      game.classList.remove(`onMenu`);
      modMenu.style.display = `none`;
    }

    modMenuOption.addEventListener(`click`, () => {
      if (modMenu.style.display === `block`)
        _closeModMenu();
      else _openModMenu();
    });

    closeModMenu.addEventListener(`click`, _closeModMenu);

    const comments = document.querySelector(`#comments`);
    comments.appendChild(modMenuOption);
    comments
      .querySelectorAll(`.button`)
      .forEach(el => el.addEventListener(`click`, () => modMenu.style.display = `none`));

    centerSection.appendChild(modMenu);

    modMenuOption.click();
  }

  setTimeout(buildUI, 500);

})();