-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpanSh_VR_commands.user.js
325 lines (270 loc) · 7.74 KB
/
SpanSh_VR_commands.user.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// ==UserScript==
// @name SpanSh VR commands
// @namespace armeagle.nl
// @include https://www.spansh.co.uk/*
// @version 1.1
// @grant none
// ==/UserScript==
var Constants = {
accessKey: {
focusCurrent: ".",
copyCurrent: "c",
markCurrent: "!",
goPrevious: "[",
goNext: "]",
},
resultsSelector: "h4.card-title ~ .card-body", //"#results",
baseSystemRowSelector: "table tbody tr",
currentSystemClass: "vr-current",
nc: "no comma"
};
function VrCommands() {}
VrCommands.prototype.initialize = function() {
this.addHotkeys();
this.addStyle();
console.log("VrCommands is initialized");
}
/**
* @return Node results container
*/
VrCommands.prototype.getContainer = function() {
return document.querySelector(Constants.resultsSelector);
}
/**
* @return Node current active line or first if none was active
*/
VrCommands.prototype.findCurrentSystemLine = function(default_to_first) {
var cont = this.getContainer();
var cur = cont.querySelector(Constants.baseSystemRowSelector + "." + Constants.currentSystemClass);
if (!cur && undefined !== default_to_first ) {
// or select first
cur = cont.querySelector(Constants.baseSystemRowSelector);
this.markSystemLineCurrent(cur);
}
return cur;
}
/**
* Mark node as current, remove from all others
* @param node Node
*/
VrCommands.prototype.markSystemLineCurrent = function(node) {
var cont = this.getContainer();
var cur;
// Bit ugly; first remove from all, then set for node.
while ((cur = cont.querySelector(Constants.baseSystemRowSelector + "." + Constants.currentSystemClass))) {
cur.classList.remove(Constants.currentSystemClass);
}
if (node) {
node.classList.add(Constants.currentSystemClass);
}
}
/**
* @return string Current system name
*/
VrCommands.prototype.getCurrentSystemName = function() {
var cur = this.findCurrentSystemLine();
if (!cur) {
return "";
}
var systemTd = cur.querySelector("td:nth-child(2)");
if (!systemTd) {
return "";
}
// Sometimes there are empty text nodes before the system name.
// Find the first non-empty textnode and return the trimmed contents.
for(let node of systemTd.childNodes) {
if (node.nodeType !== Node.TEXT_NODE) {
continue;
}
let textContents = node.nodeValue.trim();
if (textContents) {
return textContents;
}
}
return "";
}
/**
* @return boolean Whether the current system has a neutron star
*/
VrCommands.prototype.hasCurrentSystemNeutronStar = function() {
var cur = this.findCurrentSystemLine();
if (!cur) {
return false;
}
var nsTdText = cur.querySelector("td:nth-child(5)").textContent;
if (nsTdText.toLowerCase() === "yes") {
return true;
}
return false;
}
/**
* @return int The number of estimated jumps to reach this system
*/
VrCommands.prototype.getEstimatedJumps = function() {
var cur = this.findCurrentSystemLine();
if (!cur) {
return false;
}
var nsTdText = cur.querySelector("td:nth-child(6)").textContent;
return nsTdText;
}
/**
* Add hotkeys to document by defining elements with "accesskey" attribute and a click
* event listener. These keys are defined in the Constants class.
*/
VrCommands.prototype.addHotkeys = function() {
if (this.hotkeys) {
return;
}
this.hotkeys = document.createElement("div");
this.hotkeys.setAttribute("id", "hotkeys");
var label = document.createElement("label");
label.textContent = "Hotkeys";
this.hotkeys.appendChild(label);
var buttons = document.createElement("div");
buttons.classList.add("buttons-list");
this.hotkeys.appendChild(buttons);
for (var property in Constants.accessKey) {
if (Constants.accessKey.hasOwnProperty(property)) {
var accessKey = Constants.accessKey[property];
var keyAnchor = document.createElement("button");
keyAnchor.setAttribute("accesskey", accessKey);
keyAnchor.textContent = property +": "+ accessKey;
keyAnchor.classList.add("btn", "btn-default");
keyAnchor.setAttribute("data-property", property);
keyAnchor.addEventListener("click", function(event) {
// Call this.hande`property`(), i.e. this.prototype.handleGoPrevious()
var eventProperty = event.target.getAttribute("data-property");
//console.log("call", eventProperty, this);
this["handle" + this.ucFirst(eventProperty)].apply(this);
}.bind(this));
buttons.appendChild(keyAnchor);
}
}
document.body.appendChild(this.hotkeys);
}
/**
* Handling the different hotkeys
*/
VrCommands.prototype.handleFocusCurrent = function() {
var systemName = this.getCurrentSystemName();
if (!systemName) {
message = "System could not be found";
} else {
message = "Current system is "+ systemName;
}
this.speak(message);
}
VrCommands.prototype.handleCopyCurrent = function() {
var cur = this.findCurrentSystemLine();
var message = "";
if (!cur) {
message = "Could not copy, no system available";
} else {
var systemName = this.getCurrentSystemName();
if (!systemName) {
message = "System could not be found";
} else {
cur.querySelector("td button.clipboard-button").click();
message = systemName + " copied to clipboard";
var isNeutronStar = this.hasCurrentSystemNeutronStar();
if (isNeutronStar) {
message += ". Neutron Star is available";
} else {
message += ". No Neutron Star detected";
}
var estimatedJumps = this.getEstimatedJumps();
if (estimatedJumps) {
message += ". This requires about " + estimatedJumps + " jump" + (estimatedJumps > 1 ? 's' : '');
}
}
}
this.speak(message);
}
VrCommands.prototype.handleMarkCurrent = function() {
var cur = this.findCurrentSystemLine(true);
if (!cur) {
return;
}
this.markSystemLineCurrent(cur);
let checkbox = cur.querySelector("td:first-child input[type=checkbox]");
checkbox.click();
}
VrCommands.prototype.handleGoNext = function() {
var cur = this.findCurrentSystemLine();
if (!cur) {
cur = this.findCurrentSystemLine(true);
if (cur) {
var systemName = this.getCurrentSystemName();
this.speak("Selected first system " + systemName);
}
return;
}
cur = cur.nextElementSibling;
if (cur) {
this.markSystemLineCurrent(cur);
}
}
VrCommands.prototype.handleGoPrevious = function() {
var cur = this.findCurrentSystemLine();
if (!cur) {
cur = this.findCurrentSystemLine(true);
if (cur) {
var systemName = this.getCurrentSystemName();
this.speak("Selected first system " + systemName);
}
return;
}
cur = cur.previousElementSibling;
if (cur) {
this.markSystemLineCurrent(cur);
}
}
/**
* Add styling/css
*/
VrCommands.prototype.addStyle = function() {
var css = document.createElement("style");
css.setAttribute("type", "text/css");
css.textContent = "\
.vr-current, .vr-current > td { background-color: #ddaa00 !important; } \
#hotkeys { margin: 20px; } \
#hotkeys .buttons-list { max-width: 700px; display: flex; flex-flow: row wrap; justify-content: flex-start; } \
#hotkeys .buttons-list button { flex: 0 1 15%; margin: 10px} \
";
document.body.appendChild(css);
}
/*************************************************************************
*
* Helper methods
*
*************************************************************************/
/**
* Use the Speech Synthesis API to speak out text
* @param text string
* @param callback Closure called on ending of synthesis or error (with parameter).
*/
VrCommands.prototype.speak = function(text, callback) {
var u = new SpeechSynthesisUtterance();
u.text = text;
u.lang = "en-US";
u.onend = function () {
if (callback) {
callback();
}
};
u.onerror = function (e) {
if (callback) {
callback(e);
}
};
speechSynthesis.speak(u);
}
/**
* @param string string Make first letter upper case
*/
VrCommands.prototype.ucFirst = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
var c = new VrCommands();
c.initialize();