-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisten.js
98 lines (86 loc) · 2.92 KB
/
listen.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
// VanGogh - Tweet with your voice
(function () {
var VanGogh = {
options: {
lang: 'es-VE',
continuous: true,
interimResults: false,
},
instance: null,
listenEntity: '<button id="van-gogh-start" type="button" class="btn"><span class="button-text">Start Listening</span></button>',
init: function init() {
this._appendButton();
this._bindOnClick();
},
_appendButton: function _appendButton() {
document.querySelector('.ProfileNav-list .UserActions').insertAdjacentHTML('beforeend', this.listenEntity);
},
_bindOnClick: function _bindOnClick() {
var vanGogh = this;
document.getElementById('van-gogh-start').addEventListener('click', function () {
if (vanGogh.instance) {
vanGogh.instance.stop();
vanGogh.instance = null;
}
vanGogh._initRecognition();
});
},
_initRecognition: function _initRecognition() {
var recognition = new webkitSpeechRecognition();
this.instance = recognition;
Object.assign(recognition, this.options);
recognition.onresult = this._onResult.bind(this);
recognition.start();
document.getElementById('van-gogh-start').textContent = 'Listening';
},
_onResult: function _onResult(event) {
var length = event.results.length - 1;
var transcript = event.results[length][0].transcript.trim();
var command = this.getCommand(transcript);
console.log(transcript);
if (this.hasOwnProperty(command)) {
return this[command](transcript);
}
if (this.writing) {
this.writeMessage(transcript);
}
},
getCommand: function isCommand(transcript) {
var command;
Object.keys(this._commands).forEach(function (key) {
if (this._commands[key] === transcript.toLowerCase()) {
command = key;
}
}, this);
return command;
},
_commands: {
newMessage: 'nuevo mensaje',
sendMessage: 'enviar mensaje',
deleteMessage: 'borrar mensaje'
},
deleteMessage: function deleteMessage(transcript) {
document.getElementById('tweet-box-global').textContent = '';
},
newMessage: function newMessage(transcript) {
console.log('New message');
document.getElementById('global-new-tweet-button').click();
this.writing = true;
},
writeMessage: function writeMessage(transcript) {
if (!this.writing) {
return;
}
var tweetContent = document.getElementById('tweet-box-global').textContent;
console.log('Writing: ' + transcript);
document.getElementById('tweet-box-global').textContent = tweetContent + ' ' + transcript;
},
sendMessage: function sendMessage(transcript) {
this.writing = false;
console.log('Sending message');
document.querySelectorAll('.js-tweet-btn:not(.disabled)')[0].click();
},
};
window.VanGogh = VanGogh;
}());
window.VanGogh.init();