-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
247 lines (205 loc) · 6.45 KB
/
popup.js
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
const learnButton = document.getElementById('learn');
const mapButton = document.getElementById('start');
const clearButton = document.getElementById('clear');
const selector = document.getElementById("devices");
//for display
//learned input
const learnedElement = document.getElementById('learned');
const learnedData = {
value: [0,0,0]
};
const learnedHandler = {
set(target, property, value) {
// learnedElement.innerText = value;
if(value[0].toString(2).slice(0,4) === '1001') {
learnedElement.innerText = 'Channel ' + (value[0] - 143) + ', Note ' + value[1];
} else if(value[0] === 0) {
learnedElement.innerText = 'none';
} else {
learnedElement.innerText = 'Channel ' + (value[0] - 143) + ', CC ' + value[1];
}
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const learned = new Proxy(learnedData, learnedHandler);
//target element
const targetElementElement = document.getElementById('element');
const targetElementData = {
value: {
tite: 'none'
}
};
const targetElementHandler = {
set(target, property, value) {
targetElementElement.innerText = !value.tagName ? 'none' : value.title ? value.tagName + ' ' + value.title : value.tagName;
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const targetElement = new Proxy(targetElementData, targetElementHandler);
//selected midi input
const selectedMidiInputData = {
value: {}
};
const selectedMidiInputHandler = {
set(target, property, value) {
console.log(value);
selector.value = value;
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const selectedMidiInput = new Proxy(selectedMidiInputData, selectedMidiInputHandler);
//is learning midi note
const midiInputsData = {
value: false
};
const midiInputsHandler = {
set(target, property, value) {
if(value) {
selector.innerText = '';
value.forEach((input) => {
const option = document.createElement('option');
option.innerText = input.name;
option.input = input.name;
selector.appendChild(option)
});
}
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const midiInputs = new Proxy(midiInputsData, midiInputsHandler);
//is learning midi note
const isLearningData = {
value: false
};
const isLearningHandler = {
set(target, property, value) {
if(value) {
learnedElement.innerText = 'Press MIDI note now!';
} else {
learned.value = learned.value;
}
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const isLearning = new Proxy(isLearningData, isLearningHandler);
//is mapping html element
const isMappingData = {
value: false
};
const isMappingHandler = {
set(target, property, value) {
if(value) {
targetElementElement.innerText = 'mapping...';
} else {
targetElementElement.innerText = !targetElement.value.tagName ? 'none' : targetElement.value.title ? targetElement.value.tagName + ' ' + targetElement.value.title : targetElement.value.tagName;
}
return Reflect.set(...arguments);
},
get() {
return Reflect.get(...arguments);
}
}
const isMapping = new Proxy(isMappingData, isMappingHandler);
async function passToContent(eventName, data) {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true
});
const response = await chrome.tabs.sendMessage(tab.id, {
eventName,
data
});
console.log(response);
return response;
}
///////comms routes///////
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.eventName) {
case 'learning':
isLearning.value = request.data;
sendResponse({event: 'learning', data: 'success'});
break;
case 'learned':
learned.value = request.data;
sendResponse({event: 'learned', data: 'success'});
break;
case 'mapping':
isMapping.value = request.data;
sendResponse({event: 'mapping', data: 'success'});
break;
case 'targetElement':
console.log('received')
console.log(request.data);
targetElement.value = request.data;
sendResponse({event: 'targetElement', data: 'success'});
break;
case 'midiInputs': {
midiInputs.value = request.data;
sendResponse({event: 'midiAccess', data: 'success'});
break;
}
}
}
);
//////UI LISTENERS///////
//learn midi command
learnButton.addEventListener('click', async (e) => {
const response = await passToContent('learning', !isLearning.value);
isLearning.value = response.data;
});
//map to element
mapButton.addEventListener('click', (e) => {
isMapping.value = !isMapping.value;
passToContent('mapping', isMapping.value);
});
//clear
clearButton.addEventListener('click', (e) => {
isMapping.value = false;
isLearning.value = false;
learned.value = [0,0,0];
targetElement.value = {
title: 'none'
};
passToContent('clear', 'clear');
});
selector.addEventListener('input', (e) => {
const selected = midiInputs.value.find((input) => {
return input.name === e.target.value;
});
passToContent('selectedMidiInput', selected.name);
});
///////////onload//////////
async function updateData() {
const response = await passToContent('onActivated', true);
if(response !== 'denied') {
const { selectedMidiInput: savedSelectedMidiInput,
learned: savedLearned,
targetElement: savedTargetElement,
midiInputs: savedMidiInputs } = response.data;
learned.value = savedLearned;
targetElement.value = savedTargetElement;
midiInputs.value = savedMidiInputs;
if(savedSelectedMidiInput) {
selectedMidiInput.value = savedSelectedMidiInput;
}
} else {
window.alert('Must permit midi!');
}
}
document.onload = updateData();