« get me outta code hell

main.js « popup « extension - interactive-bgm - Browser extension that adds background music based on the site you're browsing
about summary refs log tree commit diff
path: root/extension/popup/main.js
blob: ad1475a99afd8425e3f2ef08dabf485b69af096a (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
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
function changeScreen(id) {
    for (const screen of document.getElementsByClassName('screen')) {
        if (screen.id === id) {
            screen.classList.add('visible');
        } else {
            screen.classList.remove('visible');
        }
    }
}

function loadTrackList(opts) {
    const {hostname, siteSettings} = opts;
    const site = siteSettings[hostname] || [];
    return browser.storage.sync.get('tracks').then(({tracks = []}) => {
        const ul = document.getElementById('track-list');
        while (ul.firstChild) {
            ul.removeChild(ul.firstChild);
        }

        tracks.sort();

        for (const track of tracks) {
            const li = document.createElement('li');
            ul.appendChild(li);

            li.classList.add('track');

            const label = document.createElement('label');
            li.appendChild(label);

            const checkbox = document.createElement('input');
            label.appendChild(checkbox);

            checkbox.type = 'checkbox';
            checkbox.checked = site.includes(track);
            checkbox.title = `Toggles whether the track "${track}" will play when this site is opened.`

            checkbox.addEventListener('click', () => {
                if (checkbox.checked) {
                    if (!site.includes(track)) {
                        site.push(track);
                    }
                } else {
                    if (site.includes(track)) {
                        site.splice(site.indexOf(track), 1);
                    }
                }

                if (!siteSettings[hostname]) {
                    siteSettings[hostname] = site;
                }

                disableButton.style.display = 'inline-block';

                browser.storage.sync.set({siteSettings})
                    .then(() => browser.runtime.sendMessage({hostname}));
            });

            label.appendChild(document.createTextNode(' ' + track));

            const deleteButton = document.createElement('button');
            li.appendChild(deleteButton);

            deleteButton.appendChild(document.createTextNode('Delete...'));
            deleteButton.title = `Deletes the track from all sites. You will be confirmed first.`;

            deleteButton.addEventListener('click', () => {
                if (confirm(`This will delete "${track}" from ALL sites - this cannot be undone. Are you sure?`)) {
                    changeScreen('loading-screen');
                    browser.storage.sync.set({tracks: tracks.filter(t => t !== track)})
                        .then(() => loadTrackList(opts))
                        .then(() => changeScreen('main-screen'));
                }
            });
        }

        const actionLi = document.createElement('li');
        ul.appendChild(actionLi);

        actionLi.classList.add('action');

        const addButton = document.createElement('button');
        actionLi.appendChild(addButton);

        addButton.appendChild(document.createTextNode('Create Track'));
        addButton.title = `Creates a new track, which will be an option present in all sites.`;

        let newTrackInput = null;
        addButton.addEventListener('click', () => {
            if (newTrackInput) {
                newTrackInput.focus();
                return;
            }

            const li = document.createElement('li');
            li.classList.add('track');
            ul.insertBefore(li, actionLi);

            newTrackInput = document.createElement('input');
            li.appendChild(newTrackInput);

            const saveButton = document.createElement('button');
            li.appendChild(saveButton);

            saveButton.appendChild(document.createTextNode('Save'));

            saveButton.addEventListener('click', () => {
                while (li.firstChild) {
                    li.removeChild(li.firstChild);
                }
                li.appendChild(document.createTextNode('Saving...'));

                const name = newTrackInput.value.trim();
                if (name.length) {
                    changeScreen('loading-screen');
                    browser.storage.sync.set({tracks: tracks.concat([name])})
                        .then(() => loadTrackList(opts))
                        .then(() => changeScreen('main-screen'));
                }
            });

            newTrackInput.focus();
        });

        const disableButton = document.createElement('button');
        actionLi.appendChild(disableButton);

        disableButton.appendChild(document.createTextNode('Disable Site'));
        disableButton.title = `Removes the entry for this site altogether. It won't change BGM when you open it again.`;

        disableButton.addEventListener('click', () => {
            changeScreen('loading-screen');
            delete siteSettings[hostname];
            browser.storage.sync.set({siteSettings})
                .then(() => loadTrackList(opts))
                .then(() => changeScreen('main-screen'));
        });

        if (!(hostname in siteSettings)) {
            disableButton.style.display = 'none';
        }
    });
}

Promise.all([
    browser.tabs.query({active: true, currentWindow: true})
        .then(([tab]) => {
            const url = new URL(tab.url);
            document.getElementById('hostname').appendChild(document.createTextNode(url.hostname));
            return url.hostname;
        }),
    browser.storage.sync.get('siteSettings')
        .then(({siteSettings = {}}) => siteSettings)
])
    .then(([hostname, siteSettings]) => {
        if (hostname) {
            return loadTrackList({hostname, siteSettings})
                .then(() => changeScreen('main-screen'));
        } else {
            changeScreen('invalid-host-screen');
        }
    });