« get me outta code hell

Switch HSMusic websites.user.js « hsmusic « userstuff - dotfiles - Miscellaneous configuration files of my personal use
about summary refs log tree commit diff
path: root/userstuff/hsmusic/Switch HSMusic websites.user.js
blob: 47c2d0bf9f842dc942f73e59496a80d6d37eda40 (plain)
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
// ==UserScript==
// @name         Switch HSMusic websites
// @description  Then try to take over the world!
// @include      http://localhost:8002/*
// @match        https://hsmusic.wiki/*
// @match        https://preview.hsmusic.wiki/*
// ==/UserScript==

const header = document.getElementById('header');
const page = header.parentElement;
const headerContainer = document.createElement('div');
const customNav = document.createElement('div');

Object.assign(headerContainer.style, {
    display: 'flex',
    flexDirection: 'row',
});
Object.assign(header.style, {
    flexGrow: '1',
});
Object.assign(customNav.style, {
    minWidth: '140px',
    textAlign: 'right',
});

page.replaceChild(headerContainer, header);
headerContainer.appendChild(header);
headerContainer.appendChild(customNav);

const pathname = () => {
    let p = location.pathname;
    p = p.replace(/^\/preview-en/, '');
    return p;
};

const customNavLink = (text, basenameOrFn) => {
    const href = typeof basenameOrFn === 'function' ? basenameOrFn() : basenameOrFn + pathname();
    const link = document.createElement('a');
    link.appendChild(document.createTextNode(text));
    link.setAttribute('href', href);
    customNav.appendChild(link);
    Object.assign(link.style, {
        display: 'inline-block',
        border: '1px dotted var(--primary-color)',
        borderRadius: '2px',
        padding: '2px 8px',
        margin: '2px 2px',
        background: '#000c',
    });
    if (location.href === href) {
        Object.assign(link.style, {
            fontWeight: '800',
            borderStyle: 'solid',
        });
    }
};

customNavLink('L', 'http://localhost:8002');
customNavLink('P', 'https://preview.hsmusic.wiki');
customNavLink('R', 'https://hsmusic.wiki');

let match = location.href.match(/album\/([^/]*)/);
if (match) {
    customNavLink('G', () => `https://github.com/hsmusic/hsmusic-data/tree/preview/album/${match[1]}.yaml`);
}

match = location.href.match(/track\/([^/]*)/);
if (match) {
    const directory = getComputedStyle(document.body).getPropertyValue('--album-directory');
    customNavLink('G', () => `https://github.com/hsmusic/hsmusic-data/tree/preview/album/${directory}.yaml`);
}