-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem-explorer-filter.user.js
More file actions
474 lines (411 loc) · 16.6 KB
/
Copy pathsystem-explorer-filter.user.js
File metadata and controls
474 lines (411 loc) · 16.6 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// ==UserScript==
// @name Outer Empires 2 - System Explorer Filter
// @namespace outer-empires-2
// @version 1.9
// @description Filter/recolor items in the System Explorer panel with an inline toggle button + Rob alert (flicker-free) + auto-toggle in/out of combat. Color/Alert lists override the filter.
// @match https://game.dev.outerempires.net/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// List of object names to HIDE (case-insensitive, partial match).
const FILTER_LIST = [
'Rescue Shuttle','Freighter','Scavenger','Factory Ship','Utility Tug',
'Hulkbreaker','Service Shuttle','Turret Eater','Hauler','Survey Shuttle',
'Rock Chipper','Assault Frigate','Prime Mover','Support Ops Rig',
'Heavy Mining Rig','Merchantman','Rock Hopper','Explorer',
'Kinetic Racer','Salvage Rig','Command Cruiser','Hostile Environment Mining Rig',
'Riot Frigate','Strategic Mover','Infiltrator','Destroyer','Fighter Bomber',
'Corvette','Cruiser','Frigate','Clipper','Screen','Battleship','Depot Ship','Escort Carrier',
'Heavy Shuttle','Interceptor','Scout','Sentry','Gravity Minelayer','Patrol','Hulkbreaker',
];
// List of object names to RECOLOR (not hidden). Matches here override FILTER_LIST.
const COLOR_LIST = [
{ match: 'Hulk ', color: '#888' },
{ match: 'AOA', color: '#FF00FF' },
{ match: 'NEC', color: '#008000' },
{ match: 'liu langhan', color: '#008000' },
];
// Alerts. `persistent: true` means the buzz repeats until the popup is clicked.
// `interval` is the buzz repeat period in ms (default 2000).
// Matches here override FILTER_LIST (so alerted items are never hidden).
const ALERT_LIST = [
{ match: 'AOA', persistent: true, interval: 1500 },
{ match: 'liu langhan', persistent: false },
{ match: 'Hulk ', persistent: false },
];
let filterEnabled = true;
let lastInstanceState = null; // null = unknown, true = in instance, false = out
const BTN_ID = 'oe2-filter-toggle';
const seenAlerts = new Set();
// Track active persistent alerts: name -> { timer, note }
const activePersistent = new Map();
// ---------- Combat / Instance Detection ----------
function isInInstance() {
return !!document.getElementById('ui-exit-instance');
}
function checkInstanceStateAndAutoToggle() {
const inInstance = isInInstance();
if (inInstance === lastInstanceState) return false;
const previousState = lastInstanceState;
lastInstanceState = inInstance;
if (previousState === null) return false;
const desired = !inInstance;
if (filterEnabled !== desired) {
filterEnabled = desired;
const btn = document.getElementById(BTN_ID);
if (btn) styleButton(btn);
return true;
}
return false;
}
// ---------- Toggle Button ----------
function styleButton(btn) {
const onColor = '#1e90ff';
const offColor = '#888';
Object.assign(btn.style, {
cursor: 'pointer',
userSelect: 'none',
color: filterEnabled ? onColor : offColor,
transition: 'color 0.15s, text-shadow 0.15s',
textShadow: filterEnabled ? '0 0 4px rgba(30,144,255,0.6)' : 'none',
});
btn.title = 'Ship filter: ' + (filterEnabled ? 'ON' : 'OFF') +
(isInInstance() ? ' (in instance)' : '');
if (!btn.dataset.hoverBound) {
btn.dataset.hoverBound = '1';
btn.addEventListener('mouseenter', () => {
btn.style.textShadow = '0 0 6px rgba(255,255,255,0.7)';
});
btn.addEventListener('mouseleave', () => {
btn.style.textShadow = filterEnabled
? '0 0 4px rgba(30,144,255,0.6)'
: 'none';
});
}
}
function ensureButton() {
const header = document.getElementById('SystemExplorer_Header');
if (!header) return;
if (document.getElementById(BTN_ID)) return;
const walker = document.createTreeWalker(header, NodeFilter.SHOW_TEXT, null);
let textNode = null;
while (walker.nextNode()) {
if (/\bSYSTEM\b/.test(walker.currentNode.nodeValue)) {
textNode = walker.currentNode;
break;
}
}
if (!textNode) return;
const match = textNode.nodeValue.match(/^([\s\S]*?)\bS(YSTEM\b[\s\S]*)$/);
if (!match) return;
const parent = textNode.parentNode;
const beforeNode = document.createTextNode(match[1]);
const toggleSpan = document.createElement('span');
toggleSpan.id = BTN_ID;
toggleSpan.textContent = 'S';
styleButton(toggleSpan);
toggleSpan.addEventListener('click', (e) => {
e.stopPropagation();
filterEnabled = !filterEnabled;
styleButton(toggleSpan);
applyFilter();
});
const afterNode = document.createTextNode(match[2]);
parent.insertBefore(beforeNode, textNode);
parent.insertBefore(toggleSpan, textNode);
parent.insertBefore(afterNode, textNode);
parent.removeChild(textNode);
}
// ---------- Asterisk Sound (Web Audio API) ----------
let audioCtx = null;
function getAudioCtx() {
if (!audioCtx) {
try {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
} catch (e) {
console.warn('AudioContext unavailable', e);
}
}
return audioCtx;
}
function playAsterisk() {
const ctx = getAudioCtx();
if (!ctx) return;
if (ctx.state === 'suspended') ctx.resume();
const now = ctx.currentTime;
const tone = (freq, start, dur) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now + start);
gain.gain.setValueAtTime(0.0001, now + start);
gain.gain.exponentialRampToValueAtTime(0.35, now + start + 0.01);
gain.gain.exponentialRampToValueAtTime(0.0001, now + start + dur);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now + start);
osc.stop(now + start + dur + 0.05);
};
tone(1320, 0.00, 0.18);
tone(1760, 0.10, 0.25);
}
function playUrgentBuzz() {
const ctx = getAudioCtx();
if (!ctx) return;
if (ctx.state === 'suspended') ctx.resume();
const now = ctx.currentTime;
const tone = (freq, start, dur, vol = 0.4) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'square';
osc.frequency.setValueAtTime(freq, now + start);
gain.gain.setValueAtTime(0.0001, now + start);
gain.gain.exponentialRampToValueAtTime(vol, now + start + 0.01);
gain.gain.exponentialRampToValueAtTime(0.0001, now + start + dur);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(now + start);
osc.stop(now + start + dur + 0.05);
};
tone(880, 0.00, 0.15);
tone(660, 0.18, 0.15);
tone(880, 0.36, 0.20);
}
// ---------- Notification Popup ----------
function showNotification(name, color, opts) {
opts = opts || {};
const c = color || '#FF00FF';
const note = document.createElement('div');
const title = document.createElement('div');
title.textContent = '⚠ ' + name + ' detected!';
note.appendChild(title);
if (opts.persistent) {
const sub = document.createElement('div');
sub.textContent = 'Click to acknowledge & silence';
Object.assign(sub.style, {
fontSize: '11px',
fontWeight: 'normal',
marginTop: '4px',
opacity: '0.8',
});
note.appendChild(sub);
}
Object.assign(note.style, {
position: 'fixed',
top: '175px',
left: '50%',
transform: 'translateX(-50%)',
zIndex: 999999,
padding: '12px 18px',
background: 'rgba(20,20,30,0.95)',
color: c,
border: '2px solid ' + c,
borderRadius: '6px',
fontFamily: 'sans-serif',
fontSize: '14px',
fontWeight: 'bold',
boxShadow: '0 0 12px ' + c,
cursor: 'pointer',
transition: 'opacity 0.4s ease',
opacity: '0',
textAlign: 'center',
});
if (opts.persistent) {
note.animate(
[
{ boxShadow: '0 0 8px ' + c },
{ boxShadow: '0 0 22px ' + c },
{ boxShadow: '0 0 8px ' + c },
],
{ duration: 1000, iterations: Infinity }
);
}
note.addEventListener('click', () => {
if (typeof opts.onDismiss === 'function') opts.onDismiss();
note.style.opacity = '0';
setTimeout(() => note.remove(), 400);
});
document.body.appendChild(note);
requestAnimationFrame(() => { note.style.opacity = '1'; });
if (!opts.persistent) {
setTimeout(() => {
note.style.opacity = '0';
setTimeout(() => note.remove(), 400);
}, 8000);
}
if (typeof Notification !== 'undefined' && Notification.permission === 'granted') {
try { new Notification('System Explorer Alert', { body: name + ' detected!' }); } catch (e) {}
}
return note;
}
function fireAlert(name) {
const lower = name.toLowerCase();
const cm = colorMatch(lower);
const am = alertMatch(lower);
const color = cm ? cm.color : null;
const persistent = !!(am && am.persistent);
if (persistent) {
if (activePersistent.has(name)) return;
const interval = (am && am.interval) || 2000;
playUrgentBuzz();
const timer = setInterval(() => {
playUrgentBuzz();
}, interval);
const note = showNotification(name, color, {
persistent: true,
onDismiss: () => {
clearInterval(timer);
activePersistent.delete(name);
},
});
activePersistent.set(name, { timer, note });
} else {
showNotification(name, color, { persistent: false });
playAsterisk();
}
}
// ---------- Helpers ----------
function getRawName(itemEl) {
const nameEl = itemEl.querySelector('.SystemExplorer_ObjectName');
return nameEl ? nameEl.textContent.trim() : '';
}
function shouldHide(txt) {
return FILTER_LIST.some(n => {
const lower = n.toLowerCase();
if (!txt.includes(lower)) return false;
// Find what comes after the matched ship type name
const idx = txt.indexOf(lower);
const after = txt.slice(idx + lower.length).trim();
// Hide only if nothing follows (no personal name/tag)
return after === '';
});
}
function colorMatch(txt) {
return COLOR_LIST.find(c => txt.includes(c.match.toLowerCase()));
}
function alertMatch(txt) {
return ALERT_LIST.find(a => txt.includes(a.match.toLowerCase()));
}
// Apply visual state for a single item.
// Color list and Alert list ALWAYS run regardless of filterEnabled.
// A match in COLOR_LIST or ALERT_LIST overrides FILTER_LIST (item won't be hidden).
function processItem(item, triggerAlerts) {
if (!item || !item.querySelector) return;
const nameEl = item.querySelector('.SystemExplorer_ObjectName');
const rawName = nameEl ? nameEl.textContent.trim() : '';
const txt = rawName.toLowerCase();
const cm = colorMatch(txt);
const am = alertMatch(txt);
const protectedFromHide = !!(cm || am);
// Hide ONLY if filter is on, item matches FILTER_LIST, AND it doesn't
// also match a color or alert rule (those take precedence).
if (filterEnabled && !protectedFromHide && shouldHide(txt)) {
if (item.style.display !== 'none') item.style.display = 'none';
item.dataset.oe2Filtered = 'true';
return;
} else if (item.dataset.oe2Filtered === 'true') {
item.style.display = '';
item.dataset.oe2Filtered = 'false';
}
if (!nameEl) return;
// Apply / clear color (always — regardless of filterEnabled).
if (cm) {
if (nameEl.style.color !== cm.color) nameEl.style.color = cm.color;
nameEl.dataset.oe2Colored = 'true';
} else if (nameEl.dataset.oe2Colored === 'true') {
nameEl.style.color = '';
nameEl.dataset.oe2Colored = 'false';
}
// Alerts always fire (regardless of filterEnabled).
if (triggerAlerts && am && !seenAlerts.has(rawName)) {
seenAlerts.add(rawName);
fireAlert(rawName);
}
}
function applyFilter() {
const items = document.querySelectorAll('.SystemExplorer_Item');
const presentAlertKeys = new Set();
items.forEach(item => {
processItem(item, false);
const rawName = getRawName(item);
const txt = rawName.toLowerCase();
const am = alertMatch(txt);
// Alerts always run — even if the item *would* have been hidden,
// because alert matches override the filter and unhide it.
if (am) {
presentAlertKeys.add(rawName);
if (!seenAlerts.has(rawName)) {
seenAlerts.add(rawName);
fireAlert(rawName);
}
}
});
for (const key of Array.from(seenAlerts)) {
if (!presentAlertKeys.has(key)) {
seenAlerts.delete(key);
const active = activePersistent.get(key);
if (active) {
clearInterval(active.timer);
if (active.note && active.note.parentNode) {
active.note.style.opacity = '0';
setTimeout(() => active.note.remove(), 400);
}
activePersistent.delete(key);
}
}
}
}
// ---------- Watch for dynamic re-renders ----------
let debounceTimer = null;
const observer = new MutationObserver((mutations) => {
for (const m of mutations) {
if (!m.addedNodes || m.addedNodes.length === 0) continue;
for (const node of m.addedNodes) {
if (node.nodeType !== 1) continue;
if (node.classList && node.classList.contains('SystemExplorer_Item')) {
processItem(node, false);
} else if (node.querySelectorAll) {
const inner = node.querySelectorAll('.SystemExplorer_Item');
if (inner.length) inner.forEach(it => processItem(it, false));
}
}
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
ensureButton();
checkInstanceStateAndAutoToggle();
applyFilter();
}, 100);
});
if (typeof Notification !== 'undefined' && Notification.permission === 'default') {
try { Notification.requestPermission(); } catch (e) {}
}
const primeAudio = () => {
const ctx = getAudioCtx();
if (ctx && ctx.state === 'suspended') ctx.resume();
window.removeEventListener('click', primeAudio);
window.removeEventListener('keydown', primeAudio);
};
window.addEventListener('click', primeAudio);
window.addEventListener('keydown', primeAudio);
const start = () => {
if (!document.body) {
setTimeout(start, 200);
return;
}
observer.observe(document.body, { childList: true, subtree: true });
const startInInstance = isInInstance();
lastInstanceState = startInInstance;
if (startInInstance) filterEnabled = false;
ensureButton();
applyFilter();
setInterval(() => {
if (checkInstanceStateAndAutoToggle()) {
applyFilter();
}
}, 1000);
};
start();
})();