-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathext.js
More file actions
102 lines (85 loc) · 3.16 KB
/
Copy pathext.js
File metadata and controls
102 lines (85 loc) · 3.16 KB
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
var pageToSpeech = {
data: {
highlightedText: "",
speechInProgress: false,
fallbackAudio: null
},
initialize: function() {
// if (this.data.speechInProgress) {
// this.data.fallbackAudio.pause();
// pageToSpeech.data.speechInProgress = false;
// }
if (!pageToSpeech.hasText() || (this.data.speechInProgress)) { return;}
if (!pageToSpeech.trySpeechSynthesizer()) {
pageToSpeech.trySpeechApi();
}
},
hasText: function() {
this.data.highlightedText = window.getSelection().toString();
if (!this.data.highlightedText) {
var input = document.createElement("input");
input.setAttribute("type", "text");
input.id = "sandbox";
document.getElementsByTagName("body")[0].appendChild(input);
var sandbox = document.getElementById("sandbox");
sandbox.value = "";
sandbox.style.opacity = 0;
sandbox.focus();
if (document.execCommand('paste')) {
this.data.highlightedText = sandbox.value;
}
sandbox.value = "";
}
return this.data.highlightedText;
},
trySpeechSynthesizer: function() {
if (window.speechSynthesis ) {
//new speech is about to get started
if (this.data.speechInProgress) {
polyfills.speechUtteranceChunker.cancel = true;
}
this.data.speechInProgress = true;
var msg = new SpeechSynthesisUtterance(this.data.highlightedText);
//speechSynthesis.speak(msg);
// Chrome Implementation BUG: http://stackoverflow.com/questions/21947730/chrome-speech-synthesis-with-longer-texts
polyfills.speechUtteranceChunker(msg, {
chunkLength: 120
},function() {
//speech has finished
pageToSpeech.data.speechInProgress = false;
});
return true;
}
return false;
},
trySpeechApi: function() {
if (this.data.speechInProgress) {
this.data.fallbackAudio.pause();
}
this.data.speechInProgress = true;
this.data.fallbackAudio = new Audio("http://api.voicerss.org/?key=20ae15bb063e44beb4797449de15174c&src=" + this.data.highlightedText);
this.data.fallbackAudio.addEventListener("error", function(evt) {
alert("Sorry, we cannot produce speech right now. Try upgrading your Chrome browser!");
})
this.data.fallbackAudio.play();
this.data.fallbackAudio.onended = function() {
pageToSpeech.data.speechInProgress = false;
}
},
addHotkeys: function() {
var activeKeys = [];
onkeydown = onkeyup = function(evt) {
var e = evt || event;
activeKeys[e.keyCode] = e.type == 'keydown';
if (activeKeys[16] && activeKeys[17] && activeKeys[89]) { //key 16 = SHIFT, 17 = CTRL, 89 = Y
pageToSpeech.initialize();
}
};
}
};
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.action == 'pageToSpeech') {
pageToSpeech.initialize();
}
});
pageToSpeech.addHotkeys();