-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (33 loc) · 835 Bytes
/
background.js
File metadata and controls
44 lines (33 loc) · 835 Bytes
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
// http://yuyin.baidu.com/docs/tts/136#POST调用方式
var chunks = [];
var audio = document.getElementById('audio');
function textToChunks(text) {
chunks = text.replace(/\s/mg, ' ').match(/[\s\S]{1,500}/g)
}
function sourceUrl(text) {
var url = 'http://tts.baidu.com/text2audio?text='
+ encodeURI(text)
+ '&lan=ZH&ie=UTF-8';
return url;
}
function playUrl(url) {
audio.src = url;
audio.play();
}
function startReader() {
if (chunks.length == 0) return;
var text = chunks.shift();
var url = sourceUrl(text);
playUrl(url);
}
function readText(text) {
if (!text) return;
textToChunks(text);
startReader();
}
audio.onended = startReader;
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id, {}, function(text) {
readText(text);
});
});