-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
259 lines (203 loc) · 7.77 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Global modules.
var gui = require('nw.gui');
// Config.
var config = require("./config.json");
var reloadAfterMinutes = config.reloadAfterMinutes || 60; // Minutes in which this bot should be automatically reloaded (NOTE: Settings WILL be persisted!)
// Hide main window, show dev tools and ensure that application is closed if this window gets closed.
var mainWin = gui.Window.get();
mainWin.hide();
mainWin.showDevTools();
setInterval(function() {
if (!mainWin.isDevToolsOpen()) process.exit();
}, 500);
// TODO: New bot loader under construction.
var Loader = require("./loader.js");
var loader = new Loader(config.roomURL, config.email, config.password);
loader.on("loaded", function() {
// TODO: Setup bot now.
// TODO: Use loader.getPlugWin() to get reference to plug.dj window and document objects.
});
loader.load();
// Plug window variables.
var plugDoc, plugWin, $;
// Bot instance.
var bot;
// Keep track of ALL intervals/timers so we can clear them all out in one place.
var intervals = {
roomMonitor: 0,
watchAPI: 0
}, timeouts = {
waitAPI: 0,
reload: 0,
validateRoom: 0
};
/*******************************
* LOAD PLUG.DJ AND LOGIN USER *
*******************************/
var win, restarted = false; // TODO: WILL NEED TO EVENTUALLY NEED A CLEANER WAY TO MAINTAIN STATE after everything is restructured.
var initWin = function() {
// Force close of existing window, if applicable.
if (typeof win != "undefined") win.close(true);
// Open a new window.
win = gui.Window.get(
window.open(config.roomURL)
);
win.hide();
win.on("loaded", function() {
plugWin = win.window;
plugDoc = plugWin.document;
$ = plugWin.$; // Utilize plug.dj's reference to jQuery.
// Attempt to login, if available.
var loginButton = plugDoc.querySelector(".header .existing button");
if (loginButton) {
console.log("[Autoloader] Attempting to login to plug.dj...");
loginButton.click();
// Populate login fields and submit form.
plugDoc.querySelector("#email").value = config.email;
plugDoc.querySelector("#password").value = config.password;
plugDoc.querySelector("#submit").click();
}
// Initialize bot loader which monitors room URL, waits for the API and loads the bot when ready.
initBotLoader();
});
};
initWin();
/***************************************************
* BOT LOADER MECHANISM (ADOPTED FROM USER SCRIPT) *
***************************************************/
var initBotLoader = function() {
console.log("[Autoloader] Bot loader initialized.");
// Clear out all intervals and timeouts now (in case this function has already been called).
for(var i in intervals) clearInterval(intervals[i]);
for(var t in timeouts) clearTimeout(timeouts[t]);
// Set interval that monitors to ensure that we're always logged into the correct room.
if (!validateRoom()) return;
intervals.roomMonitor = setInterval(function() {
if (!validateRoom()) {
clearInterval(intervals.roomMonitor);
}
}, 1000);
// Watch and wait for the API to become available...
var startedWatching = (new Date()).getTime();
intervals.watchAPI = setInterval(function() {
if (getAPI()) {
// Found API. Give it a few seconds to completely load...
clearInterval(intervals.watchAPI);
timeouts.waitAPI = setTimeout(function() {
// Get previously stored configuration stored specifically for reload.
var storedConfig = getSettings("storedConfig");
// Load any persisted settings while applying current override configuration, since this is a page reload.
console.log("Restarted: " + restarted);
var configOverride = {};
if (storedConfig) {
console.log("Found config:");
console.log(storedConfig);
// Reset all configuration information EXCEPT for commands.
for(var k in storedConfig) {
if (k == "commands") continue;
configOverride[k] = storedConfig[k];
}
// Retain "restarted" setting from current scope.
configOverride.restarted = restarted;
console.log("Saved restarted");
}
// Now load the bot!
// TODO: Need to convert the rest of the bot to utilize node's module pattern!
try {
// Load bot module...
bot = require("./bot.js");
// ... and setup global instance in plug.dj window.
plugWin.ChunkBot = bot;
// Initialize bot in OLD architecture by passing through scope for various needed objects.
bot.init($, console, getAPI(), configOverride);
// Administrative functionality, given access to the bot itself for manipulation by commands.
// TODO: This functionality will eventually all be properly packaged up... eventually.
bot.restart = reloadBrowser;
bot.die = function() {
process.exit();
};
// Show load success.
console.log("[Autoloader] Bot is now loaded! To show window, type 'win.show()' in the console and 'win.hide()' to hide again.");
console.log("[Autoloader] To kill the bot, type 'bot.die()'. To restart, type 'bot.restart()'.");
} catch(e) {
console.log(e);
}
// Setup reload timeout.
if (reloadAfterMinutes > 0) {
timeouts.reload = setTimeout(function() {
reloadBrowser();
}, reloadAfterMinutes * 1000 * 60);
}
}, 3000);
} else {
// If we've been watching for 60 seconds and still no joy, let's try reloading again!
var now = (new Date()).getTime();
var beenWatching = (now - startedWatching) / 1000;
var threshold = 60;
var remaining = Math.floor(threshold - beenWatching);
var warnAt = 20; // remaining.
var message = "Waiting for API...";
if (remaining <= warnAt) message += " reloading to try again in " + remaining + " seconds.";
console.log(message);
if (beenWatching >= threshold) reloadBrowser();
}
}, 1000);
};
/****************************
* HELPER FUNCTIONS AND ETC *
****************************/
// Just a shortcut to help with refactoring from old user script.
var getAPI = function() {
if (typeof plugWin != "undefined" && plugWin.API && plugWin.API.getUser() && plugWin.API.getUser().username) return plugWin.API;
return false;
};
// Small abstraction for persisting configuration after page load.
var settings = {}; // TODO: Storing this way since localStorage fails to work since node-webkit is utilizing data URL's for pages.
var storeSettings = function(name, values) {
try {
// In rare circumstances (changing between rooms), stringify below may result in "Converting circular structure to JSON".
settings[name] = values;
} catch (e) {}
};
var getSettings = function(name) {
return settings[name];
};
// Allows reloading browser without losing settings.
var reloadBrowser = function() {
// Store current ChunkBot configuration specifically for reload.
if (bot) storeSettings("storedConfig", bot.config);
// Perform reload.
restarted = true;
win.close(true);
initWin();
};
// Indicates that the bot is located at the correct room URL.
var isValidRoom = function() {
return parseRoom(config.roomURL) == parseRoom(getCurrentUrl());
};
// Current URL of plug.dj window.
var getCurrentUrl = function() {
if (typeof plugWin.location.href == "undefined") return "";
return plugWin.location.href;
};
// Pulls out the lowercase name of the room based on the [presumably valid] room URL.
var parseRoom = function(url) {
// Break URL into segments and return first non-empty segment.
var segments = url.split("/");
var segment;
while ((segment = segments.pop()) !== undefined) {
if (segment != "") return segment.toLowerCase();
}
return "";
};
// Centralized room validation and redirect.
var validateRoom = function() {
if (!isValidRoom()) {
console.log("[Autoloader] Current room '" + parseRoom(getCurrentUrl()) + "' is not the properly configured room of '" + parseRoom(config.roomURL) + "', redirecting in 10 seconds...");
timeouts.validateRoom = setTimeout(function() {
reloadBrowser();
}, 10000);
return false;
}
return true;
};