-
Notifications
You must be signed in to change notification settings - Fork 12
/
browser.js
70 lines (62 loc) · 1.9 KB
/
browser.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
var util = require('lib/util')
, btoa = window.btoa || util.btoa
, player = null;
/**
* Generate a DOM node with given properties and children.
*
* @param {Object} details - Details of the node to create.
* @param {string} details.name - The tag name of the node to create.
* @param {Object=} details.attrs - Key-value map of attributes to apply to the node.
* @param {Array=} details.children - Array of DOM nodes to append as children to the new node.
* @returns {Node} - The newly created element.
*/
function element(details) {
var el = document.createElement(details.name);
if (typeof details.attrs !== 'undefined') {
Object.keys(details.attrs).forEach(function(key) {
el[key] = details.attrs[key];
});
}
if (typeof details.children !== 'undefined') {
details.children.forEach(function(child) {
el.appendChild(child);
});
}
return el;
}
/**
* Append a player for the given RIFF WAVE stream to the element identified by the given target, replacing an existing player if any.
*
* @param {string} wavFile - The raw RIFF WAVE data for which to create the player.
* @param {string} targetSelector - The element to contain the player.
* @returns {Node} The newly created player.
*/
module.exports = function(wavFile, targetSelector) {
var target;
if (player) {
player.parentNode.removeChild(player);
}
player = element({
name: 'audio',
attrs: {
'controls': true,
'name': 'media'
},
children: [
element({
name: 'source',
attrs: {
type: 'audio/wav',
src: 'data:audio/wav;base64,' + escape(btoa(wavFile))
}
})
]
});
target = document.querySelector(targetSelector);
if (typeof target === 'undefined' || target === null) {
throw new Error('No element matching selector "' + targetSelector + '"');
} else {
target.appendChild(player);
}
return player;
};