-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
232 lines (201 loc) · 6.99 KB
/
index.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Chromecast</title>
</head>
<body>
<h1>Test Chromecast!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<button onclick="init()">init!</button>
<button onclick="reqSession()">request session!</button>
<button onclick="clearThisAll()">clear!</button>
<form method="get" action="update();">
<input id="input" class="border" type="text" size="30"
onwebkitspeechchange="transcribe(this.value)" x-webkit-speech/>
</form>
<button onclick="update()">send message!</button>
<br>
<textarea rows="20" cols="70" id="debugmessage">
</textarea><br>
<script>
let trigID = 0;
const receiverFn = (receivers) =>
new Promise((resolve, reject) => {
let dialog = document.createElement('paper-dialog');
trigID++;
const body = document.getElementsByTagName('body')[0];
const title = 'Choose Cast Device'; // TODO: New translate key needed
const h3 = document.createElement('h3');
h3.textContent = title;
dialog.appendChild(h3);
const listContainer = document.createElement('paper-listbox');
receivers.forEach((receiver) => {
const cast = document.createElement('button');
cast.textContent = receiver.friendlyName;
cast.setAttribute('data-reciever-id', `${receiver.ipAddress}_${receiver.port}_${trigID}`);
listContainer.appendChild(cast);
document.body.addEventListener('click', (e) => {
if (e.target.getAttribute('data-reciever-id') === `${receiver.ipAddress}_${receiver.port}_${trigID}`) {
dialog.parentNode.removeChild(dialog);
dialog = null;
resolve(receiver);
appendMessage(receiver);
//appendMessage(chrome.cast.session);
}
});
});
dialog.appendChild(listContainer);
body.appendChild(dialog);
});
require('electron-chromecast')(receiverFn);
var applicationID = '794B7BBF';
var namespace = 'urn:x-cast:com.google.cast.sample.helloworld';
var session = null;
function init() {
initializeCastApi();
}
function reqSession() {
chrome.cast.requestSession(onRequestSessionSuccess, onError);
}
// You can also require other files to run in this process
require('./renderer.js')
function clearThisAll() {
var debugarea = document.getElementById('debugmessage');
debugarea.innerHTML = "";
}
// /**
// * Call initialization for Cast
// */
// if (!chrome.cast || !chrome.cast.isAvailable) {
// setTimeout(initializeCastApi, 1000);
// }
/**
* initialization
*/
function initializeCastApi() {
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
sessionListener,
receiverListener);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
}
/**
* initialization success callback
*/
function onInitSuccess() {
appendMessage('onInitSuccess');
//chrome.cast.requestSession(onRequestSessionSuccess, onError);
}
/**
* generic success callback
*/
function onSuccess(message) {
appendMessage('onSuccess: ' + message);
}
function onRequestSessionSuccess(e) {
appendMessage('worked' + e)
session = e;
}
/**
* initialization error callback
*/
function onError(message) {
appendMessage('onError: ' + JSON.stringify(message));
}
/**
* callback on success for stopping app
*/
function onStopAppSuccess() {
appendMessage('onStopAppSuccess');
}
/**
* session listener during initialization
*/
function sessionListener(e) {
appendMessage('session init');
appendMessage('New session ID:' + e.sessionId);
session = e;
session.addUpdateListener(sessionUpdateListener);
session.addMessageListener(namespace, receiverMessage);
}
/**
* listener for session updates
*/
function sessionUpdateListener(isAlive) {
var message = isAlive ? 'Session Updated' : 'Session Removed';
message += ': ' + session.sessionId;
appendMessage(message);
if (!isAlive) {
session = null;
}
}
/**
* utility function to log messages from the receiver
* @param {string} namespace The namespace of the message
* @param {string} message A message string
*/
function receiverMessage(namespace, message) {
appendMessage('receiverMessage: ' + namespace + ', ' + message);
}
/**
* receiver listener during initialization
*/
function receiverListener(e) {
if(e === chrome.cast.ReceiverAvailability.AVAILABLE) {
appendMessage('receiver found');
//chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
}
else {
appendMessage('receiver list empty' + e);
}
}
/**
* stop app/session
*/
function stopApp() {
session.stop(onStopAppSuccess, onError);
}
/**
* send a message to the receiver using the custom namespace
* receiver CastMessageBus message handler will be invoked
* @param {string} message A message string
*/
function sendMessage(message) {
if (session != null) {
appendMessage('session: ' + session.ipAddress + ' namespace: ' + namespace);
session.sendMessage(namespace, message, onSuccess.bind(this, 'Message sent: ' + message),
onError);
}
else {
appendMessage('Cannot send because session is null');
}
}
/**
* append message to debug message window
* @param {string} message A message string
*/
function appendMessage(message) {
console.log(message);
var dw = document.getElementById('debugmessage');
dw.innerHTML += '\n' + JSON.stringify(message);
}
/**
* utility function to handle text typed in by user in the input field
*/
function update() {
sendMessage(document.getElementById('input').value);
}
/**
* handler for the transcribed text from the speech input
* @param {string} words A transcibed speech string
*/
function transcribe(words) {
sendMessage(words);
}
</script>
</body>
</html>