-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathultimate-guitar.user.js
More file actions
788 lines (652 loc) · 27.3 KB
/
ultimate-guitar.user.js
File metadata and controls
788 lines (652 loc) · 27.3 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// ==UserScript==
// @name Ultimate Guitar Layout Optimizer
// @author Luuk Kablan
// @namespace https://github.com/LuckyLuuk12/UserScripts
// @updateURL https://raw.githubusercontent.com/LuckyLuuk12/UserScripts/main/ultimate-guitar.user.js
// @downloadURL https://raw.githubusercontent.com/LuckyLuuk12/UserScripts/main/ultimate-guitar.user.js
// @source https://github.com/LuckyLuuk12/UserScripts/blob/main/ultimate-guitar.user.js
// @homepageURL https://github.com/LuckyLuuk12/UserScripts
// @version 2.2.10
// @description Optimize Ultimate Guitar layout: remove ads, move chords to left sidebar, expand main content
// @match https://tabs.ultimate-guitar.com/tab/*
// @match https://www.ultimate-guitar.com/tab/*
// @run-at document-end
// @grant none
// @license MIT
// @copyright 2025, Luuk Kablan
// ==/UserScript==
(function () {
'use strict';
// Licensed under the MIT License (see LICENSE in repository root).
// Original author: Luuk Kablan
let hasReorganized = false;
const LS_KEY_CHORDS_FONT_SIZE = 'ug-chords-fontsize';
const LS_KEY_TAB_FONT_FAMILY = 'ug-tab-font-family';
const CHORD_TEXT_RE = /^[A-G](?:#|b)?(?:m|maj|min|sus|add|dim|aug)?\d*(?:\/[A-G](?:#|b)?)?$/;
const PROMO_TEXT_PATTERNS = [
/your ai guitar coach/i,
/welcome offer/i,
/try now/i,
/powered by/i
];
function normalizeText(value) {
return (value || '').replace(/\s+/g, ' ').trim().toLowerCase();
}
function findButtonsByLabel(pattern, root = document) {
return Array.from(root.querySelectorAll('button')).filter(btn => {
const aria = normalizeText(btn.getAttribute('aria-label'));
const text = normalizeText(btn.textContent);
return pattern.test(aria) || pattern.test(text);
});
}
function findNavbar() {
return document.querySelector('[role="banner"], header');
}
function findMain() {
return document.querySelector('main');
}
function findHeadingByText(text, root = document) {
const needle = normalizeText(text);
return Array.from(root.querySelectorAll('h1, h2, h3, h4')).find(h => normalizeText(h.textContent) === needle);
}
function headingSet(root) {
return new Set(
Array.from(root.querySelectorAll('h2, h3')).map(h => normalizeText(h.textContent))
);
}
function findMoreVersionsContainer() {
const byAria = Array.from(document.querySelectorAll('[aria-label]')).find(el => normalizeText(el.getAttribute('aria-label')) === 'more versions');
if (byAria) {
return byAria.closest('aside, section, div') || byAria;
}
const heading = Array.from(document.querySelectorAll('h2, h3')).find(h => normalizeText(h.textContent) === 'more versions');
return heading ? (heading.closest('aside, section, div') || heading.parentElement) : null;
}
function findRightSidebar() {
const main = findMain();
const scope = main || document;
const chordsHeading = findHeadingByText('Chords', scope);
if (!chordsHeading) return null;
let node = chordsHeading.closest('section, aside, article, div') || chordsHeading.parentElement;
while (node && node !== document.body) {
const headings = headingSet(node);
if (
headings.has('chords') &&
(headings.has('play next') || headings.has('strumming pattern') || headings.has('get effects'))
) {
return node;
}
node = node.parentElement;
}
return null;
}
function findChordsSection(root) {
const scope = root || document;
const heading = Array.from(scope.querySelectorAll('h2, h3')).find(h => normalizeText(h.textContent) === 'chords');
if (!heading) return null;
let node = heading.parentElement;
while (node && node !== scope) {
if (node.querySelector('[role="tablist"], [role="tabpanel"]')) {
return node;
}
node = node.parentElement;
}
return heading.closest('section, article, div') || heading.parentElement;
}
function applyMainLayoutOverrides() {
const main = findMain();
if (!main) return;
main.style.setProperty('width', '100%', 'important');
main.style.setProperty('max-width', 'none', 'important');
main.style.setProperty('margin-left', '0', 'important');
main.style.setProperty('margin-right', '0', 'important');
main.style.setProperty('box-sizing', 'border-box', 'important');
const topContainer = main.firstElementChild;
if (topContainer) {
topContainer.style.setProperty('width', '100%', 'important');
topContainer.style.setProperty('max-width', 'none', 'important');
topContainer.style.setProperty('margin', '0', 'important');
topContainer.style.setProperty('box-sizing', 'border-box', 'important');
Array.from(topContainer.children).forEach(child => {
child.style.setProperty('max-width', 'none', 'important');
child.style.setProperty('box-sizing', 'border-box', 'important');
});
}
const appShell = document.querySelector('.XSbtP');
if (appShell) {
appShell.style.setProperty('--ug-layout-center-column-width', '100%', 'important');
appShell.style.setProperty('max-width', 'none', 'important');
appShell.style.setProperty('width', '100%', 'important');
}
document.documentElement.style.setProperty('--ug-layout-center-column-width', '100%');
}
function hideSidebar(sidebar) {
if (!sidebar) return;
sidebar.style.display = 'none';
}
function hideCollapseButtons(root) {
if (!root) return;
const buttons = findButtonsByLabel(/collapse|expand/i, root);
buttons.forEach(btn => {
btn.style.display = 'none';
});
}
function moveLoginButtonsToNavbar() {
const navbar = findNavbar();
if (!navbar) return;
const signUp = findButtonsByLabel(/^sign up$/i)[0];
const logIn = findButtonsByLabel(/^log in$/i)[0];
if (!signUp || !logIn) return;
if (navbar.contains(signUp) || navbar.contains(logIn)) return;
const searchBox = navbar.querySelector('input[type="search"], [role="searchbox"]');
let navTarget = null;
if (searchBox) {
let cursor = searchBox.closest('div');
while (cursor && cursor !== navbar) {
const hasTopNavLinks = !!cursor.querySelector('ul[aria-label*="Main" i], [role="list"] a[href]');
if (hasTopNavLinks) {
navTarget = cursor;
break;
}
cursor = cursor.parentElement;
}
}
if (!navTarget) {
navTarget = navbar.firstElementChild || navbar;
}
const existingParent = signUp.parentElement === logIn.parentElement ? signUp.parentElement : null;
if (existingParent) {
existingParent.style.display = 'inline-flex';
existingParent.style.width = 'auto';
existingParent.style.gap = '8px';
existingParent.style.flex = '0 0 auto';
existingParent.style.alignItems = 'center';
existingParent.style.justifySelf = 'end';
navTarget.appendChild(existingParent);
console.log('[UG Script] Moved login buttons to navbar');
return;
}
const wrapper = document.createElement('div');
wrapper.style.display = 'inline-flex';
wrapper.style.gap = '8px';
wrapper.style.width = 'auto';
wrapper.style.flex = '0 0 auto';
wrapper.style.alignItems = 'center';
wrapper.style.justifySelf = 'end';
wrapper.appendChild(signUp);
wrapper.appendChild(logIn);
navTarget.appendChild(wrapper);
console.log('[UG Script] Moved login buttons to navbar');
}
function removeAds() {
const selectors = [
'[id^="ad_"]',
'[id^="google_ads"]',
'bidding-wrapper',
'bidding-unit',
'iframe[src*="doubleclick"]',
'iframe[src*="googlesyndication"]',
'.adsbygoogle'
];
document.querySelectorAll(selectors.join(',')).forEach(node => {
node.remove();
});
}
function getChordsTargetSection() {
const leftContainer = findMoreVersionsContainer();
if (!leftContainer) return null;
const rightSidebar = findRightSidebar();
const chordsSection = findChordsSection(rightSidebar || document);
if (!chordsSection) return null;
return { leftContainer, rightSidebar, chordsSection };
}
function ensureLeftSidebarSpacer(leftContainer) {
if (!leftContainer) return null;
if (leftContainer.__ugSpacer?.isConnected) return leftContainer.__ugSpacer;
const spacer = document.createElement('div');
spacer.className = 'ug-left-sidebar-spacer';
leftContainer.parentElement?.insertBefore(spacer, leftContainer);
leftContainer.__ugSpacer = spacer;
return spacer;
}
function pinLeftSidebar(leftContainer) {
if (!leftContainer) return;
const navbarHeight = Math.max(0, Math.ceil(findNavbar()?.getBoundingClientRect().height || 68));
const top = navbarHeight + 8;
const rect = leftContainer.getBoundingClientRect();
const sidebarBg = getComputedStyle(leftContainer).backgroundColor;
const bodyBg = getComputedStyle(document.body).backgroundColor;
const resolvedBg = (!sidebarBg || sidebarBg === 'rgba(0, 0, 0, 0)' || sidebarBg === 'transparent')
? (bodyBg && bodyBg !== 'rgba(0, 0, 0, 0)' ? bodyBg : '#101010')
: sidebarBg;
const width = Math.max(220, Math.floor(rect.width || leftContainer.offsetWidth || 260));
const left = Math.max(8, Math.floor(rect.left));
const height = Math.max(240, window.innerHeight - top - 10);
const spacer = ensureLeftSidebarSpacer(leftContainer);
if (spacer) {
spacer.style.setProperty('height', `${Math.max(height, leftContainer.offsetHeight)}px`, 'important');
spacer.style.setProperty('width', `${width}px`, 'important');
spacer.style.setProperty('flex', '0 0 auto', 'important');
}
leftContainer.style.setProperty('position', 'fixed', 'important');
leftContainer.style.setProperty('top', `${top}px`, 'important');
leftContainer.style.setProperty('left', `${left}px`, 'important');
leftContainer.style.setProperty('z-index', '200', 'important');
leftContainer.style.setProperty('width', `${width}px`, 'important');
leftContainer.style.setProperty('min-width', '0', 'important');
leftContainer.style.setProperty('box-sizing', 'border-box', 'important');
leftContainer.style.setProperty('max-height', `${height}px`, 'important');
leftContainer.style.setProperty('overflow-y', 'auto', 'important');
leftContainer.style.setProperty('overscroll-behavior', 'contain', 'important');
leftContainer.style.setProperty('padding-right', '6px', 'important');
leftContainer.style.setProperty('background-color', resolvedBg, 'important');
leftContainer.style.setProperty('border-radius', '8px', 'important');
leftContainer.style.setProperty('box-shadow', '0 8px 24px rgba(0, 0, 0, 0.24)', 'important');
}
function makeLeftSidebarSticky(leftContainer) {
if (!leftContainer) return;
pinLeftSidebar(leftContainer);
Array.from(leftContainer.children).forEach(child => {
child.style.setProperty('width', '100%', 'important');
child.style.setProperty('max-width', 'none', 'important');
child.style.setProperty('min-width', '0', 'important');
child.style.setProperty('box-sizing', 'border-box', 'important');
});
}
function findPanelByHeading(sidebar, headingText) {
if (!sidebar) return null;
const heading = findHeadingByText(headingText, sidebar);
if (!heading) return null;
let panel = heading.closest('section, article, div') || heading.parentElement;
while (panel && panel.parentElement && panel.parentElement !== sidebar) {
const parentHeadings = headingSet(panel.parentElement);
if (parentHeadings.has('chords') || parentHeadings.has('strumming pattern') || parentHeadings.has('get effects')) {
break;
}
panel = panel.parentElement;
}
return panel;
}
function removePlayNextAndPromo(sidebar) {
if (!sidebar) return;
const promoCandidates = Array.from(sidebar.querySelectorAll('section, article, div')).filter(node => {
const text = normalizeText(node.textContent);
if (!text || text.length > 500) return false;
if (node.querySelector('h2, h3')) return false;
return PROMO_TEXT_PATTERNS.some(re => re.test(text));
});
promoCandidates.forEach(node => {
const card = node.closest('section, article, div');
if (card && card !== sidebar) {
card.remove();
}
});
}
function movePlayNextToLeftSidebar() {
const layout = getChordsTargetSection();
if (!layout) return;
const { leftContainer, rightSidebar, chordsSection } = layout;
const playNextPanel = findPanelByHeading(rightSidebar || document, 'Play next');
if (!playNextPanel) return;
// Ensure Chords stays first, then Play next, then More Versions.
if (chordsSection.parentElement === leftContainer) {
const nextSibling = chordsSection.nextSibling;
if (nextSibling !== playNextPanel) {
leftContainer.insertBefore(playNextPanel, nextSibling);
}
} else {
leftContainer.insertBefore(playNextPanel, leftContainer.firstChild);
}
playNextPanel.style.setProperty('width', '100%', 'important');
playNextPanel.style.setProperty('max-width', 'none', 'important');
playNextPanel.style.setProperty('min-width', '0', 'important');
playNextPanel.style.setProperty('box-sizing', 'border-box', 'important');
hideCollapseButtons(playNextPanel);
}
function moveChordsToLeftSidebar() {
const layout = getChordsTargetSection();
if (!layout) return;
const { leftContainer, chordsSection } = layout;
makeLeftSidebarSticky(leftContainer);
if (leftContainer.contains(chordsSection)) return;
leftContainer.insertBefore(chordsSection, leftContainer.firstChild);
chordsSection.style.setProperty('width', '100%', 'important');
chordsSection.style.setProperty('max-width', 'none', 'important');
chordsSection.style.setProperty('min-width', '0', 'important');
chordsSection.style.setProperty('box-sizing', 'border-box', 'important');
hideCollapseButtons(chordsSection);
hideCollapseButtons(chordsSection.parentElement);
console.log('[UG Script] Moved chords to More Versions sidebar');
}
function findTwoColumnLayoutRoot(rightSidebar) {
const main = findMain();
if (!main || !rightSidebar) return null;
const tabCode = main.querySelector('code');
if (!tabCode) return null;
let node = tabCode.closest('section, article, div');
while (node && node !== main) {
if (node.contains(rightSidebar) && node.contains(tabCode)) {
return node;
}
node = node.parentElement;
}
return null;
}
function collapseMainToSingleColumn(rightSidebar) {
const layoutRoot = findTwoColumnLayoutRoot(rightSidebar);
if (!layoutRoot) return;
layoutRoot.style.setProperty('display', 'grid', 'important');
layoutRoot.style.setProperty('grid-template-columns', 'minmax(0, 1fr)', 'important');
layoutRoot.style.setProperty('column-gap', '0', 'important');
layoutRoot.style.setProperty('width', '100%', 'important');
layoutRoot.style.setProperty('max-width', 'none', 'important');
Array.from(layoutRoot.children).forEach(child => {
if (child !== rightSidebar) {
child.style.setProperty('grid-column', '1 / -1', 'important');
child.style.setProperty('max-width', 'none', 'important');
}
});
}
function updateChordsFontSize(sizeDelta) {
const leftContainer = findMoreVersionsContainer() || document;
const chordsSection = findChordsSection(leftContainer);
if (!chordsSection) return;
const textNodes = Array.from(chordsSection.querySelectorAll('span, a, button, div')).filter(el => {
if (el.children.length > 0) return false;
const text = (el.textContent || '').trim();
if (text.length === 0 || text.length > 10) return false;
return CHORD_TEXT_RE.test(text);
});
textNodes.forEach(el => {
el.style.fontSize = sizeDelta === 0 ? '' : `calc(1em + ${sizeDelta * 0.15}em)`;
});
}
function getTabContentRoot() {
const main = findMain();
if (!main) return null;
const codeNode = main.querySelector('code');
return codeNode?.closest('section, article, div') || codeNode || null;
}
function getSavedTabFontFamily() {
return (localStorage.getItem(LS_KEY_TAB_FONT_FAMILY) || '').trim();
}
function setSavedTabFontFamily(value) {
localStorage.setItem(LS_KEY_TAB_FONT_FAMILY, value || '');
applyTabFontFamily(value || '');
}
function applyTabFontFamily(fontFamilyValue) {
const value = (fontFamilyValue || '').trim();
const tabRoot = getTabContentRoot();
if (!tabRoot) return;
tabRoot.setAttribute('data-ug-tab-root', '1');
if (!value) {
tabRoot.style.removeProperty('font-family');
const codeNode = tabRoot.querySelector('code');
if (codeNode) codeNode.style.removeProperty('font-family');
tabRoot.querySelectorAll('code, code *, pre, pre *').forEach(node => {
node.style.removeProperty('font-family');
});
return;
}
tabRoot.style.setProperty('font-family', value, 'important');
const codeNode = tabRoot.querySelector('code');
if (codeNode) {
codeNode.style.setProperty('font-family', value, 'important');
}
// UG sometimes applies font styles deep in descendants; apply recursively for reliability.
tabRoot.querySelectorAll('code, code *, pre, pre *').forEach(node => {
node.style.setProperty('font-family', value, 'important');
});
}
function getSavedChordsFontSize() {
return parseInt(localStorage.getItem(LS_KEY_CHORDS_FONT_SIZE) || '0', 10);
}
function setSavedChordsFontSize(value) {
localStorage.setItem(LS_KEY_CHORDS_FONT_SIZE, String(value));
updateChordsFontSize(value);
}
function patchSettingsPopup(dialog) {
if (!dialog || dialog.__ugChordsFontSizePatched) return;
if (normalizeText(dialog.getAttribute('role')) !== 'dialog') return;
if (!/font size/i.test(dialog.textContent || '')) return;
if (dialog.querySelector('.ug-chords-fontsize-setting')) return;
dialog.style.setProperty('width', 'min(92vw, 960px)', 'important');
dialog.style.setProperty('max-width', '960px', 'important');
dialog.style.setProperty('min-width', '640px', 'important');
const anchors = Array.from(dialog.querySelectorAll('div, section, article'));
const fontSizeAnchor = anchors.find(el => /font size/i.test(normalizeText(el.textContent)) && el.querySelector('button, [role="spinbutton"]'));
if (!fontSizeAnchor || !fontSizeAnchor.parentElement) return;
const controlRow = document.createElement('div');
controlRow.className = 'ug-chords-fontsize-setting';
controlRow.style.display = 'flex';
controlRow.style.alignItems = 'center';
controlRow.style.gap = '8px';
controlRow.style.marginTop = '8px';
const label = document.createElement('div');
label.textContent = 'Chords font size';
label.style.fontWeight = '600';
const minusBtn = document.createElement('button');
minusBtn.type = 'button';
minusBtn.setAttribute('aria-label', 'Decrement Chords Font size');
minusBtn.textContent = '-';
const valueNode = document.createElement('div');
valueNode.className = 'ug-chords-fontsize-value';
valueNode.style.minWidth = '20px';
valueNode.style.textAlign = 'center';
const plusBtn = document.createElement('button');
plusBtn.type = 'button';
plusBtn.setAttribute('aria-label', 'Increment Chords Font size');
plusBtn.textContent = '+';
const min = -3;
const max = 6;
const updateValue = (newValue) => {
valueNode.textContent = String(newValue);
setSavedChordsFontSize(newValue);
};
minusBtn.addEventListener('click', () => {
const current = getSavedChordsFontSize();
if (current > min) updateValue(current - 1);
});
plusBtn.addEventListener('click', () => {
const current = getSavedChordsFontSize();
if (current < max) updateValue(current + 1);
});
controlRow.appendChild(label);
controlRow.appendChild(minusBtn);
controlRow.appendChild(valueNode);
controlRow.appendChild(plusBtn);
fontSizeAnchor.parentElement.insertBefore(controlRow, fontSizeAnchor.nextSibling);
updateValue(getSavedChordsFontSize());
const fontInputRow = document.createElement('div');
fontInputRow.className = 'ug-tab-font-family-setting';
fontInputRow.style.display = 'flex';
fontInputRow.style.alignItems = 'center';
fontInputRow.style.gap = '8px';
fontInputRow.style.marginTop = '8px';
const fontInputLabel = document.createElement('label');
fontInputLabel.textContent = 'Tab font family';
fontInputLabel.style.fontWeight = '600';
fontInputLabel.setAttribute('for', 'ug-tab-font-family-input');
const fontInput = document.createElement('input');
fontInput.type = 'text';
fontInput.id = 'ug-tab-font-family-input';
fontInput.placeholder = 'e.g. "Courier New", monospace';
fontInput.value = getSavedTabFontFamily();
fontInput.style.flex = '1 1 auto';
fontInput.style.minWidth = '0';
const applyBtn = document.createElement('button');
applyBtn.type = 'button';
applyBtn.textContent = 'Apply';
applyBtn.setAttribute('aria-label', 'Apply tab font family');
const clearBtn = document.createElement('button');
clearBtn.type = 'button';
clearBtn.textContent = 'Reset';
clearBtn.setAttribute('aria-label', 'Reset tab font family');
applyBtn.addEventListener('click', () => {
setSavedTabFontFamily(fontInput.value);
});
clearBtn.addEventListener('click', () => {
fontInput.value = '';
setSavedTabFontFamily('');
});
fontInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
setSavedTabFontFamily(fontInput.value);
}
});
fontInput.addEventListener('input', () => {
applyTabFontFamily(fontInput.value);
});
fontInputRow.appendChild(fontInputLabel);
fontInputRow.appendChild(fontInput);
fontInputRow.appendChild(applyBtn);
fontInputRow.appendChild(clearBtn);
fontSizeAnchor.parentElement.insertBefore(fontInputRow, controlRow.nextSibling);
applyTabFontFamily(getSavedTabFontFamily());
dialog.__ugChordsFontSizePatched = true;
console.log('[UG Script] Patched settings popup with chords font size control');
}
function observeSettingsPopups() {
if (document.body.__ugSettingsObserver) return;
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType !== 1) continue;
if (node.matches?.('[role="dialog"]')) {
patchSettingsPopup(node);
}
if (node.querySelectorAll) {
node.querySelectorAll('[role="dialog"]').forEach(patchSettingsPopup);
}
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
document.body.__ugSettingsObserver = observer;
}
function setupSettingsButtonListener() {
const settingsBtn = findButtonsByLabel(/^settings$/i)[0] || document.querySelector('button[aria-label="Settings"]');
if (!settingsBtn || settingsBtn.__ugSettingsHooked) return;
settingsBtn.addEventListener('click', () => {
setTimeout(() => {
document.querySelectorAll('[role="dialog"]').forEach(patchSettingsPopup);
}, 100);
});
settingsBtn.__ugSettingsHooked = true;
}
function dismissPopups() {
const activeSettingsDialog = Array.from(document.querySelectorAll('[role="dialog"]')).find(dialog => {
return /font size|settings/i.test(dialog.textContent || '');
});
if (activeSettingsDialog) return;
const dismissButtons = findButtonsByLabel(/^dismiss$/i);
dismissButtons.forEach(btn => {
if (!btn.isConnected) return;
const dialog = btn.closest('[role="dialog"]');
if (dialog && /font size|settings/i.test(dialog.textContent || '')) return;
btn.click();
});
const closeButtons = findButtonsByLabel(/^close$/i);
closeButtons.forEach(btn => {
const areaText = normalizeText(btn.closest('div, section, article')?.textContent);
if (/sale|offer|pro access|ad|promotion/.test(areaText)) {
btn.click();
}
});
const privacyDialog = Array.from(document.querySelectorAll('[role="dialog"]')).find(dialog => {
return /privacy/i.test(dialog.textContent || '');
});
if (privacyDialog) {
const disagree = findButtonsByLabel(/^disagree$/i, privacyDialog)[0];
if (disagree) disagree.click();
}
}
function attachSidebarObserver(sidebar) {
if (!sidebar || sidebar.__ugChordsObserver) return;
const observer = new MutationObserver(() => {
const leftContainer = findMoreVersionsContainer();
if (leftContainer) {
makeLeftSidebarSticky(leftContainer);
}
moveChordsToLeftSidebar();
movePlayNextToLeftSidebar();
removePlayNextAndPromo(sidebar);
hideSidebar(findRightSidebar());
collapseMainToSingleColumn(findRightSidebar());
updateChordsFontSize(getSavedChordsFontSize());
});
observer.observe(sidebar, { childList: true, subtree: true });
sidebar.__ugChordsObserver = observer;
}
function reorganizeLayout() {
if (hasReorganized) return;
const main = findMain();
const navbar = findNavbar();
const leftContainer = findMoreVersionsContainer();
const rightSidebar = findRightSidebar();
if (!main || !navbar || !leftContainer || !rightSidebar) {
setTimeout(reorganizeLayout, 150);
return;
}
console.log('[UG Script] Starting layout reorganization');
removeAds();
dismissPopups();
moveLoginButtonsToNavbar();
applyMainLayoutOverrides();
makeLeftSidebarSticky(leftContainer);
moveChordsToLeftSidebar();
movePlayNextToLeftSidebar();
removePlayNextAndPromo(rightSidebar);
hideSidebar(rightSidebar);
collapseMainToSingleColumn(rightSidebar);
attachSidebarObserver(rightSidebar);
applyTabFontFamily(getSavedTabFontFamily());
updateChordsFontSize(getSavedChordsFontSize());
observeSettingsPopups();
setupSettingsButtonListener();
hasReorganized = true;
console.log('[UG Script] Layout reorganization complete');
}
function layoutNeedsReapply() {
const leftContainer = findMoreVersionsContainer();
const rightSidebar = findRightSidebar();
const chordsInLeft = !!(leftContainer && findChordsSection(leftContainer));
if (!leftContainer || !chordsInLeft) return true;
if (rightSidebar && rightSidebar.style.display !== 'none') return true;
return false;
}
// Run on page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', reorganizeLayout);
} else {
reorganizeLayout();
}
// Auto-dismiss popups periodically
setInterval(dismissPopups, 2000);
// Re-apply layout when UG re-renders widgets without URL changes.
setInterval(() => {
if (!hasReorganized) {
reorganizeLayout();
return;
}
if (layoutNeedsReapply()) {
hasReorganized = false;
reorganizeLayout();
}
}, 1200);
window.addEventListener('resize', () => {
hasReorganized = false;
setTimeout(reorganizeLayout, 120);
});
// Watch for URL changes (SPA navigation)
let lastUrl = location.href;
new MutationObserver(() => {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
hasReorganized = false;
setTimeout(reorganizeLayout, 500);
}
}).observe(document.documentElement, { childList: true, subtree: true });
})();