« get me outta code hell

background.js « 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/background.js
blob: 020a2bef9cd1750354f5f5b9206f5c5932f0fc9e (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
console.log('Start');

const port = browser.runtime.connectNative('interactive_bgm');

browser.browserAction.onClicked.addListener(() => {
    console.log('Hello??');
    // port.postMessage('[{"track": "mantis", "volume": 100}]\n');
});

console.log('Hi', port);

port.postMessage('[{"track": "mantis", "volume": 100}]\n');

port.onMessage.addListener(msg => {
    console.log('Nyoom', msg);
});

/*
setTimeout(() => {
    port.disconnect();
}, 4000);
*/

port.onDisconnect.addListener(() => {
    console.log('Disconnected');
});

browser.runtime.onMessage.addListener(({hostname}) => {
    const map = {
        'scratch.mit.edu': ['mantis'],
        'stackoverflow.com': ['bass', 'main'],
        'www.youtube.com': []
    };

    const mode = map[hostname];

    if (mode) {
        console.log('BGM:', mode);
        port.postMessage(mode.map(track => ({track, volume: 100})));
    } else {
        console.log('No BGM found for ' + location.hostname);
    }
});