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
|
import {getRuleScoreOnPage} from './lib.js';
console.log('Start');
const port = browser.runtime.connectNative('interactive_bgm');
port.postMessage('[{"track": "mantis", "volume": 100}]\n');
port.onMessage.addListener(msg => {
console.log('Nyoom', msg);
});
port.onDisconnect.addListener(() => {
console.log('Disconnected');
});
const uploadCallbacks = {};
const deleteCallbacks = {};
port.onMessage.addListener(({type, trackName}) => {
if (type === 'uploadFinished') {
if (uploadCallbacks[trackName]) {
uploadCallbacks[trackName]();
}
} else if (type === 'deleteFinished') {
if (deleteCallbacks[trackName]) {
deleteCallbacks[trackName]();
}
}
});
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
browser.runtime.onMessage.addListener(async ({urlString, music, type, base64, trackName}, sender, sendResponse) => {
if (urlString) {
browser.storage.sync.get(['siteSettings', 'disableEverywhere'])
.then(({siteSettings, disableEverywhere}) => {
if (disableEverywhere) {
port.postMessage([]);
return;
}
const url = new URL(urlString);
const allRules = siteSettings;
const rulesOnThisPage = allRules.map(rule => ({
rule,
score: getRuleScoreOnPage(rule, urlString)
})).filter(obj => obj.score > 0);
if (!rulesOnThisPage.length) {
return;
}
rulesOnThisPage.sort((a, b) => b.score - a.score);
const {rule} = rulesOnThisPage[0];
const {music} = rule;
port.postMessage(
Array.from(Object.entries(music)).map(([track, volume]) => ({track, volume}))
);
});
} else if (music) {
port.postMessage(
Array.from(Object.entries(music)).map(([track, volume]) => ({track, volume}))
);
} else if (type === 'uploadTrack' && trackName && base64) {
port.postMessage({type: 'uploadTrack', trackName, base64});
return new Promise(resolve => {
uploadCallbacks[trackName] = resolve;
}).then(() => {
browser.notifications.create({
type: 'basic',
title: 'Save Complete',
message: `Successfully saved track "${trackName}".`
});
});
} else if (type === 'deleteTrack' && trackName) {
port.postMessage({type: 'deleteTrack', trackName});
return new Promise(resolve => {
deleteCallbacks[trackName] = resolve;
}).then(() => {
browser.notifications.create({
type: 'basic',
title: 'Delete Complete',
message: `Successfully deleted track "${trackName}".`
});
});
}
});
|