-
Notifications
You must be signed in to change notification settings - Fork 1
/
GreaseTamperMonkey.js
174 lines (148 loc) · 4.67 KB
/
GreaseTamperMonkey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// ==UserScript==
// @name Cookies and Tears
// @version 0.0.2
// @author HighTechLowIQ
// @description Beats The Spiffing Brit at Cookie Clicker
// @grant none
// @match https://orteil.dashnet.org/cookieclicker/
// @match http://orteil.dashnet.org/cookieclicker/
// ==/UserScript==
var cheatsOn = false;
var variables;
var cookieFunction;
var goldenCookieFunction;
var upgradeFunction;
var futureCookiesFunction;
var timerFunction;
function injectFunction(functionToInject, doExecute) {
var script = document.createElement("script");
script.type = "application/javascript";
if (doExecute) {
script.textContent = "(" + functionToInject + ")();";
} else {
script.textContent = functionToInject;
}
document.body.appendChild(script);
return script;
}
function getCookiesInXSeconds(seconds) {
return Math.floor(Game.cookies) + Math.floor(Game.cookiesPs * seconds);
}
function getToggleCheatsColor() {
var toggleCheatsColor;
if (cheatsOn) {
toggleCheatsColor = "green";
} else {
toggleCheatsColor = "red";
}
return toggleCheatsColor;
}
function getToggleCheatsText() {
var toggleCheatsText;
if (cheatsOn) {
toggleCheatsText = "Disable Cheats";
} else {
toggleCheatsText = "Enable Cheats";
}
return toggleCheatsText;
}
function clickCookie() {
Game.ClickCookie();
}
function buyUpgrades() {
var upgradesAvailable = Game.UpgradesInStore;
// Always buy upgrades - start at the last upgrade, and go down to the first. If you can afford an upgrade in 10 seconds, don't buy anything
for (var i = upgradesAvailable.length - 1; i >= 0; i--) {
var futureCookies = getCookiesInXSeconds(30);
if (upgradesAvailable[i].canBuy()) {
upgradesAvailable[i].buy();
} else if (upgradesAvailable[i].getPrice() <= futureCookies) {
console.log("Will have enough cookies for " + upgradesAvailable[i].name + " in 30 seconds.");
return;
}
}
var buildingsAvailable = Game.ObjectsById;
// The last building is the best
for (var i = buildingsAvailable.length - 1; i >= 0; i--) {
if (buildingsAvailable[i].locked == 0) {
// Continuously buy the building if we have enough money
while (buildingsAvailable[i].price <= Game.cookies) {
buildingsAvailable[i].buy();
}
// Check if we'll have enough for the next building in 10 seconds. If not, move on to the next building
var futureCookies = getCookiesInXSeconds(15);
if (buildingsAvailable[i].price <= futureCookies) {
console.log("Will have enough cookies for " + buildingsAvailable[i].name + " in 15 seconds.");
return;
}
}
}
}
function clickGoldenCookie() {
Game.shimmers.forEach(
function(shimmer) {
if (shimmer.type == "golden") {
console.log("Clicking Golden Cookie!");
shimmer.pop();
}
}
);
}
function startTimers() {
// Click the cookie every 4ms
clickTimer = setInterval(function() {
clickCookie();
}, 4);
// Buy relevant upgrades every tenth of a second
upgradeTimer = setInterval(function() {
buyUpgrades();
}, 100);
// Click any golden cookies every half second
goldenCookieTimer = setInterval(function() {
clickGoldenCookie();
}, 500);
}
function stopTimers() {
clearInterval(clickTimer);
clearInterval(upgradeTimer);
clearInterval(goldenCookieTimer);
}
function injectTimers() {
if (cheatsOn) {
if (timerFunction) {
timerFunction.remove();
}
timerFunction = injectFunction(startTimers, true);
} else {
timerFunction.remove();
timerFunction = injectFunction(stopTimers, true);
}
}
function toggleCheats() {
console.log("Toggling cheats from " + cheatsOn + " to " + !cheatsOn + ".");
cheatsOn = !cheatsOn;
const toggleCheats = document.querySelector('#toggleCheats');
toggleCheats.style.color = getToggleCheatsColor();
toggleCheats.textContent = getToggleCheatsText();
injectTimers();
}
function addButton() {
const topBar = document.querySelector('#topBar');
let toggleElement = document.createElement('div');
toggleElement.id = "toggleCheats";
toggleElement.style.color = "red";
toggleElement.style.cursor = "pointer";
toggleElement.textContent = getToggleCheatsText();
toggleElement.onclick = toggleCheats;
topBar.appendChild(toggleElement);
}
function runScript() {
console.log("Running!");
addButton();
variables = injectFunction("var clickTimer; var upgradeTimer; var goldenCookieTimer;", false);
cookieFunction = injectFunction(clickCookie, false);
goldenCookieFunction = injectFunction(clickGoldenCookie, false);
upgradeFunction = injectFunction(buyUpgrades, false);
futureCookiesFunction = injectFunction(getCookiesInXSeconds, false);
}
runScript();