-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeybinds.user.js
More file actions
480 lines (425 loc) · 21.6 KB
/
Copy pathkeybinds.user.js
File metadata and controls
480 lines (425 loc) · 21.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
475
476
477
478
479
480
// ==UserScript==
// @name UNIT3D Keybinds
// @namespace https://github.com/flowerey/unit3d-scripts
// @version 1.5.1
// @description Adds keybinds to UNIT3D.
// @author blueberry
// @match https://*/torrents*
// @downloadURL https://raw.githubusercontent.com/flowerey/unit3d-scripts/main/keybinds.user.js
// @updateURL https://raw.githubusercontent.com/flowerey/unit3d-scripts/main/keybinds.user.js.meta.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
const STORAGE_KEY = 'unit3d-keybinds-map';
const DEFAULT_BINDS = {
imdb: { key: 's', ctrl: false, shift: false, alt: false },
letterboxd: { key: 'l', ctrl: false, shift: false, alt: false },
tmdb: { key: 'm', ctrl: false, shift: false, alt: false },
bluray: { key: 'x', ctrl: false, shift: false, alt: false },
nzbgeek: { key: 'd', ctrl: false, shift: false, alt: false },
trailer: { key: 't', ctrl: false, shift: false, alt: false },
edit: { key: 'e', ctrl: false, shift: false, alt: false },
back: { key: 'b', ctrl: false, shift: false, alt: false },
listNext: { key: 'j', ctrl: false, shift: false, alt: false },
listPrev: { key: 'k', ctrl: false, shift: false, alt: false },
listOpen: { key: 'Enter', ctrl: false, shift: false, alt: false },
search: { key: '/', ctrl: false, shift: false, alt: false },
help: { key: '?', ctrl: false, shift: false, alt: false }
};
function loadBinds() {
try {
const stored = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (stored && typeof stored === 'object') {
const merged = { ...DEFAULT_BINDS };
for (const [action, value] of Object.entries(stored)) {
if (typeof value === 'string') {
merged[action] = { key: value, ctrl: false, shift: false, alt: false };
} else if (typeof value === 'object' && value.key) {
merged[action] = { ...DEFAULT_BINDS[action], ...value };
}
}
return merged;
}
} catch (e) { /* ignore */ }
return { ...DEFAULT_BINDS };
}
function saveBinds(binds) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(binds));
} catch (e) { /* ignore */ }
}
const UI = {
toastEl: null,
toastTimeout: null,
showToast(message, type = 'error') {
if (!this.toastEl) {
this.toastEl = document.createElement('div');
this.toastEl.id = 'unit3d-kb-toast';
Object.assign(this.toastEl.style, {
position: 'fixed',
top: '20px',
right: '20px',
padding: '12px 24px',
borderRadius: '4px',
zIndex: '10000',
color: '#fff',
fontWeight: '600',
fontSize: '14px',
boxShadow: '0 4px 12px rgba(0,0,0,0.3)',
transition: 'all 0.3s ease',
pointerEvents: 'none',
opacity: '0',
transform: 'translateY(-20px)'
});
document.body.appendChild(this.toastEl);
}
this.toastEl.innerText = message;
this.toastEl.style.backgroundColor = type === 'error' ? '#e74c3c' : '#2ecc71';
this.toastEl.style.opacity = '1';
this.toastEl.style.transform = 'translateY(0)';
clearTimeout(this.toastTimeout);
this.toastTimeout = setTimeout(() => {
this.toastEl.style.opacity = '0';
this.toastEl.style.transform = 'translateY(-20px)';
}, 3000);
},
helpOverlay: null,
toggleHelp(binds) {
if (this.helpOverlay) {
this.helpOverlay.remove();
this.helpOverlay = null;
return;
}
this.helpOverlay = document.createElement('div');
this.helpOverlay.id = 'unit3d-kb-help';
Object.assign(this.helpOverlay.style, {
position: 'fixed',
top: '0',
left: '0',
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.85)',
zIndex: '20000',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backdropFilter: 'blur(4px)'
});
const content = document.createElement('div');
Object.assign(content.style, {
backgroundColor: '#1a1a1a',
padding: '30px',
borderRadius: '8px',
border: '1px solid #333',
maxWidth: '500px',
width: '90%',
color: '#ddd',
fontFamily: 'system-ui, sans-serif'
});
const isListPage = /\/torrents\/?$/.test(window.location.pathname);
const fmt = (bind) => {
if (typeof bind === 'string') return bind === ' ' ? 'Space' : bind === 'Enter' ? 'Enter' : bind.length === 1 ? bind.toUpperCase() : bind;
const parts = [];
if (bind.ctrl) parts.push('Ctrl');
if (bind.shift) parts.push('Shift');
if (bind.alt) parts.push('Alt');
const k = bind.key === ' ' ? 'Space' : bind.key.length === 1 ? bind.key.toUpperCase() : bind.key;
parts.push(k);
return parts.join('+');
};
const detailBinds = [
{ key: binds.imdb, desc: 'Open IMDb' },
{ key: binds.letterboxd, desc: 'Open Letterboxd' },
{ key: binds.tmdb, desc: 'Open TMDB' },
{ key: binds.bluray, desc: 'Open Blu-ray.com' },
{ key: binds.nzbgeek, desc: 'Search NZBGeek' },
{ key: binds.trailer, desc: 'Search YouTube Trailer' },
{ key: binds.edit, desc: 'Edit Torrent' },
{ key: binds.back, desc: 'Back to Torrents' }
];
const listBinds = [
{ key: binds.listNext, desc: 'Select next torrent' },
{ key: binds.listPrev, desc: 'Select previous torrent' },
{ key: binds.listOpen, desc: 'Open selected torrent' },
{ key: binds.search, desc: 'Focus search box' },
{ key: 'Esc', desc: 'Clear selection / unfocus' }
];
const sharedBinds = [
{ key: binds.help, desc: 'Show/Hide this help' }
];
let bindsHtml = '';
if (!isListPage) {
bindsHtml += `<div style="color:#2ecc71; font-weight:bold; margin-bottom:6px; font-size:13px;">Torrent Detail</div>`;
bindsHtml += detailBinds.map(b => `<div style="display:contents;"><span style="color:#2ecc71; font-weight:bold; text-align:center; min-width:60px;">[${fmt(b.key)}]</span><span style="color:#ccc;">${b.desc}</span></div>`).join('');
}
if (isListPage) {
bindsHtml += `<div style="color:#2ecc71; font-weight:bold; margin-bottom:6px; font-size:13px;">Torrent List</div>`;
bindsHtml += listBinds.map(b => `<div style="display:contents;"><span style="color:#2ecc71; font-weight:bold; text-align:center; min-width:60px;">[${fmt(b.key)}]</span><span style="color:#ccc;">${b.desc}</span></div>`).join('');
}
bindsHtml += `<div style="color:#2ecc71; font-weight:bold; margin-top:10px; margin-bottom:6px; font-size:13px;">General</div>`;
bindsHtml += sharedBinds.map(b => `<div style="display:contents;"><span style="color:#2ecc71; font-weight:bold; text-align:center; min-width:60px;">[${fmt(b.key)}]</span><span style="color:#ccc;">${b.desc}</span></div>`).join('');
content.innerHTML = `
<h3 style="margin-top:0; color: #fff; border-bottom: 1px solid #333; padding-bottom: 10px; font-size: 16px;">Keybinds Help</h3>
<div style="display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin-top: 16px; align-items: center;">
${bindsHtml}
</div>
<div style="margin-top: 20px; text-align: center;">
<button id="kb-settings-btn" style="padding: 6px 16px; background:#333; color:#ddd; border:1px solid #555; border-radius:4px; cursor:pointer; font-size:12px;">Customize Keybinds</button>
</div>
<div style="margin-top: 15px; text-align: center; font-size: 12px; color: #666;">Click anywhere to close</div>
`;
this.helpOverlay.appendChild(content);
this.helpOverlay.onclick = (e) => {
if (e.target.id === 'kb-settings-btn') return;
this.helpOverlay.remove();
this.helpOverlay = null;
};
document.body.appendChild(this.helpOverlay);
content.querySelector('#kb-settings-btn').onclick = (e) => {
e.stopPropagation();
this.helpOverlay.remove();
this.helpOverlay = null;
this.openSettings(binds);
};
},
openSettings(binds) {
if (document.querySelector('.kb-settings-overlay')) return;
const overlay = document.createElement('div');
overlay.className = 'kb-settings-overlay';
overlay.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:29999;';
overlay.onclick = () => overlay.remove();
const panel = document.createElement('div');
panel.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:#1a1a1a;border:1px solid #444;border-radius:8px;padding:20px;z-index:30000;color:#ddd;font-family:system-ui,sans-serif;min-width:340px;max-width:90vw;max-height:80vh;overflow-y:auto;box-shadow:0 8px 32px rgba(0,0,0,0.5);';
panel.onclick = (e) => e.stopPropagation();
const fields = [
{ key: 'imdb', label: 'IMDb' },
{ key: 'letterboxd', label: 'Letterboxd' },
{ key: 'tmdb', label: 'TMDB' },
{ key: 'bluray', label: 'Blu-ray.com' },
{ key: 'nzbgeek', label: 'NZBGeek' },
{ key: 'trailer', label: 'YouTube Trailer' },
{ key: 'edit', label: 'Edit Torrent' },
{ key: 'back', label: 'Back to Torrents' },
{ key: 'listNext', label: 'Next (List)' },
{ key: 'listPrev', label: 'Previous (List)' },
{ key: 'listOpen', label: 'Open (List)' },
{ key: 'search', label: 'Focus Search' },
{ key: 'help', label: 'Toggle Help' }
];
panel.innerHTML = `
<h3 style="margin:0 0 12px 0;color:#fff;border-bottom:1px solid #333;padding-bottom:8px;font-size:14px;">Customize Keybinds</h3>
${fields.map(f => {
const bind = binds[f.key] || { key: '', ctrl: false, shift: false, alt: false };
return `
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:8px;">
<label style="font-size:12px;color:#aaa;min-width:100px;">${f.label}</label>
<div style="display:flex;align-items:center;gap:4px;">
<label style="font-size:10px;color:#666;"><input type="checkbox" data-mod="${f.key}-ctrl" ${bind.ctrl ? 'checked' : ''}> Ctrl</label>
<label style="font-size:10px;color:#666;"><input type="checkbox" data-mod="${f.key}-shift" ${bind.shift ? 'checked' : ''}> Shift</label>
<label style="font-size:10px;color:#666;"><input type="checkbox" data-mod="${f.key}-alt" ${bind.alt ? 'checked' : ''}> Alt</label>
<input type="text" data-bind="${f.key}" value="${bind.key}" style="width:60px;padding:4px 8px;border:1px solid #444;border-radius:4px;background:#111;color:#ddd;font-size:12px;text-align:center;">
</div>
</div>`;
}).join('')}
<div style="display:flex;gap:8px;margin-top:12px;">
<button class="kb-save-btn" style="flex:1;padding:8px;background:#2ecc71;color:#fff;border:none;border-radius:4px;font-weight:bold;cursor:pointer;">Save</button>
<button class="kb-reset-btn" style="flex:1;padding:8px;background:#555;color:#fff;border:none;border-radius:4px;cursor:pointer;">Reset</button>
</div>
`;
panel.querySelector('.kb-save-btn').onclick = () => {
const newBinds = { ...binds };
panel.querySelectorAll('[data-bind]').forEach(input => {
const actionKey = input.dataset.bind;
newBinds[actionKey] = {
key: input.value.trim() || binds[actionKey].key,
ctrl: !!panel.querySelector(`[data-mod="${actionKey}-ctrl"]`)?.checked,
shift: !!panel.querySelector(`[data-mod="${actionKey}-shift"]`)?.checked,
alt: !!panel.querySelector(`[data-mod="${actionKey}-alt"]`)?.checked,
};
});
saveBinds(newBinds);
overlay.remove();
UI.showToast('Keybinds saved!', 'success');
};
panel.querySelector('.kb-reset-btn').onclick = () => {
panel.querySelectorAll('[data-bind]').forEach(input => {
const actionKey = input.dataset.bind;
const def = DEFAULT_BINDS[actionKey];
input.value = def.key;
const ctrlCb = panel.querySelector(`[data-mod="${actionKey}-ctrl"]`);
const shiftCb = panel.querySelector(`[data-mod="${actionKey}-shift"]`);
const altCb = panel.querySelector(`[data-mod="${actionKey}-alt"]`);
if (ctrlCb) ctrlCb.checked = def.ctrl;
if (shiftCb) shiftCb.checked = def.shift;
if (altCb) altCb.checked = def.alt;
});
};
overlay.appendChild(panel);
document.body.appendChild(overlay);
}
};
const LIST_SELECTORS = [
'tr.torrent-search--list__no-poster-row',
'tr:has(.torrent-search--grouped__overview)',
'tr.torrent-search--list__row'
].join(', ');
let currentInstance = null;
class KeybindManager {
constructor() {
this.selectors = {
title: 'h1.meta__title',
metaLink: 'a.meta-id-tag'
};
this.selectedIndex = -1;
this.boundHandler = this.handleKeydown.bind(this);
this.binds = loadBinds();
this.init();
}
getLink(parentSelector) {
const el = document.querySelector(`${parentSelector} ${this.selectors.metaLink}`);
return el ? el.href : null;
}
getMediaInfo() {
const h1 = document.querySelector(this.selectors.title);
if (!h1) return { name: '', year: '' };
const text = h1.innerText.trim();
const match = text.match(/\((\d{4})\)/);
const year = match ? match[1] : '';
const name = match ? text.split(`(${year})`)[0].trim() : text;
return { name, year };
}
getVisibleRows() {
return Array.from(document.querySelectorAll(LIST_SELECTORS)).filter(r => r.offsetParent !== null);
}
highlightRow(index) {
const rows = this.getVisibleRows();
rows.forEach((r, i) => {
r.style.outline = i === index ? '2px solid #2ecc71' : '';
r.style.outlineOffset = i === index ? '-2px' : '';
});
this.selectedIndex = index;
if (index >= 0 && rows[index]) {
rows[index].scrollIntoView({ block: 'nearest', behavior: 'smooth' });
}
}
clearHighlight() {
const rows = this.getVisibleRows();
rows.forEach(r => {
r.style.outline = '';
r.style.outlineOffset = '';
});
this.selectedIndex = -1;
}
init() {
document.removeEventListener('keydown', this.boundHandler);
document.addEventListener('keydown', this.boundHandler);
}
destroy() {
document.removeEventListener('keydown', this.boundHandler);
}
handleKeydown(e) {
const active = document.activeElement;
if (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable) return;
const key = e.key;
const keyLower = key.toLowerCase();
const isListPage = /\/torrents\/?$/.test(window.location.pathname);
const b = this.binds;
const matchesBind = (bindDef, pressedKey) => {
if (!bindDef || typeof bindDef !== 'object') return false;
const keyMatch = bindDef.key.length === 1
? pressedKey.toLowerCase() === bindDef.key.toLowerCase()
: pressedKey === bindDef.key;
return keyMatch &&
!!e.ctrlKey === !!bindDef.ctrl &&
!!e.shiftKey === !!bindDef.shift &&
!!e.altKey === !!bindDef.alt;
};
if (key === 'Escape') {
if (UI.helpOverlay) {
e.preventDefault();
UI.toggleHelp(b);
return;
}
if (isListPage) {
e.preventDefault();
this.clearHighlight();
return;
}
}
if (matchesBind(b.help, key)) {
e.preventDefault();
UI.toggleHelp(b);
return;
}
if (isListPage) {
const rows = this.getVisibleRows();
if (rows.length === 0) return;
if (matchesBind(b.listNext, key)) {
e.preventDefault();
const next = Math.min(this.selectedIndex + 1, rows.length - 1);
this.highlightRow(next);
return;
}
if (matchesBind(b.listPrev, key)) {
e.preventDefault();
const prev = Math.max(this.selectedIndex - 1, 0);
this.highlightRow(prev);
return;
}
if (matchesBind(b.listOpen, key) && this.selectedIndex >= 0) {
e.preventDefault();
const link = rows[this.selectedIndex].querySelector('a[href*="/torrents/"]');
if (link) window.location.href = link.href;
return;
}
if (matchesBind(b.search, key)) {
e.preventDefault();
const searchInput = document.querySelector('input[type="search"], input[name="search"], input.form-control');
if (searchInput) searchInput.focus();
return;
}
}
if (!isListPage) {
const { name, year } = this.getMediaInfo();
const query = encodeURIComponent(`${name} ${year}`.trim());
const links = {
imdb: this.getLink('.meta__imdb'),
letterboxd: this.getLink('.meta__letterboxd'),
tmdb: this.getLink('.meta__tmdb'),
bluray: this.getLink('.meta__blu-ray')
};
const actionMap = {
imdb: () => links.imdb ? window.open(links.imdb, '_blank') : UI.showToast('IMDb link not found'),
letterboxd: () => links.letterboxd ? window.open(links.letterboxd, '_blank') : UI.showToast('Letterboxd link not found'),
tmdb: () => links.tmdb ? window.open(links.tmdb, '_blank') : UI.showToast('TMDB link not found'),
bluray: () => links.bluray ? window.open(links.bluray, '_blank') : UI.showToast('Blu-ray.com link not found'),
nzbgeek: () => window.open(`https://nzbgeek.info/geekseek.php?browseincludewords=${query}`, '_blank'),
trailer: () => window.open(`https://www.youtube.com/results?search_query=${query}+trailer`, '_blank'),
edit: () => {
const path = window.location.pathname.replace(/\/$/, '');
window.location.href = `${window.location.origin}${path}/edit`;
},
back: () => window.location.href = `${window.location.origin}/torrents`
};
for (const [actionName, actionFn] of Object.entries(actionMap)) {
if (matchesBind(b[actionName], key)) {
e.preventDefault();
actionFn();
return;
}
}
}
}
}
const start = () => {
if (currentInstance) currentInstance.destroy();
currentInstance = new KeybindManager();
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', start);
} else {
start();
}
})();