auto-audio-recorder/extension-firefox/background.js
2019-09-18 23:05:44 -04:00

59 lines
1.5 KiB
JavaScript

let url = 'http://localhost:50233';
function send(data) {
console.log('sending', data);
let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:50233');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*')
xhr.send(JSON.stringify(data));
xhr.addEventListener('abort', (event) => {
console.error('XHR Abort', event);
});
xhr.addEventListener('error', (event) => {
console.error('XHR Error', event);
});
xhr.addEventListener('timeout', (event) => {
console.error('XHR Timeout', event);
});
xhr.addEventListener('load', (event) => {
if (xhr.status != 200) {
console.error('bad xhr response status: ' + xhr.status);
}
});
}
function sendNewSong(song) {
send({ type: 'new-song', song: song });
}
function sendUpdateSong(song) {
send({ type: 'update-song', song: song });
}
function sendAdvertisement() {
send({ type: 'advertisement' });
}
function sendHeartbeat() {
send({ type: 'heartbeat' });
}
browser.runtime.onMessage.addListener((message) => {
message = JSON.parse(message);
switch (message.type) {
case 'new-song':
sendNewSong(message.song);
break;
case 'update-song':
sendUpdateSong(message.song);
break;
case 'advertisement':
sendAdvertisement();
break;
case 'heartbeat':
sendHeartbeat();
break;
}
});