forked from orlando/twitter-speech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
146 lines (131 loc) · 4.26 KB
/
speech.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
// Jaimito - Speech API Wrapper
(function () {
var Jaimito = {
_defaults: function _defaults() {
return {
lang: 'en-US',
voice: this.getVoiceByName('Ellen'),
text: 'Hello World!',
volume: 1, // 0 to 1
rate: 1, // 0.1 to 10
pitch: 1 //0 to 2
}
},
// This is expecting an Array of Arrays
// [['message1', 'message2', 'message3'], ['message1']]
say: function talk(messages, config) {
this._sayArray(messages, config);
},
getVoiceByName: function getVoiceByName(name) {
var selectedVoice;
return window.speechSynthesis.getVoices().some(function (voice) {
if (voice.name === name) {
return selectedVoice = voice;
}
});
},
_sayArray: function sayArray(messages, config) {
messages.forEach(function (message) {
if (typeof message === 'Array') {
return this._sayArray(message, config);
}
this._say(message, config);
}, this);
},
_say: function _say(message, config) {
var msg = new SpeechSynthesisUtterance(message);
Object.assign(msg, this._defaults(), config);
msg.text = message;
window.speechSynthesis.speak(msg);
}
};
window.Jaimito = Jaimito;
}());
// Tuit - Tweet DOM wrapper
(function () {
function Tuit(params, tuitero) {
this.tuitero = tuitero;
this.box = params.box;
this.init();
}
Tuit.prototype = {
playEntity: '<div class="tuit-play" style="position: absolute; top: 5px; right: 5px; cursor: pointer;">► Play</div>',
tuitTextSelector: '.js-tweet-text',
init: function init() {
this._setText();
this._appendPlayEntity();
},
_appendPlayEntity: function _appendPlayEntity() {
this.box.style.position = 'relative';
this.box.insertAdjacentHTML('beforeend', this.playEntity);
this._bindPlayEntityEvents();
},
_bindPlayEntityEvents: function _bindPlayEntityEvents() {
var that = this;
this.box.querySelector('.tuit-play').addEventListener('click', function () {
that.tuitero.say(that.text);
});
},
_setText: function _setText() {
var authorName = this.box.querySelector('.fullname').textContent;
var authorHandler = this.box.querySelector('.username').textContent;
var date = this.box.querySelector('._timestamp').textContent;
var textWrapper = this.box.querySelector(this.tuitTextSelector);
this.text = [authorName, authorHandler, date];
if (textWrapper !== null) {
var text = this._sanitizeText(textWrapper.textContent);
this.text.push(text);
} else {
this.text.push('No text');
}
},
_sanitizeText: function _sanitizeText(text) {
var picTwitter = /pic.twitter.com.*\s?$/g
var urls = /(?:https?|ftp):\/\/[\n\S]+/g
text = text.replace(picTwitter, '');
text = text.replace(urls, '');
return text;
},
addText: function addText(text) {
this.text.push(text);
}
};
window.Tuit = Tuit;
}());
// Tuitero - Tweet reader
(function () {
function Tuitero() {
this.init();
}
Tuitero.prototype = {
playAllEntity: '<button id="tuitero-say-all" type="button" class="btn"><span class="button-text">► Play All</span></button>',
init: function init() {
this.brain = {tuits: []};
this._appendEachTuit();
this._appendPlayAll();
},
_appendEachTuit: function _appendEachTuit() {
var that = this;
Array.prototype.forEach.call(document.querySelectorAll('.js-stream-item[data-item-type="tweet"]'), function (item) {
that.brain.tuits.push(new Tuit({box: item}, that));
});
},
_appendPlayAll: function _appendPlayAll() {
document.querySelector('.ProfileNav-list .UserActions').insertAdjacentHTML('beforeend', this.playAllEntity);
this._bindPlayAllEntityEvents();
},
_bindPlayAllEntityEvents: function _bindPlayEntityEvents() {
var that = this;
document.getElementById('tuitero-say-all').addEventListener('click', function (event) {
that.brain.tuits.forEach(function (tuit) {
that.say(tuit.text)
});
});
},
say: function say(input) {
window.Jaimito.say(input);
}
};
window.Tuitero = Tuitero;
}());
window.tuitero = new Tuitero();