-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsender.html
273 lines (219 loc) · 7.03 KB
/
sender.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html>
<head>
<title>URL Cast Demo</title>
<style type="text/css">
html, body {height:100%; width: 100%; margin: 0; padding: 0; border: 0;}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #FFF;
}
#wrapper {
width: 770px;
position: relative;
top: 20%;
transform: translateY(-20%);
margin: auto;
}
h1 {margin: 0; font-size: 36px;}
input, select, button {
border: 2px solid #CCC;
border-radius: 5px;
font-size: 25px;
font-weight: bold;
padding: 10px 15px;
background: #FFF;
}
input:focus, select:focus, button:focus {outline: none;}
input:focus, select:focus, button:active {
outline: none;
border-color: #8ecaed;
box-shadow: 0 0 5px #8ecaed;
}
select {
padding: 10px 38px 10px 15px;
background: url('./br_down.png') no-repeat right #fff;
-webkit-appearance: none;
background-position-x: 120px;
-webkit-appearance: none;
-moz-appearance: none;
}
input[disabled], select[disabled], button[disabled] {
background: #FAFAFA;
cursor: not-allowed;
}
select[disabled] {
background: url('./br_down.png') no-repeat right #FAFAFA;
-webkit-appearance: none;
background-position-x: 120px;
-webkit-appearance: none;
-moz-appearance: none;
}
.complete, .complete a {text-decoration: line-through; color: #ccc;}
.notready {color: #ccc;}
#notifications {position: fixed; bottom: 0; right: 0;}
.notification {
background: #222;
color: #FFF;
padding: 13px 20px;
margin: 10px;
border-radius: 2px;
}
.error {
background: #e64a4a;
color: #FFF;
padding: 13px 20px;
margin: 10px;
border-radius: 2px;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>URL Cast Receiver</h1>
<p id="step1">Step 1:</p>
<button id="requestSession">Start cast session</button>
<p id="step2" class="notready">Step 2: Send url</p>
<input id="url" class="border" type="text" placeholder="e.g. http://example.com" size="30" value="https://www.windytv.com/" disabled/>
<select id="type" disabled>
<option value="iframe">iframe</option>
<option value="loc">location</option>
</select>
<button id="send" disabled>send</button>
<h3>Details</h3>
<p>
There are two methods for casting, with pros and cons:
<ol>
<li>Load an iframe (You will have trouble loading a url with x-frame-options: same origin.)</li>
<li>Change the window location (You will lose control when you load the url over the receiver.)</li>
</ol>
Also, notice the chromecast viewport is 1280x720 and the default background is black before the receiver loads.
</p>
<p><a href="#">« GitHub</a></p>
</div>
<div id="notifications"></div>
<script type="text/javascript" src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
<script type="text/javascript">
// click handlers
document.getElementById('requestSession').onclick = function() {
chrome.cast.requestSession(sessionListener, onErr);
};
document.getElementById('send').onclick = function() {
var url = document.getElementById('url').value
, type = document.getElementById('type').value;
if (!url || !type) return;
sendMessage({type: type, url: url});
};
var applicationID = '5CB45E5A'
, namespace = 'urn:x-cast:com.url.cast'
, receiverDead = false
, session = null;
// initialize
window.__onGCastApiAvailable = function(loaded) {
if (loaded) initializeCastApi();
};
function initializeCastApi() {
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(
sessionRequest,
sessionListener,
receiverListener
);
chrome.cast.initialize(
apiConfig,
onSuccess.bind(this, 'initialized ok'),
onErr
);
}
function onErr(err) {
console.log('Err: ' + JSON.stringify(err));
showError(err);
}
function onSuccess(msg) {
console.log('Sucess: ' + msg);
}
function sessionListener(newSession) {
console.log('New session ID:' + newSession.sessionId);
session = newSession;
session.addUpdateListener(sessionUpdateListener);
session.addMessageListener(namespace, receiveMessage);
enableInputs();
}
function receiverListener(e) {
(e === 'available')
? console.log('receiver found')
: console.log('no receivers found');
}
function sessionUpdateListener(isAlive) {
if (!isAlive) {
session = null;
disableInputs();
}
console.log('Session is alive?: ', isAlive);
}
function receiveMessage(namespace, msg) {
// namespace = 'urn:x-cast:com.url.cast'
// it only ever says 'ok' - just confirming when a url has been received
console.log('Receiver said: ' + msg);
}
function sendMessage(msg) {
if (receiverDead || !session) return;
// send msg
session.sendMessage(
namespace,
msg,
function() {
console.log('Message sent: ', msg);
notify('Message sent: ' + JSON.stringify(msg));
},
onErr
);
if (msg.type === 'loc') {
receiverDead = true;
disableInputs();
notify('Receiver will now be unresponsive');
}
}
function notify(msg) {
var el = document.getElementById('notifications')
, notice = document.createElement('div');
notice.className = 'notification';
notice.innerHTML = msg;
el.appendChild(notice);
// notice self destruct timer
setTimeout(function() {
el.removeChild(notice);
}, 5000);
}
function showError(err) {
var el = document.getElementById('notifications')
, notice = document.createElement('div');
notice.className = 'error';
notice.innerHTML = 'Got error!: ' + JSON.stringify(err);
el.appendChild(notice);
// notice self destruct timer
setTimeout(function() {
el.removeChild(notice);
}, 5000);
}
function enableInputs() {
document.getElementById('url').disabled = false;
document.getElementById('type').disabled = false;
document.getElementById('send').disabled = false;
document.getElementById('step1').classList.add('complete');
document.getElementById('step2').classList.remove('notready');
document.getElementById('url').focus();
}
function disableInputs() {
document.getElementById('url').disabled = true;
document.getElementById('type').disabled = true;
document.getElementById('send').disabled = true;
document.getElementById('step1').classList.remove('complete');
document.getElementById('step2').classList.add('notready');
}
</script>
</body>
</html>